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 conses, 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:
(setopt dired-open-extensions '(("png" . "sxiv") ("PNG" . "sxiv") ("jpeg" . "sxiv") ("jpg" . "sxiv") ("JPEG" . "sxiv") ("JPG" . "sxiv") ("gif" . "sxiv") ("GIF" . "sxiv") ("webp" . "sxiv") ("WEBP" . "sxiv") ("avi" . "mplayer") ("m4a" . "mplayer") ("mkv" . "mplayer") ("mp3" . "mplayer") ("flac" . "mplayer") ("mp4" . "mplayer") ("webm" . "mplayer") ("eps" . "gv")))
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:
M-pshows you the commands which you entered before in this prompt.M-nshows the predefined commands.
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:
dired-guess-shell-alist-user '(("\\.\\([Jj][Pp][Ee]?[Gg]\\|[Pp][Nn][Gg]\\|[Tt][Ii][Ff]\\{2\\}\\|[Ww][Ee][Bb][Pp]\\)$" "sxiv -t * " "sxiv -o * | xargs rm -f" "sxiv -o * | xargs dragon" "sxiv -o * | xargs gimp" "gm convert ? -resize 1080x1080^ resized_`?`" "gm convert ? -resize 2560x2560^ resized_`?`" "pngquant --speed 1 --strip --skip-if-larger --force * " "jpegoptim -m 80 --auto-mode -s * ") ("\\.ORF$" "rawtherapee * ") ("\\.[Gg][Pp][Xx]$" "qmapshack * "))
This variable is just a list of lists, where the each inner list should contain two things:
- Regular expression which selects the file extension. For this
extension (or these extensions) the next written command(s) will be
displayed after pressing
M-n. - The next items of inner list is just a strings with commands, which should be launched for the files with extensions from item #1.
So, in the example, provided above, there are:
- Some commands:
sxiv,gm,pngquantandjpegoptim— were assigned to the common extensions for images, like: JPG, PNG, TIFF and WEBP images. - Command to launch RawTherapee for
*.orffiles. - Command to launch QMapShack for
*.gpxfiles.
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:
rm 01.jpg 02.jpg 03.jpg
But, if you type the rm ? instead — then the Dired will issue three
commands:
rm 01.jpg rm 02.jpg rm 03.jpg
And for the mv ? `?`.test the next three commands will be issued:
mv 01.jpg 01.jpg.test mv 02.jpg 02.jpg.test mv 03.jpg 03.jpg.test
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 it. So with e.g. gm convert ? `?`.png the
next three commands will be executed by the Emacs:
gm convert 01.jpg 01.jpg.png gm convert 02.jpg 02.jpg.png gm convert 03.jpg 03.jpg.png
And the next command won't do anything meaningful:
gm convert ? ?.png
First, it will ask user about:
1 occurrence of ‘?’ will not be substituted. Proceed? (y, n, ?)
And if user answers y, then the next non-desired commands will be
executed:
gm convert 01.jpg ?.png gm convert 02.jpg ?.png gm convert 03.jpg ?.png