TranslateProject/sources/tech/20221209.1 ⭐️⭐️ A Linux file manager for Emacs fans.md

165 lines
9.8 KiB
Markdown
Raw Normal View History

[#]: subject: "A Linux file manager for Emacs fans"
[#]: via: "https://opensource.com/article/22/10/linux-file-manager-dired"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
A Linux file manager for Emacs fans
======
In 2009, I was working hard at a startup in Pittsburgh, and in the late evenings of coding, I developed a GNU Emacs habit. The thing about Emacs is that it's just too versatile to close. Whether you're writing code, writing articles about open source, jotting down a task list, or even playing music, you can do it all from within Emacs. And every time you think you've found a task outside of Emacs, you discover an Emacs mode to prove you wrong. One of my favorite reasons to not close Emacs is its file manager, called directory editor or just Dired.
### Install GNU Emacs
Dired is included with Emacs, so there's no install process aside from installing Emacs itself.
On Linux, you can find GNU Emacs in your distribution's software repository. On Fedora, CentOS, Mageia, and similar:
```
$ sudo dnf install emacs
```
On Debian, Linux Mint, Elementary, and similar:
```
$ sudo apt install emacs
```
On macOS, use [Homebrew][1] or [MacPort][2].
For Windows, use [Chocolatey][3].
![Image of dired file directory.][4]
### File management with Dired
Dired mode is a text-based file management system. It can run in the graphical version of Emacs or in the terminal version of Emacs, making it a flexible, lightweight, and approved for use during a [zombie apocalypse][5].
To launch it, press **Ctrl+X** and then **d**. You're prompted in the mini buffer (the field at the bottom of the Emacs window) for the directory you want to open. It defaults to your home directory (`~`).
```
/home/tux:
total used in directory 40 available 88.1 GiB
drwx------. 17 tux  tux  4096 Sep 20 15:15 .
drwxr-xr-x.  5 root root   42 Sep 14 05:29 ..
-rw-------.  1 tux  tux   938 Sep 20 15:28 .bash_history
-rw-r--r--.  1 tux  tux    18 Nov  6  2021 .bash_logout
-rw-r--r--.  1 tux  tux   141 Nov  6  2021 .bash_profile
-rw-r--r--.  1 tux  tux   492 Nov  6  2021 .bashrc
drwxr-xr-x. 16 tux  tux  4096 Sep 20 14:23 .cache
drwx------. 16 tux  tux  4096 Sep 20 14:51 .config
drwxr-xr-x.  2 tux  tux    59 Sep 20 15:01 Desktop
drwxr-xr-x.  2 tux  tux     6 Sep 15 15:54 Documents
drwxr-xr-x.  3 tux  tux   166 Sep 20 15:12 Downloads
-rw-r--r--.  1 tux  tux   334 Oct  5  2021 .emacs
drwx------.  2 tux  tux     6 Sep 20 14:25 .emacs.d
-rw-------.  1 tux  tux    33 Sep 20 15:15 .lesshst
drwx------.  4 tux  tux    32 Sep 15 15:54 .local
drwxr-xr-x.  6 tux  tux    81 Sep 15 16:03 .mozilla
drwxr-xr-x.  2 tux  tux     6 Sep 15 15:54 Music
drwxr-xr-x.  2 tux  tux    59 Sep 20 14:52 Pictures[...]
```
The file listing provided looks familiar to anyone accustomed to `ls -l` in a terminal. From left to right:
- Identifies the entry as a directory, if applicable, and then lists the [file permissions][6]
- The number of hard links to the entry (for example, the `Desktop` entry has 1 hard link representing itself, and 1 file in it)
- User
- Group
- Disk space used, in bytes
- Time last modified
- File name
### Navigation
To navigate Dired, you can use either the arrow keys or [standard Emacs key bindings][7]. For this article, I use Emacs notation: **C-** for **Ctrl** and **M-** for **Alt** or **Meta**.
- **C-p** or **Up** arrow: Previous entry in list
- **C-n** or **Down** arrow: Next entry in list
- **Enter** or **v**: Descend into the selected directory
- **^**: Move "up" the directory tree to the current directory's parent
### Refreshing the view
Dired doesn't redraw the screen for every action, so sometimes you may need to prompt it to refresh. Press **g** to redraw a Dired listing.
### Open a file
One of the reasons you use a file manager is to find a file and then open it. Emacs can't open every file type, but you might be surprised at just how much it can handle. Then again, not everything it can handle is necessarily useful to you. For instance, it's nice that Emacs _can_ open a JPEG but I rarely view a JPEG in Emacs, and I certainly don't use it to edit a JPEG.
Assuming you're considering the types of files you find Emacs useful for, you can open them directly from Dired. That includes text files (Asciidoc, Markdown, HTML, CSS, Lua, Python, and so on) as well as compressed TAR archives.
To close a file that you've opened, use the **C-x****C-k** Emacs binding to invoke the `kill-buffer` function.
### Copy a file
To copy a file from one directory to another, press **C** (that's the capital letter `C`, not the **Ctrl** key). You're prompted to provide a destination directory and file name in the mini buffer at the bottom of the Emacs window.
### Move a file
Moving a file is, confusingly, _renaming_ a file (the exact opposite terminology used in Linux, where renaming a file is actually _moving_ a file.) I've used Dired for years and I still fail to remember this linguistic quirk.
To rename a file, whether you're renaming it back into its current directory or renaming it to some other directory, press **R** (capital `R`.) You're prompted to provide a destination directory and a new file name in the mini buffer at the bottom of the Emacs window.
### Selecting files
There are a few ways to mark selections in Dired. The first is to have your cursor on the same line as a file or directory entry. If your cursor is on the same line as an entry, then that entry is considered the implicit selection. Any action you take in Dired that targets a file targets that one. This includes, incidentally, "marking" a file as selected.
To mark a file as selected, press **m** while your cursor is on its line. You can mark as many files as you want, and each one is considered selected. To deselect (unmark) a file, press the **u** key.
Yet another way to select multiple lines at once is to use a specialized selection function. Dired has several, including `dired-mark-directories` to mark all directories in a list, `dired-mark-executables` to select all binary executables in a list, `dired-mark-files-regexp` to mark files containing a regex pattern, and more. If you're not a regular Emacs user, this is a considered advanced because it requires you to invoke Emacs functions, but here's how to do it and what to look for.
Suppose you want to select all directories in a list:
- Press **M-x** to activate the mini buffer prompt.
- Type `dired-mark-directories` and press **Return** on your keyboard.
- Look at the mini buffer. It tells you how many directories have been marked, and then it tells you that you can invoke this function again in the future with *******/** key combination.
Any function in GNU Emacs that has a key binding associated with it reveals the keys to you after you've invoked it in its long form.
### Creating an archive
To create an archive of a file or a selection of files, press `c` (that's a lower-case `c`, not **Ctrl**). If you have nothing selected (or "marked" in Emacs terminology), then the current line is compressed. If you have files marked, then they're compressed into a single archive. In the mini buffer at the bottom of the Emacs window, you're prompted for a file name and path. Luckily, Emacs is a smart application and derives the target file type from the name you provide. If you name your archive `example.tar.xz`, then Emacs creates a TAR archive with lzma compression, but if you name it `example.zip` then it creates a ZIP file.
### Cancel an action
Should you accidentally invoke a function you don't want to finish, press **C-g** (that's Emacs notation for **Ctrl+G**.) Depending on where you are in the course of the function, you may have to press **C-g** in the mini buffer specifically to stop it from prompting you to continue. This is true for Emacs as a whole, so learn this valuable trick for Dired and carry it over to every mode you use.
### Emacs is always open
To quit Dired, you press **C-x****C-k** to kill the Dired buffer, just as you kill any Emacs buffer.
To quit Emacs altogether, press **C-x****C-c**.
Dired is a very capable file manager, and I've only covered the basics here. For a full list of what Dired can do, press they `h` key.
I think Dired is probably most useful to those using or intending to use Emacs. I probably wouldn't choose it as a general-purpose file manager on a graphical system, because there are so many great alternatives already configured to work with the rest of the system when opening files. Of course, Emacs is infinitely configurable, so if you really enjoy Dired you can set it to do whatever you want it to do.
For a headless system, though, I find that Dired makes a great file manager. Emacs is such a robust operating environment as it is, and Dired only adds to its versatility. With Emacs open, you have a built-in file manager, shell, multiplexer, text editor, and file previewer. You could very nearly use Emacs essentially as your login shell.
Dired is a good text-based file manager, and well worth a look.
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/10/linux-file-manager-dired
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lkxed
[1]: https://opensource.com/article/20/6/homebrew-mac
[2]: https://opensource.com/article/20/11/macports
[3]: https://opensource.com/article/20/3/chocolatey
[4]: https://opensource.com/sites/default/files/2022-10/dired.directory.png
[5]: https://opensource.com/zombie
[6]: https://www.redhat.com/sysadmin/suid-sgid-sticky-bit
[7]: https://opensource.com/article/20/1/emacs-cheat-sheet