HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

File Management with Emacs (dired tutorial)

Advertise Here For Profit

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.

emacs dired

A Screenshot of emacs's feature for file management.

Copy, Delete, Rename a File

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:

KeyPurpose
EnterOpen the file
aOpen the file and close the dired buffer
oOpen the file in a split window, so the dir is still visible
qClose the dir
CCopy file
RRename file
DDelete file
the Operate menu in Emacs's dired mode

Emacs's dired mode's Operate menu.

Mark/Unmark to Operate on Multiple Files

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:

KeyPurpose
mmark a file
uunmark
Uunmark all marked
tswitch marked/unmarked
% mmark 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.

Dired Navigation

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:

KeyPurpose
grefresh dir listing
^go to parent dir.
+create a dir
Zcompress/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).

Menu as Cheatsheet

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.

(info "(emacs) Dired")

Renaming Many Files

See: Emacs: Rename Files Interactively by Pattern (regex; wdired).

Advanced Tips and Settings

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
 ))
blog comments powered by Disqus