Saturday, July 9, 2011

Cool Things to do in Emacs' Dired Mode

Delete some files in the Downloads folder

My Downloads folder is always out of control. There are always a ton of pdf files that I opened once in a browser but don't need to save. It's a pain to open a GUI file navigator, like Nautilus, and double click on each pdf file to view it and see if it should be tossed. However with Emacs, trimming my Downloads directory of old files is quick and easy.

Viewing Pdfs in Emacs

If you see a pdf with an uninformative name like 2010-09.pdf just hit <enter> when the cursor is on the file name, (when the file name is at point). Emacs will open the pdf in a new buffer. Once you have viewed the pdf and have decided its face you can quickly kill the pdf buffer with 'k' and you'll be returned to the directory. Now, if the pdf is unimportant, queue it to be deleted with 'd'. If you want to keep the file, but give it a better name, type 'R' to rename it. To unqueue a file for deletion, use 'u'. And finally to delete all the files currently queued for deletion, type 'x'.

Dired mode emacs

Delete old Emacs backup files

If you have a lot of backup files that emacs created with ~ appended to the file name, you can quickly queue them all for deletion by typing '~'.

Here's the List of Directory Organizing Commands

key combocommand
<enter>open file at point
dQueue File For Deletion
uUnqueue File
~Queue Backup Files for Del
xeXecute Queued Delete Jobs
RRename File
kKill PDF Buffer

Edit Zip'd or Tar-gz'd archives in place

If you need to edit a file inside a zip or tar archive, simply open the archive by hitting <enter> on the name in Dired mode, then click on the file. Edit the file and save it with 'C-x C-s', and then do another C-x C-s inside the zip buffer to save the archive with the file changes. The archive will be compressed again with your changes.

OMG!: Change File Owner, Group, Mode

key combocommand
Ochange owner
Mchange mode
Gchange group
Zcompress file

Encrypt, Decrypt Files

Use ':e' to encrypt the file at point. Emacs will ask who you want to encrypt the file for (ie whose public key to use to encrypt the file.) You can also encrypt the file symmetrically with a password. Once the file is encrypted, there will be two files in the directory: the original file and the encrypted file with '.gpg' added to the file name.


To open the symmetrically encrypted file, click on its name and you will be prompted for a password. You can also type ':d' when the filename is at point.

EasyPG Assistant Docs

The program that is used to encrypt the file's is EasyPG Assistant. EasyPG Assistant Manual

Get More Help

Type 'C-hm' (describe mode) while in dired-mode, to pop open a help buffer, with a description of dired mode and a list of commands.

All Key Combinations named in post

key combocommand
dmark file for deletion
uunmark file
xeXecute queued deletions
Rrename/move file
Ochange owner
Mchange mode
Gchange group
Zcompress file
yshow only certain file type
wcopy file name
RETopen file at point
kif in pdf buffer, kill
C-x C-sSave file or archive
iinsert sub-directory
~mark all backup files for deletion
Cool Things to do in Emacs' Dired Mode


  1. You've missed a few of the greatest features of dired, like pressing Q after marking a few files. That will start a regexp search-and-replace for all your marked files, which is so handy it's hard to understand.

    Also, setting dired-dwin-target to t will ease up your file copying a lot. Let's say you want to copy 7 files from your download folder to a backupfolder, after checking those PDFs. Split your frame into two windows (C-x 2), open a new dired in the other window to your backup folder (C-x d ~/backups), go back to the first window and press C or R (copy or rename, that is, move) and dired will ask if you want to copy to the other window's path. Really handy.

  2. There is one more very useful thing in Dired.

    You can press C-x C-q to enable editing in this buffer. Then you can change permissions, rename files just like changing text in the buffer. Press C-x C-q again when you done and Emacs will do all renaming and permission changes.

    Very useful when you have to rename a couple of files. I use it even when I need to rename just one. ;]

  3. hron - how could I forget about wdired-change-to-wdired-mode? Silly me :(

    C-x C-q in dired is extremly powerful when combined with macros (imagine changing names of some files in a large directory, but instead of having to code some script, just use the macro magic!).

  4. Oscar and hron, those are brilliant features! Thanks so much. I knew I had just scratched the surface and that there would be too much to cover in a single post. If you have any posts on this I'd gladly link to them.

    I'm going to go try them out now!

  5. the ~ trick just made me have a big chuckle, I've got a script that did this before... not now.