Xah Lee, 2006, 2009-08, 2010-03, 2011-03-23
Emacs is a excellent tool for file management. For example, listing files, copy/delete files, rename files, moving files, creating/deleting directory. Once you become familiar with it, you almost never go back to shell or the OS desktop for these tasks.
A Screenshot of emacs's feature for file management.
To start viewing directory, type 【Ctrl+x d】, then type the dir you want. (Or, use the graphical menu 〖File▸Open Directory…〗.) You can now use arrow keys to move around. To open a file, just move cursor to it and press Enter. To view a subdir, just move cursor to it and press Enter. To close the current dir, type q.
Here's a list of common basic commands:
| Key | Purpose |
|---|---|
| Enter | Open the file |
| a | Open the file and close the dired buffer |
| o | Open the file in a split window, so the dir is still visible |
| q | Close the dir |
| C | Copy file |
| R | Rename file |
| D | Delete file |
Emacs's dired mode's Operate menu.
Sometimes you want to copy or delete a set of files. Instead of using the command on one file at a time, you can simply mark the files you want, then apply a command on all marked files. To mark a file, press m. Here's a list of basic marking commands:
| Key | Purpose |
|---|---|
| m | mark a file |
| u | unmark |
| U | unmark all marked |
| t | switch marked/unmarked |
| % m | mark by regular expression |
For example, if you want to mark all html files, type “%m”, then type “\.html$”.
Once you've marked, you can type any of the key for rename, copy, delete etc commands. When there are marked files, commands applies to the marked files. When no files are marked, commands applies to the file under cursor.
The keyboard shortcut 【Ctrl+x d】 calls the command “dired”. “dired” is short for Directory Edit. It is a old term for file management.
Here are other common dired commands:
| Key | Purpose |
|---|---|
| g | refresh dir listing |
| ^ | go to parent dir. |
| + | create a dir |
| Z | compress/decompress the file by gzip |
“dired” is often used in combination with 【Alt+!】 (shell-command). For example, suppose you downloaded a program and is going thru the install process of unzip, untar, configure, make, sudo make install. You can either run these command inside a emacs shell with 【Alt+x shell】, or you can just execute them one at a time with 【Alt+!】. (which method to use is a matter of preference) For more about working with shell, see: Emacs Shell Tutorial (bash, cmd.exe, PowerShell).
When you are in dired mode, there are 3 new graphical menus: “Operate”, “Mark”, “Regex”. These menus lists the commands specific to dired. They are very useful as a cheatsheet. So, if you forgot what's the key to press to do something, look up in these menus.
See: Emacs: Rename Files Interactively by Pattern (regex; wdired).
How to delete or copy a directory?
Emacs by default does not delete non-empty dir. To set it to do so, call “customize-variable” then “dired-recursive-deletes”, then click on the Value Menu to make a choice. Then, click “Save for Future Sessions”, then “Finish”. Do the same with variable “dired-recursive-copies”.
Or, put the following in your emacs init file:
;; allow dired to be able to delete or copy a whole dir. (setq dired-recursive-copies (quote always)) (setq dired-recursive-deletes (quote top)) ;; “always” means no asking. ;; “top” means ask once (top = top dir). ;; any other symbol means ask each and every time for a dir and subdir.
How to copy from one dired dir to the next dired dir shown in a split window?
Call “customize-variable” then “dired-dwim-target”, then set the value to On by clicking the Toggle button. Then, click “Save for Future Sessions”, then “Finish”.
Or, put the following in your emacs init file:
(setq dired-dwim-target t)
Now, when you have dired of different dir in 2 panes, and when you press C to copy, emacs will automatically prompt the other dir path.
How to make dired use the same buffer for viewing directory, instead of spawning many?
In dired, you can press a instead of Enter to open the dir. This way, the previous dir will be automatically closed.
If you want Enter and ^ (parent dir) to use the same buffer, put the following in your emacs init file:
(add-hook 'dired-mode-hook (lambda () (define-key dired-mode-map (kbd "<return>") 'dired-find-alternate-file) ; was dired-advertised-find-file (define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory ))