File Management with Emacs

Xah Lee, 2006

Emacs is a excellent tool for file management, such as viewing directories, creating files, copying files, moving files, creating directory, deleting files or directory. Once you become familiar with it, you almost never go back to shell or your operating system's file viewer 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 hit enter. To view a subdir, just move cursor to it and enter. To close the current dir, type “q”. To copy a file, move cursor to it and type “C”. To rename a file, type “R”. To delete a file, type “D”. Typing “Z” will compress (gzip) the file the cursor is on. (These are under the graphical menu “Operate”)

the Operate menu in Emacs's dired mode

Emacs's dired mode's Operate menu.

Operate On A Set Of 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 execute the command once for all.

To mark a file, type “m”. To unmark a file, press “u”. Type “U” to unmark all marked files. To mark all files, first type “U” to unmark all, then type “t” for toggle. You can also mark files by a regular expression, by typing “% m”, then type the pattern. For example, to mark all files ending in “.html”, type “%m” then “\.html$”.

General Info

The command bound to “Ctrl+x d” is called “dired”, short for directory edit. Here's a list of the most commonly used keystrokes within dired:

keystrokepurpose
+create a dir
Rrename file
Ccopy file
Ddelete file
Zcompress/decompress the file
mmark a file
uunmark a file
Uunmark all
%mmark by regex
gupdate dir listing

“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)

Once you are in dired mode, you will have 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

The most useful rename command is wdired-change-to-wdired-mode. This is under the menu “Immediate‣Edit File Names”. This lets you edit file names in the dired buffer like a normal text file. Once you are done, type “Ctrl+c Ctrl+c” to commit the change. You can also abort your edit by “Ctrl+c Esc”.

You can put this in your emacs init file: “(defalias 'rn 'wdired-change-to-wdired-mode)” so that you can just type “Alt+x rn” to invoke it.

You can also use dired-do-rename-regexp to rename, under the menu “Regexp‣Rename...” with shortcut “%R”. This will let you rename marked files by a regex. For detail, see: Rename Files by a Text Pattern.

Advanced Tips and Settings

How to delete or copy a entire directory

Type “Alt+x 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”.

How to copy from one dired dir to the next dired dir shown in a split pane?

Type “Alt+x 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”.

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. If you want “Enter” and “^” (paren 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
 ))

Related essays:

2007
© 2007 by Xah Lee.