My Personal Notes on dired Mode
1 Dired Basics
To enter dired mode you have several options:
M-x dired
C-x dired
orC-x d
(then specifying the directory)- Simply
C-x C-f
and "find" the "file" that is the directory itself.
R
rename filee
edit fileC
copy filed
mark file as delete,u
unmark file#
marks all auto-save files as deleted~
marks all backukp files (those ending in ~) as deleted+
creates a new directory
r
rename filee
edit filec
compress filed
mark file as delete,u
unmark file#
marks all auto-save files as deleted~
marks all backukp files (those ending in ~) as deleted
__
x
to actually "execute" the delete commandsv
to view file, q to quitM
chmod
__
m
mark filesu
unmark file*
/ marks all directoriest
inverts the marked files.Z
to toggle a file compressed or uncompressedR
to rename marked files or move them to another directory!
open to open the file in the native Mac app. (i.e. open it in finale, or numbers…)
n
next filep
previous fileM
to change permissions i.e. directly typee
to edit ( useC-x b
to get back to the dired buffer )v
to view- while viewing,
=
will show what line you are on s
to searchq
to quit
- while viewing,
s
searchq
quit dired
__ Navigating in dired is almost like navigating in any emacs buffer:
n
scroll down one line (only in dired mode)space
scroll down one line (only in dired mode)C-n
scroll down one lineC-p
scroll up one lineC-v
scroll down one page at a timeM-v
scroll up one page at a timeM-<
scroll to top of diredM->
scroll to bottom of dired
to see these changes, re-open the directory in emacs, i.e. C-x C-f /home/zintis
Or, you can use g
to refresh the entire Dired buffer. revert-buffer
You can also use l
against a file to refresh just a single file
M-x make-directory
to create a new directory while in dired.
1.1 Direct dired edit of file names C-x C-q
You can toggle "dired-toggle-read-only" mode with C-x C-q then just edit the
filnames.
You can't change the metadata, only the names. Is it C-c C-c
to exit?
1.2 dired metadata editing
You can use dired to change file metadata as follows:
H
new <RET> Make hard links to the specified files (dired-do-hardlink). This
is like the shell command ln. The argument new is the directory to make the links in, or (if making just one link) the name to give the link.
S
newsymlink <RET> Make symbolic links to the specified files
(dired-do-symlink). This is like ‘ln -s’. The argument new is the directory to make the links in, or (if making just one link) the name to give the link.
M
modespec <RET> Change the mode (also called permission bits) of the specified files (dired-do-chmod). modespec can be in octal or symbolic
notation, like arguments handled by the chmod program.
GO
newgroup <RET> Change the group of the specified files to newgroup
(dired-do-chgrp).
O
newowner <RET> Change the owner of the specified files to newowner
(dired-do-chown). (On most systems, only the superuser can do this.) The variable dired-chown-program specifies the name of the program to use to do the work. (This variable is necessary because different systems put chown in different places).
T
timestamp <RET> Touch the specified files (dired-do-touch). This means
updating their modification times to the present time. This is like the shell command touch.
1.3 dired grep (search across all files)
Use M-x find-grep-dired
interactively to search and replace a regex across
all files in a specified directory and subdirectories which match a pattern.
Basic usage is:
- invoke, passing your search phrase
M-x find-grep-dired RET catch
(FooException RET - mark buffers of interest
% m .php$ RET
- invoke search and replace across the marked buffers
Q catch
(FooException RET catch (BarException RET
At this point, Emacs will cycle through every match in every file and ask you
to confirm the replacement. If you press !
it will replace the rest of the
matches in the current file, and start prompting you for the next; Y
will
replace every match in every file with no further prompting. M-,
restarts
the interactive prompting if you break out of it.
Going back to the find-grep-dired buffer, and C-x s
will save all changed
files.
1.3.1 Try dired search and replace
Query through what should be replaced or not, in each of the files that are "marked":
- in
dired
, orfind-dired
: (see dired.org for details) Mark
the files you want. You can mark by regex by typing【% m】.
- Type
Q
to calldired-do-query-replace-regexp.
- Type your find
regex
and replacestring
. 〔☛ common elisp regex pattern〕 - For each occurrence, type
y
to replace,n
to skip. Type 【Ctrl+g】 to abort the whole operation. - Type
!
to replaceall
occurrences in current file without asking,N
to skip all possible replacement for rest of the current file. (N is emacs 23 only)
Don't confuse find-dired
with find-name-dired They do different things.
1.3.2 dired M-x rgrep
This is another way to do it, and one I use more often
.
It will ask for:
- the directory where you want to search recursively
- a file pattern for the files you want to include in the search
- the pattern you want to search
As an extra, it will exclude source control private directories from your search (like CVS, .svn or .git).
1.4 M-x find-name-dired.
It reads arguments directory and pattern, and chooses all the files in directory or its subdirectories whose individual names match pattern.
Simply run M-x find-name-dired hammer
and answer the prompts.
1.5 %m (Mark files (regexp))
Regexp mark files:
This will mark all files that match a regular expression. Cool cool!
for example %m\.py
will mark all files that conain ".py" Notice the
escaped . character
.
An example typical workflow is:
- mark all .org file
%m \.org$
i.e. escaped period, org, at end of line - mark all directories
- then invert the selection, if you wanted to move all non-.org files to another directory
2 Filtering files (file globbing)
C-x d *org
to filter on only .org files. or C-x d *.{org,html,doc}
To remove the filter again, use q
This does NOT work when you are already in dired.
* But it DOES work from any
other buffer / mode
. So for example view a file, then C-x d *.html
from
there and she'll be right.
C-x d *.data
to open dired will only files ending in data.
There is also dired-filter
and dired-narrow
. Need to be installed to use.
but try dired-find-file
, dired-find-file-other-window
, and
find-lisp-find-dired-filter
Actually dired-filter
and dired-narrow
were already installed. I don't know
why it did not work earlier.
2.1 M-x find-name-dired
Just enter a regexp for the files you want displayed and voila.
2.2 dired-narrow
I have not installed this yet, but it looks promising. See the blog entry I found in pragmaticemacs.com . After installed, you add this to init.el
;;narrow dired to match filter (use-package dired-narrow :ensure t :bind (:map dired-mode-map ("/" . dired-narrow)))
2.2.1 Hide subdirectories (i.e. to not recursively mark files)
3 Working Within dired
Dired can be used to browse a directory tree, or edit permission or file dates or used to launch emacs on individual files to edit them.
3.1 Launching editor on individual files:
r
to rename the file
e
to edit a file
c
to copy a file (choosing a directory where you want the file??? confirm this)
shift-c
to copy a file (chosing a new name for it)
C-x b
to get back to dired buffer . (this will NOT close the current buffer)
Z
to toggle a file compressed or uncompressed
T
setting the timestamp of file or directory (default is now)
M
chmod a.k.a change permissions
3.2 Navigating within dired
3.2.1 Jumping to file
C-s
to search, that puts the cursor on the next file that matches the search
hammer to open for editing
j
to jump to a file. Very similar.
3.2.2 C-x k to kill subdirectory buffers
moving deeper into a tree structure will leave lots of emacs buffer windows open in your wake. You can clean up as you go by killing buffers you don't need.
3.2.3 ^ to jump to parent directory
When in a directory, the ^
key will immediately jump to the parent directory
3.3 Marking files (for multiple use cases)
d
to mark a file as deleted
to mark an entire directory for deletion (and all subdirectories under this directory)u
to unmark file orU
to unmark all files?%-M
to regexp mark multiple files very powerful, to mark all files matching a regex Then executing an operation on those files, like chmod, or delete, or move etc.. To delete you would hitD
- =%-* In general type this to have several useful options pop up while in dired.
#
marks all auto-save files as deleted~
marks all backukp files (those ending in ~) as deletedm
to mark filest
toggles marking ALL files and not.t
will toggle the XOR set of files, and remember that it itself is a toggle. try it will a few files already marked and see.*/+
to mark all directories*/+t
to mark all directories and then toggle to make it all FILES.
3.4 Working with multiple marked files simultaneously
x
to actually "execute" the delete commands for files marked with d
D
to delete files marked with m
or %-M
!
to excecute other commands on the marked files
A
to search for a regexp within all the marked files
M-,
to move to the next match, even if in the next file
R
to rename marked files or move them to another directory
C
to copy marked files to another directory
to see these changes, re-open the directory in emacs, i.e. C-x C-f /home/zintis
or use g
to revert-buffer (i.e. refresh buffer)
n
next file
p
previous file
e
to edit ( use C-x b to get back to the dired buffer )
v
to view
-while viewing, = will show what line you are on
s to search
q to quit
file in chrome, type !
chrome and voila.
other examples
- finder
- quicktime
- numbers
- preview
3.5 Changing the dired buffer to be editable/writeable.
This is a sub-mode of dired mode, called wdired mode.
Why? Let's say you want to rename a bunch of files .
C-x C-q
Makes the dired buffer editable
M-%
will then let you regexp replace strings and in that way
renaming a bunch of files based on regexp. Very cool
Press C-c C-c
when finished or C-c ESC
to abort changes
Press C-c C-c
when finished or C-c ESC
to abort changes
3.6 Allowing dired to change permissions
If you set wdired-allow-to-change-permissions
to t
Then you can directly change the permissions of files according to your
authority to do so.
Similarly set wdired-allow-to-redirect-links
to t
will allow you to rewrite symlinks
3.6.1 Setting LISP variables interactively
See link to variables in emacs.org here.
In a nutshell, M-x set-variable
dired C-n
til you find:
wdired-allow-to-change-permissions
Then set that to true t
As of emacs version 27.1 (Jan 2021) I did not find dired-allow-to-change-permissions. ?
I had to be in wdired
mode, set by hitting C-x C-q
while in dired. While in
wdired
mode you should see "Editable Dired"
in the message bar. (remember
C-c C-c
to when finished, to get out of wdired
mode.
3.7 Creating, deleting directories
+
to add a new directory
C-x C-f
like usual to create a file in the current directory After entering
the new file's name, you are in that buffer. You can save it C-x C-s
Then
switch back to dired buffer, C-x b
(ord way?) and then see that the new file
is NOT there.
You must refresh the dired buffer with g
After which you new file should show
up. g
is really the "Update the entire contents of the dired
buffer. (revert-buffer
).
3.8 Navigating the Tree
Simple hammer any directory (that means "hit RETURN when cursor is on a directory) To back up a level, hammer .. directory.
Somehow I got a split buffer too. Figure out how later…
Please note that each directory opened does NOT close the previous directory
So, you have to use C-x C-b
to list buffers, and kill the directory buffers
you don't want or you can leave them open in each buffer.
Even better, if you are done with the directory buffer currently open, just
C-x k
to kill the current buffer, which will move the last buffer to the top
which was most likely the last directory you were in.
4 Customizing dired mode.
4.1 Before entering dired
Customizing dired mode is done in either .emacs.d/init.el or .emacs
;; Make dired less verbose (require 'dired-details) (setq-default dired-details-hidden-string "--- ") (dired-details-install) ;; Allow wdired to change file permissions (C-c C-c to commit) (setq wdired-create-parent-directories nil)
Remember that you can 'try' various dired packages from melpa.org using the
'try' package i.e. M-x try
then diredfl
Do this while in a dired buffer and
you'll see subtle changes to the file colours.
Now I can toggle the detail level (hide and show) using the open and close parenthes is (I think)
You can also enter this command in a dired buffer to hide details (it is a
toggle) M-x dired-hide-details-mode
Also, obvious tip is run M-x diredTAB
to see a list of completions to dired
modes, There are lots!! such as
- dired-hide-details-mode
- dired-hide-all
- dired-hide-subdir
- etc.
Then also, coolest of cool, once you have hidden the details, you can toggle
the details just with the (
key. Not the )
key, the (
key
5 Opening a dired buffer
You usual approach is to simply "open the directory" with C-x C-f
but you can also
use one of these approaches:
C-x d
/etcM-x find-dired
Runs find and then opens a dired on the found outputM-x find-name-dired
Search DIR recursively for files matching the globbing pattern
6 Closing the dired buffer
A common mistake is "saving" the direde buffer before exiting, C-x C-s C-x C-c But that will also "save" an ASCII file representation of the directory, which other than historic reasons (to see what the directory looked like at a certain point in time, is useless.
So, just C-x C-c
to leave dired mode and close that buffer.
7 More reading:
Go into info mode, and then selecting dired, to read up the manuals on dired
C-h i
- find emacs manual
- find dired and look for that
Look also here for customizing coloured
M-x customize-group
to get into the customize settings of emacs
dired
to customized dired
8 dired sort
You can sort the dired view. By default pressing the s
key will toggle the
sort order displayed between:
- by date (newest at the top)
- alphabetically (by name)
You may customize
the sort order by providing an argument to the
dired-sort-toggle-or-edit
which is bound to the s
keystroke.
Customization is done with C-u
followed by the key you select. In this case s
.
Net result: just type C-u s
and pass the -lS
switches. You may pass -lhS
for
human readable results.
Alternatively you can run the command directly, with M-x dired-sort-toggle-or-edit s
Common ls options:
c | last modification time |
---|---|
u | last access time |
S | file size |
X | file extension |
You can make it more permanent by adding something like this to your startup
~/.emacs.d/init.el
file.
(setq dired-listing-switches "-Al --si --time-style long-iso")
9 dired diff
Plenty of other useful dired commands. Try running M-x dired
and see what
options are available. You would need to be running == for this
to work well.
9.1 Home
example
python5 = 'source code ' * 5