Dired is Emacs’s very powerful directory browsing and management package. To use it, just type ‘C-x d’ and select a directory; the directory will be displayed in a dired buffer. In this dired buffer, you can mark files either with ‘m’ or by more advanced option, such as by regexp. You can perform very powerful commands such as renaming groups of files by regexp. Dired is actually a huge package, so I’ll only go into a few of the most useful parts of it here; you really should read the manual on it.
Dired can perform operations on either individual files or groups of files that you have marked. The main ways of marking files are by selecting individual files and pressing ‘m’ (’u’ unmarks), or ‘% m’, which prompts for a regexp and matches all files matching that regexp. There are also options for marking temporary files, all directories, symlinks, etc.
Dired also lets you operate on either the current file, if no files are marked, or all marked files. The most useful operation is ‘Q’, which does a query-replace on all selected files. You can copy or move groups of files using ‘C’ and ‘R’. You can also execute shell commands on them, using !. This prompts you for a shell command to execute; if you say something without a ‘*’, such as just ‘cat’, it will run the shell command and display the output in a different buffer. Otherwise, a ‘*’ in the command will be replaced with the filenames.
Like I said, there is a huge amount to Dired, and so you should explore it on your own to find out what is available. If you have menus enabled, you can explore those to find out some of the options.
There are also two additional packages that you probably want, both of which come with GNU Emacs. Dired-x adds additional functionality to dired. Really, I mostly just use it for it’s ability to provide default shell commands to execute on files, but this is enough (actually, looking at the documentation, there’s a few other things I use that are actually from it. Just goes to show how well it integrates with dired). To enable dired-x features, add the following to your initialization file:
(require 'dired-x)
The other package you will want to install is wdired, which allows you to edit dired buffers and have the changes reflected in the filesystem. Once you enter wdired-mode, you can edit filenames using all you emacs commands, and they will be saved - including into any subdirectories added by adding ‘/’ to the filepath. If you set the permissions like I do, you can also edit the permissions. You need some way to get to wdired-mode from dired, so add the following to your .emacs file to be able to just press ‘r’ in dired mode. Once you are finished in wdired-mode and want to save your changes, simply do ‘C-x C-s’ like you are saving a regular buffer and you will switch back to dired.
(require 'wdired) (setq wdired-allow-to-change-permissions 'advanced) (define-key dired-mode-map (kbd "r") 'wdired-change-to-wdired-mode)
I use dired a lot, and so find it very annoying when my dired buffers get out of sync with my filesystem. I help fix this problem using the following pieces of advice around the major buffer-switching and buffer-displaying code. Once switching or displaying the buffer, it refreshes the buffer contents if it is dired-mode. This isn’t perfect - if the filesystem gets out of sync while you are in the dired buffer, it will remain out of sync until you switch to a different buffer - but I haven’t had problems with that so far.
(defadvice switch-to-buffer-other-window (after auto-refresh-dired (buffer &optional norecord) activate) (if (equal major-mode 'dired-mode) (revert-buffer))) (defadvice switch-to-buffer (after auto-refresh-dired (buffer &optional norecord) activate) (if (equal major-mode 'dired-mode) (revert-buffer))) (defadvice display-buffer (after auto-refresh-dired (buffer &optional not-this-window frame) activate) (if (equal major-mode 'dired-mode) (revert-buffer))) (defadvice other-window (after auto-refresh-dired (arg &optional all-frame) activate) (if (equal major-mode 'dired-mode) (revert-buffer)))
That’s all the customizations I have for dired currently, but there are still other packages that don’t currently come with Emacs that customize it further, such as dired+. I’d recommend going here and looking for further customizations you like, as well as reading the dired manual to find more information on it.
Tags: emacs
thanks.
Is there any extension/package which would take a name (in C-s style) and narrow down the listing (in dired buffer) to only those files which (fuzzy) match ?
chaitanya:
There isn’t one that I know of, but it wouldn’t suprise me. Another option is to do M-x find-dired, which will run find with the arguments you specify and put the contents in a dired buffer.
Instead of all those advices, why don’t you simply enable `auto-revert-mode’ to keep dired buffers in sync with the file system? Works like a charm for me, and I don’t need to switch buffers to get a refresh.
[...] Nice #emacs dired-mode introduction: http://nflath.com/2009/07/dired/ [...]
Regarding auto-reverting dired:
I have global-auto-revert-mode on, but by default it doesn’t revert non-file buffers like dired buffers. If you set global-auto-revert-non-file-buffers to non-nil, it auto-reverts them most of the time. See Section 22.5 of the manual for details (http://www.gnu.org/software/libtool/manual/emacs/Autorevert.html#Autorevert).