Directory editor
Once you are used to the Emacs environment and you have made it your own with customizations, it can be very comfortable to work in it. For this reason, many people do much more than text editing in Emacs. One of its strengths is actually that it can replace many other tools, letting you do most of your work with the same kbds and habits.
Among many other things, Emacs is is a powerful file manager.
What is Dired?
Dired (directory editor) can be launched with M-x dired
or C-x d
and choosing the directory to open in the Dired buffer. You can also use wildcards to select a subset of files matching some pattern.
You can quit Dired with the usual C-x k
, but also simply with q
.
Much can be done with it and we won’t have time to cover it all, but if you want to learn more, you can go over the Dired manual. You will see that you can really do a lot in Dired and configure the ls
flags launched by default. The sections below only cover a subset of commands.
Let’s launch Dired.
Opening files
Files can be opened with f
or RET
(the Enter key), they can be opened in another window with o
, opened in another window but without jumping to it with C-o
, or they can simply be viewed with v
.
“Viewing a file” means that the minor mode View Mode is enabled. The file cannot be edited, but it can be read quickly by scrolling up and down by entire screen-full with SPC
and S-SPC
or DEL
. You can quit the file with q
or turn off View Mode while keeping the position in the file with e
.
Your turn:
Navigate to your .bashrc
file and view it.
Deleting files
d
flags a file for deletion, u
unflags it, and x
deletes the files flagged for deletion.
#
deletes all auto-save files and ~
deletes all backup files (see section on Backup files).
% d regexp RET
flags for deletion files matching regexp.
Be careful that deleting files this way is equivalent to running rm
from the command line: files don’t go to a garbage bin, but are truly deleted.
Other operations
You can mark several files with m
(you will see a star *
appear at the start of the line) and perform the operations below on all marked files at once. To remove all marks, press U
. If no file is marked, these operations are performed on the current file (file where the cursor is).
You can copy files with C
, delete them with D
, rename them with R
, create hard links with H
or symlinks with S
.
You can also change mode with M
, change group with G
, or change owner with O
.
You can run touch
(change the timestamp) with T
or compress the file with Z
.
A
will search files for a regexp and Q
will replace regexp with whatever expression you provide.
You can also apply shell commands with !
.
% u
turns names to upper case, % l
to lower case, % R
, % C
, % H
, and % S
will rename, copy, create hard links and symlinks of selected files based on a regexp. This is extremely convenient to quickly renaming many files.
Finally, you can compare files with =
(this will run diff
on both files).
Subdirectories
The content of subdirectories can be viewed in a section below with i
. Sections can then be contracted or extended with $
.
Your turn:
Display the content of the subdirectory projects
.
Editing files
C-x C-q
toggles Wdired mode—a mode in which you can directly edit file and directory names. Once you have edited what you wanted, save the changes with C-c C-c
.
To edit permissions, you need to set the variable wdired-allow-to-change-permissions
to 1 or 2. For this, run M-x customize variable wdired-allow-to-change-permissions
, navigate to “Value Menu”, press Enter, type 1 or 2, press Enter, then navigate to “State”, press Enter, and save the change.
You can now edit the file permissions simply by typing r
, w
, or x
directly in the WDired buffer.
Your turn:
Change the permission of the .bash_logout
file to rw-rw----
.