Picking back up on the Emacs series I started earlier this month I would like to take some time to discuss the Dired mode for directory management.
I’ve used ECB in the past and found it nice, feature rich but in my opinion it had too much information and was in my way, not to mention it drastically increases emacs load time. So I removed it from my configs and now only use Dired. Once you get used to it it really is very powerful.
h3. The Basics
To initiate a Dired buffer type C-x d
and you will be prompted for a directory path starting in the directory of your current open buffer. Navigate the path you want to open and return to open it. If you haven’t extended Dired with additional modes, you’ll get something that looks like a terminal directory listing (ls -lna
). I’ll tell you how to change this later.
Navigation
Movement in Dired is exactly like any buffer so I won’t go into that here. For long directories I always navigate with iSearch (C-s
and C-r
). It’s much easier to jump to a particular location.
To open a folder press f
(find-file). To go up a directory type ^
. Every time you open a new directory a new buffer is opened in the background for you and always accessible until you kill it.
Top view a file in read-only mode, press v
. To return to the Dired buffer press q
.
o
will open the file in another window while RET
or f
will open the file in the current window. At anytime in the Dired buffer you can sort by typing s
.
Deleting files
Dired allows you to perform actions on files and folders such as deletion, renaming, etc.
To delete files, you first must mark the files you want to delete by navigating to the file or folder and pressing d
which will place a capital D to the left of the file. When you are done with your selections x
will initiate the actions and you will be prompted to confirm your actions. Typing #
marks all the autosave files for deletion with a D status and typing ~
marks all the backup files for deletion with the same status. At any time, if you accidentally mark a file you can press u
to unmark it.
Copying and Renaming Files
To copy a file first navigate to it and press C
(capital C). Emacs will ask you for the name of the file to copy to. Enter will execute the copy. If you type C4
from the file you are on, the current file and the next three will be copied.
To rename a file type R
. Are you getting the naming conventions yet? Emacs will ask you for the new file name and again, Enter will execute it.
File Compression
You can compress and uncompress files from Dired by typing Z
. You will be asked to confirm. Z
is universal in that it will uncompress a compressed file or compress it if it’s not compressed already.
Diffs
Dired lets you compare a file with its respective backup file. This has come in handy many times for me. Find the file you want to compare and press =
. You’ll be presented a buffer showing you the difference in the two files.
Extending
To make the dired buffer a little more presentable and less like terminal I installed a few other modes. Install Dired+ which will give you extra command and expand on existing commmands. It will also give you some better file highlighting than what is present in the default Dired mode. I also installed Dired-details and Dired-details+ to customize the look of my Dired buffers like the picture above.
With these additions I added the following to my dotemacs files to hide files I don’t want to see, such as .pyc, backups and autosave files.
(require 'dired-x)
(setq dired-omit-files
(rx (or (seq bol (? ".") "#") ;; emacs autosave files
(seq "~" eol) ;; backup-files
(seq bol "svn" eol) ;; svn dirs
(seq ".pyc" eol)
)))
(setq dired-omit-extensions
(append dired-latex-unclean-extensions
dired-bibtex-unclean-extensions
dired-texinfo-unclean-extensions))
(add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
(put 'dired-find-alternate-file 'disabled nil)