Using Dired as a file manager
Table of Contents
Python with Elpy
Julien is going to present on this. His notes are here: https://github.com/julienchastang/emacs-2016-05-24
Using dired as a file manager in Emacs
For reference, anything I've taken from my config can be seen here: http://writequit.org/eos/eos-dired.html
Dired is a built-in feature of Emacs that builds a file listing and allows you to see/modify/open/etc files that are in a directory.
One of the great things about it is that it also supports all of the abstractions that Emacs does, like TRAMP for accessing remote machines.
You might have seen it if you open a directory with find-file
by accident
instead of a file, but you can invoke it manually by doing M-x dired-jump
. It
defaults to the current directory. By default, you can also hit C-x d
and be
prompted for where to open a dired buffer.
(I like to bind dired-jump
to C-x C-j
but that's just my preference)
Key | Action |
---|---|
C-x d | Prompt for directory and open dired there |
Once inside of dired, you can navigate and perform actions with the following: (this is a subset that I find the most useful)
Key | Action |
---|---|
n/p | next-line/previous-line |
RET | open a file or directory |
f | open the file |
e | open the file |
o | open the file in the other window (alongside) |
! | perform a shell function on the file |
& | perform a shell function asynchronously on the file |
( | hide/unhide details for the listing |
+ | prompt to create a directory |
Z | compress or uncompress file at point |
g | refresh buffer |
i | insert the sub-directory at point |
m | mark a file to do something on it later |
C | copy the file to a location |
R | rename/move the file to a location |
D | delete the file |
d | mark a file to be deleted |
x | perform the deletion of the files that are marked as deleted |
M | chmod a file (change permissions) |
O | chown a file (change owner) |
G | chgrp a file (change group) |
q | quit/close the dired window |
u | unmark a file |
U | unmark all files |
DEMO
Customizing Dired
Dired has a bunch of options, here are some that I like setting
dired-listing-switches
This changes the switches that are passed to the ls
command when a dired
buffer has been generated. On Linux I like to set mine to:
(setq dired-listing-switches "-lFaGh1v --group-directories-first")
On OSX, however, ls
doesn't support all this, so I do the following:
;; brew install coreutils (if (executable-find "gls") (progn (setq insert-directory-program "gls") (setq dired-listing-switches "-lFaGh1v --group-directories-first")) (setq dired-listing-switches "-ahlF"))
gls
is the GNU version of ls
that can be installed, if it doesn't exist I
fall back to a different set of switches (just on OSX).
ls-lisp-dirs-first
By default dired will list files and directories in alphabetical order. If you
would rather have directories at the top of the file and files below, you can
set ls-lisp-dirs-first
to t
and they will always be at the top.
(setq ls-lisp-dirs-first t)
Recursive copying and deleting
When deleting or copying files, you can have dired default to acting recursively by setting:
(setq dired-recursive-copies 'always) (setq dired-recursive-deletes 'always)
Showing symlinks differently
Just like with ls
, if you want an "@" appended to the end of file names if
they're symlinks, you can set the following:
(setq dired-ls-F-marks-symlinks t)
Instead of a hard delete, move a file to the trash
This one is great, does what it says:
(setq delete-by-moving-to-trash t)
Packages to enhance Dired
There are a bunch of packages that can enhance dired or make it better, here are some that I use
dired-x
This is a package, but not really an external one because it's built into Emacs,
by doing this you can easily jump to dired while browsing for files with C-j
.
(require 'dired-x)
Here's how I like to configure dired-x:
(use-package dired-x :init (setq-default dired-omit-files-p t) :config (add-to-list 'dired-omit-extensions ".DS_Store"))
dired-aux
Also built into Emacs, this is what provides the chgrp, chown, chmod functionality as well as some other builtins
(require 'dired-aux)
wdired
Another awesome extension for Dired that's built into Emacs, this makes the
dired buffer writable, you can hit C-x C-q
to enter the mode, make your
changes and then hit C-c C-c
to apply them, or C-c ESC
to discard them.
DEMO
dired+
Dired+ provides additional fontification (colors), enhances some of the existing
functionality (like the i
command), allows operating on all files or files and
directories in the directory and adds the diredp-hide-details-initially-flag
that allows hiding or showing details automatically when opening new Dired
buffers.
dired-narrow
The dired-narrow function (which hails for the dired-hacks project) allows you
to narrow a dired buffer to only files that match a particular name. I like to
bind it to /
in my config
(use-package dired-narrow :ensure t :bind (:map dired-mode-map ("/" . dired-narrow)))
Asynchronous Dired
You can make the copy and rename/move commands in dired by installing the
async
package. From there, all you need to do is:
(require 'dired-async)
And the commands will automatically by asynchronous
quick-preview
Not dired-specific per-se, but quick-preview is great for files that Emacs might not be able to open, but your regular X11 (or OSX's quick preview) can show well, like moves are things. Works great when you're in a dired buffer to preview a file.
I like to bind it globally to C-c q
and in dired to Q
(use-package quick-preview :ensure t :init (global-set-key (kbd "C-c q") 'quick-preview-at-point) (define-key dired-mode-map (kbd "Q") 'quick-preview-at-point))