
This blog post is a part of the series of posts about my Emacs configuration. Previous posts:
Here I will be dedicated to the configuration, and my habits, and usual actions during the Dired usage. I decided not to just describe configuration and that's all — you could read a lot of likewise blogposts already. Instead of it I also would like to describe how I am using Dired every day.
Little explanation about Dired for readers, who didn't use it before. It is a program for Emacs which works as a powerful file browser, which treats your precious files and directories as an (almost) usual Emacs buffer — so you can navigate through and work with you catalogs using the same "emacs-habits".
Since, I tend to use a kinda minimalist environment for computing — first, I used a CLI (bash then zsh) for the most of the operations with files and catalogs. It works well, but I wanted to have some interactivity — copy/move some files from one catalog to another catalog without issuing multiple cp commands1, view contents of one directory while browsing another catalogs, like in Midnight Commander, etc.
Some time I used the mentioned mc and it went well. But I wanted more — preview images, etc — and the Midnight Commander was unable to help me with this, since it doesn't have plugins like Far. Yes, it has a good menu, binded to F2 key, able to display various commands for various file types. But it wasn't enough for me.
So, by the way, I decided to give Dired a try — since it is already installed with almost every Emacs distribution. And things went so well, that I'm staying with the Dired now.
Usually I'm invoking the Dired in two ways. First, if I'm working with some project2, I press C-x p d hotkey and type some letters to jump into the desired catalog inside the project.
E.g. if I work with my dotfiles and want to open directory configs I type:
And fall into the desired catalog:
If I want just to open the root catalog of the project — I use C-x p D keybinding instead.
Second, my beloved, method — the C-x C-j keybinding3. It opens the Dired buffer in the current working directory. For example, if I want to work with files for my blogpost, while I'm editing the post itself — I just press C-x C-j and buffer with article's Org file and images opens:
Of course, there are some other methods to invoke the Dired, which I'm using. For example the default C-x d keybinding, which I rebinded to C-x C-d for convenience:
I'm using this keybinding most of the time, when I want just to open some directory, local or remote (with the help of TRAMP). E.g. this is how I could open bin catalog on the not so remote server server.example.com with the username user account:
Also, in my system the Emacs frame with Dired buffer could be opened from any program which could open the file manager — e.g. after downloading file in Librewolf, I could press the folder icon in the "Downloads" drop-down menu and the Emacs frame with Dired buffer for directory with my downloaded file will be opened:
I already described how to setup Emacs as a default file manager here: Emacs Dired as a default file manager.
Usual file operations: copying, moving or removing sets of files — are pretty simple. "Just" mark some files with m keybinding and copy/move them (see the 32.7 Operating on Files in the Emacs Manual).
I placed just in the quotes, because there is not only m keybinding to mark files. There are a lot of ways to do so — look at the 32.6 Dired Marks vs. Flags section from Emacs manual. Most of the time I'm using the next ways to mark some files:
For example, I'm using * . to mark unwanted files by it's extension and delete them. E.g. to delete all web-pee images: * . webp RET D y RET. Neat thing: if cursor already on the line with WEBP image, then the * . RET D y RET is enough, the webp part is preselected by default.
By the way, these long "keybindings" are not real — there are two separate keybindings (. * and D), my answer for the prompt about file extension to select, and answer for the file deletion confirmation.
Or another example: if I want to convert all WEBP images to the JPG (I dislike WEBP as a corporate image format, to be honest), then I'll do it in the next way:
You may wonder — how I define gm convert command for image files? The answer is in the External programs section.
Another favorite keybinding is the % m — with the power of regular expressions I'm finally able to select any files in some catalog in a very tricky way — this is a thing that I was missing while using CLI or mc for file operations. IIRC, the mc allows to select files by regexp, but in Dired this feature a way richer.
Here the synthetic example, where I'm selecting image files starting from e6f substring and ending with .webp or .webp.SOME_3_LETTER_EXTEN — with the help of ^e6f.+.webp.?([a-z]{3})? regexp:
Separately from described marks, files also could be flagged for deletion — mark and flags are different inside Dired, so I able to mark some files to copy them to another place; and simultaneously flag another files to delete them. These flagged files will have another face than the marked files — e.g. if marked files have purple face (see the screenshot above), then the flagged files will have the red one:
To flag by hand there is a d hotkey. And to delete flagged files the hotkey x could be used. And of course, there are much more keybindings, which I'm using to flag files for deletion:
The "garbage files" defined by the dired-garbage-files-regexp variable. E.g. in my configuration the artifacts from LaTeX build are considered "garbage":
So, when I press % &, then all LaTeX build artifacts will be selected in the current directory for deletion.
The % d works like % m keybinding, but it flags files instead of marking them.
Let's look to the one of the most powerful Dired features. Since directory listing just a buffer in the Emacs — it could be edited as a usual text? Of course!
This is that the WDired mode does — it makes Dired buffer writable. So the filenames could be edited as usual text: with the help of isearch-query-replace-regexp, macros and other cool Emacs features.
To enter WDired mode the C-x C-q keybinding could be used. And the C-c C-c keybinding will return to the usual Dired mode with all the changes applied. With the C-c C-k keybinding to return to the usual Dired mode with all the changes discarded.
I often use WDired mode to strip unwanted text from filenames. E.g. I want to remove .fb2 part from a lot of filenames like:
In this situation I just perform the next things:
Another good example (without regular expressions). Here, I have audio files from "Regular Expressions" (Регулярные выражения) podcast:
As you can see, the files are badly named: months, days and hours don't have the prepending 0 for numbers less than 10, so the files sorted in the wrong way. I can fix it by renaming everything in the Dired buffer:
Of course, some automation with regular expression could be also performed here, but I just want to show that WDired buffer could be edited literally as a text.
By default, WDired allows to change only filenames. But it also able to edit Unix access rights with r, w and x buttons with the next line in the configuration:
If dired-hide-details-mode is disabled in the Dired buffer (it could be done by the pressing to the ( key), then access rights will be visible and could be changed with the next keys:
Basically, there is nothing Dired-specific — just create two vertical windows and open Dired buffers in each of them.
But, there is one caveat exists — with default settings you will not be able to mimic the common thing from mc: copy files between two directories with substituted path to the destination directory, like mc or Far does.
To fix it, I just set the next option:
And after pressing C (copy) or R (rename/move) the path for destination is substituted from the second Dired window.
Common search operations could be performed as usual, like in any other Emacs buffer (e.g. with C-s). I already provided some not so easy examples above.
There is one Dired-related variable, which I changed to search only for filenames (and skip catalogs) with C-s hotkey:
Another common hotkey, which I'm using, is a j. It allows to move cursor to the desired file or directory in the Dired buffer just by entering it's name:
But, of course, there are much more commands to use. There are two notable and very useful commands:
I don't know what's up in the Linux world, but in the BSD world there are the next settings necessary to use these commands and see the nice output in the Emacs, since the Dired defaults are for Linux coreutils but not for the BSD coreutils:
First cool command is M-x find-name-dired. It searches for files with given filename (wildcards allowed) in the given catalog. The found files are shown in the nice Dired buffer with all Dired-related features, mentioned above and below.
For example, the result of M-x find-name-dired RET /bin RET c* RET:
Basically, it is just an interface for find DIR -name NAME command, with Dired buffer attached. So I'm using it mostly to search for some files, e.g. for some Java files when I'm know only the class name.
Second cool command is the M-x find-grep-dired. It searches for files with given content (regexp not only allowed but strongly recommended!) in the specified catalog. The found files also shown in the newly opened Dired buffer.
E.g. the result of M-x find-grep-dired RET ~/rsync/blog/articles/it/ RET [Ee]macs RET:
This command is just an Emacsified version of grep -r '[Rr]egex' *, so it is used in the same way — when I need to search for some text in files. Of course, the buffer with results is just an usual Dired buffer.
Now it's time to speak about external programs, which could be called from inside Dired.
By default, the Emacs tries to open any file inside itself, if user presses RET on the file in the Dired buffer. But, it may be convenient to open some files with external programs, for example open videos with MPlayer or open an image with nSxiv. And the variable dired-open-extensions could help with that!
It is just a list of conses4, where the car is the file extension and the cdr is the program (from $PATH in my case) which should open all the files with such extension:
I think, this part of configuration is self-explanatory — it instructs Dired to open all images with sxiv command, and all videos with mplayer command.
Let's look to another way to launch specific programs for specific files in the Dired. I'm speaking about dired-guess-shell-alist-user variable and the ! or & hotkeys in the Dired buffer.
First thing, you (as a reader) should know, The ! hotkey in the Dired buffer will run entered command synchronously — so Emacs will lock itself, while the command is running. Sometimes, when the command (like gm convert) will exit very fast, it is OK and I could wait some small time while my files are transforming. But, some commands take a lot of time, so there the & is to the rescue. It will run the entered command asynchronously, so the Emacs will not be locked while the command is running. It is useful for e.g. converting some video files from WEBM to MP4, etc.
Of course, the entered command will be applied to the file under the cursor or to the marked files in the Dired buffer.
Second thing: after your press one of the before-mentioned keybindings, then the Dired will show you the prompt in the minibuffer, like this:
And there are two important keybindings:
Emacs Dired allows to define a lot of commands, depending on the filename extension, so for the images the M-n will show one set of predefined commands and for videos it will show another set of commands. The variable dired-guess-shell-alist-user allows to define these commands, like this:
This variable is just a list of lists, where the each inner list should contain two things:
So, in the example, provided above, there are:
Note, that where a various wildcard symbols in these command: *, ? and `?`. For example (and for my further explanation), lets assume what we have the next files in the catalog, opened in Dired:
The command like this: rm * — issued from Dired with the help of ! or & keys will be landed to the shell in the next form:
But, if you type the rm ? instead — then the Dired will issue three commands:
And for the mv ? `?`.test the next three commands will be issued:
How these tricks with *, ? and `?` could be used? The first one is self-explanatory — just use it, if need to apply some command to the all selected files.
The ? and `?` are more tricky. E.g., they could be used for image conversion via GraphicsMagick — when you don't want to lost the original filename. Here the `?` comes to the rescue — with back-quotes it is possible to add some text before or after the original filename, because both * and ? should be surrounded by spaces or Dired will not understand it5. So with e.g. gm convert ? `?`.png the next three commands will be executed by the Emacs:
And the next command won't do anything meaningful:
First, it will ask user about:
And if user answers y, then the next non-desired commands will be executed:
Let's delve6 into the photography topic much more.
Usually, my photos land to the some catalog, named with pattern: date of event-name of the event. And I'm using Dired to work with these photos, intensively:
With the help of before-mentioned dired-guess-shell-alist-user I'm first selecting some (usually 30-50%) photos for deletion. The command sxiv -o * | xargs rm -f allows me to mark some bad photos with m key, so their filenames will be sent to the input of rm -f, when I quit from nSxiv image viewer.
Another few tricks which I'm using to connect photography and Dired. First of all, there is a C-t t hotkey to display thumbnails for selected images right in the Dired buffer. This looks like this:
The same hotkey could be used to hide thumbnails for selected images. It is extremely useful when I want just copy specific images to the (e.g.) friend's USB-drive.
The next convenient hotkey is a C-t o. It displays a separate Image Dired buffer with thumbnails (and customizable information bar) for selected images and some useful hotkeys, to rotate pictures, open them in Gimp, etc. I didn't use it much enough, but it convenient to operate with photos like it happens with usual file browser within a fat DE; or to select the photo with necessary DPI and size.
The information bar is not the default — it is customizable and of course it was customized. The contents of this bar is generated in the image-dired-format-properties-string function and (in my case) I just override it with my function via advice-add:
The custom function evgandr/image-dired-enrich-properties looks like this:
This function just wraps around original function and add some string from evgandr/image-size function to the default contents of the bar: number of file in the list, file name and size. The default contents is taken from the original function (look at the original-fun parameter).
The evgandr/image-size function retrieves the information about given image file via gm indetify (it is a GraphicsMagick, since ImageMagick embraced LLM slop in the codebase7):
The general idea of all of these modifications is not mine — it was taken from the Extending image-dired blogpost.
Here just an one thing, which I'm using. Since all my Emacs configuration is byte-compiled, I'm using Dired to byte-compile changed .el files from my configuration. There are 3 simple steps:
Let's go to the last chapter — to the ELisp code with which I configure Dired. As I wrote before, I don't use use-package and use simple old ways to write my Emacs configuration with require, provide and so on.
With Dired I'm using the next few external packages:
Then, I'm disabling some minor modes from displaying them in the modeline with the help of delight:
Also, I added this line to enable dired-find-alternate-file9. It works in conjunction with dired-kill-when-opening-new-dired-buffer variable, described below and helps me to maintain amount of opened Dired buffer at low level:
Some custom keybindings is in use. First:
The default one is C-x d but I want to use C-x C-d since it is uniform with C-x C-f.
Next keybinding is to create something:
By default, the + could be used to create a new directory. But I want not to just create a new catalog, but also a new empty file with the same keybinding. So, the custom evgandr/dired-create-file-or-directory function helps here:
The idea of this function is not mine, but I lost the URL of blogpost where it was introduced :-(. The function works pretty simple: if path ends with / then user wants to create a new catalog and it will be created. Otherwise, the new empty file will be created.
Third, I defined some keybindings especially for dired-mode-map to simplify usage of some plugins:
To enable some helper minor modes there are some hooks are in use:
These minor modes enables some pretty things:
This section will be a looooong one :-). If code comment for some variable is self-explanatory — I will skip the unnecessary repeating.
If I marked some files and want to create an archive with the help of Z button, then the resulting TAR archive will be compressed with zstd:
The next Dired setting is a very convenient. I can specify a lot of non-existent catalogs in the path, where I want to copy file — and Dired will create all of these catalogs by itself:
Since I'm using GNU Stow to manage my dotfiles, these files in $HOME are just symlinks. And I'm too lazy to press ( every time when I need to see the destination of the symlink. So, I have this:
By default (IIRC) the Dired will open a new buffer for each opened subdirectory. So, when I'm browsing e.g. some Maven projects, I'll get a long list of unnecessary for me buffers. To avoid this, the variable dired-kill-when-opening-new-dired-buffer exists — it instructs Dired to kill the buffer with parent directory when I go to deeper in the file hierarchy. There is always a ^ button to go back.
The next two variables are necessary to instruct dired-rsync to use openrsync:
By default, the dired-subtree plugin uses a very strange set of colors — aka not matching with my Solarized Light theme — in the background face for files and catalogs in the subtree. So the next variable set to nil disables this behavior:
There are not so much customization I've made. Just changed colors of blue header with current path on the top of Dired buffer and color of directory — so they match with my Solarized Light color scheme:
E.g. I have a catalog with some JPEG and MP4 files — without consistency in filenames — and want to copy or move only some of the JPEG and some of the MP4 files, interactively selecting them.
Sadly, the Emacs Jabber using the same keybinding (C-x C-j) as a prefix key. So, if you are using Emacs Jabber, then no C-x C-j :-(, while you don't change the default keybinding.
Common Lisp/Reference/cons (Wikibooks)
See the 32.8 Shell Commands in Dired
Obviously, this blogpost and other posts in my blog written only by me and no LLM were involved }:->. The word above is just a smirk to the direction of people, who believe that:
Also, consider this: I'm Kenyan. I Don't Write Like ChatGPT. ChatGPT Writes Like Me.
Look at this commit in the ImageMagick repository, which was "co-authored" by Claude LLM. Or at this attempt to receive some policy about LLM usage in project from the main developers (unsuccessful).
Look at this commit, this commit and this PR.
See the output of F1 f dired-find-alternate-file RET.
Source: Hacker News — This article was automatically imported from the source. Read full article at original source →