Category Archives: dired

Editing and managing files remotely

Emacs supports editing files remotely, as a nice alternative to opening a new emacs session on your remote machine and sending the window over X11. This feature is called tramp.

It works seamlessly – just open a file as usual with C-x C-f and then give the name of the remote file, including the host address – e.g.

C-x C-f /host.server.com:/home/bjm/myfile.org

Note the syntax, that the remote machine name is opened at the top level directory “/”.

You can then edit and save as normal.

It is useful to note that you can also do exactly the same thing to open remote directories in dired to browse, copy, rename and otherwise manage files on a remote machine.

Update

As commenter Nagora points out, if your username is different on the remote machine, you should specify it like

C-x C-f /user@host.server.com:/home/bjm/myfile.org

Dired: human-readable sizes and sort by size

We’ve looked a few times at dired, emacs’ powerful file browser. This time I want to look at file sizes in dired. By default, dired lists file sizes in bytes, but this is easy to change by customising the switches that dired uses when it calls your system’s ls command to generate the directory listing.

Add the following to your emacs config file to have file sizes given in “human-readable” format (i.e. in units of B, K, M, G as appropriate).

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dired                                                                  ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq dired-listing-switches "-alh")

We already saw that hitting s in a dired window changes the sorting between file name and modification date. It is also nice to sort by file size sometimes. To do this, we can use the prefix argument in front of the s command, so use C-u s in the dired buffer. This prompts us for the ls switches, which we can change from -alh to -alhS to sort by size. Xah Lee covers this in more detail in his extensive emacs pages.

Dired: redirect symbolic links

We have previously looked at using dired for managing files and switching dired into writeable mode for renaming multiple files. Here is a related tip to redirect symbolic links in dired.

Suppose you moved some data from one location to another and ended up with a bunch of broken symbolic links, you can easily edit those links to point to the new path. In the example below, I have links pointing to files in /old/path and I want the to point to files in /new/dir instead, so I do the following

  • Use C-x d to enter dired and choose the directory with the files in
  • Use C-x C-q to turn dired into editing mode. You can then edit the file names by hand or
  • Use multiple cursors to edit the links in one go (you could also use e.g. M-% to do a query-replace). This changes the path of the links.
  • Use C-c C-c so apply the changes, or C-c C-k to cancel

This is illustrated in the following animation.

dired-links.gif

This is just great – it feels like magic every time!

Dired: rename multiple files

Following on from the previous dired tips we can do much more in dired. A very powerful feature is that you can switch the dired buffer which lists the files into editable mode. Then you can directly edit file names, links and so on. Here we will look at using dired to rename multiple files in a nice visual way.

Now we will use dired to rename multiple files at the same time. Suppose in some directory we have a bunch of files called test_foo_01.dat, test_foo_02.dat, etc and we wanted to rename them to replace foo with bar, then we would do the following:

  • use C-x d to enter dired and choose the directory with the files in
  • use C-x C-q to turn dired into editing mode. You can then edit the file names by hand or
  • use M-% to replace foo with bar in the dired buffer. This will change the file names
  • use C-c C-c so apply the changes, or C-c ESC to cancel

The animation below shows this example. Of course you can also manually change the name of the file, as I do for the last file on the list.

dired-rename.gif

Dired: marking, copying, moving and deleting files

Following from our introduction to dired, here is an example of how to use dired for simple file operations. You can do much more with dired, and we will return to it in the future.

Open dired for a directory containing files you want to work with. Then use C, R, or D when the cursor is on the line of a file to copy, rename/move or delete the file, respectively. This can also be done for multiple files by marking them.

You can mark files in dired by pressing m on the line of a file, and unmark it by pressing u. You can mark several files individually using m or mark all files whose names match a search string by using % m string <RET>. Use U to unmark all files.

The strings used for matching are regular expressions, so are very powerful. For example:

  • % m test <RET> will mark all files with names containing the string “test” (equivalent to *test* on the command line.
  • % m ^test <RET> will mark all files with names starting with the string “test” (equivalent to test* on the command line).
  • % m txt$ <RET> will mark all files with names ending with the string “txt” (equivalent to *txt on the command line).
  • % m ^test.*org$ <RET> will mark all files with names starting with the string “test” and ending with “org” (equivalent to test*org on the command line).
  • % m [kxj] <RET> will mark all files with names containing the letters k, x, or j
  • % m [kxj] <RET> will mark all files with names containing the letters k, x, or j
  • % m [6-9] <RET> will mark all files with names containing the digits 6,7,8,9

Read more about emacs’ regular expressions here.

Once you have marked files, use C, R, or D as before to copy, move or delete them.

You can invert the marking of files using * t, and mark all files (or directories) using * s.

You can hide marked files from the current dired view using k. Note that these are not deleted, use g to see them again.

So, supposing you wanted to delete a bunch of files, but wanted to see them first to visually check you are deleting the right ones, you might do the following

  1. mark the files manually, or using a regular expression
  2. * t invert the selection
  3. k hide the selected files so now you see the ones you originally selected for deletion so you can check you got the right ones
  4. * s to select those visible files
  5. D to delete them

This animation illustrates me going through these steps when I have selected all files ending “txt”.

dired-files.gif

Dired: Emacs as a file browser

Emacs has a powerful file browser built in. I’ll introduce some of the things it can do here, and come back to this topic in future tips.

Use C-x d to enter dired mode (emacs’ file browser), and give the path of a directory to open.

To move the cursor around you can use the arrow keys. Of course you can use emacs’ usual tricks for moving around the buffer – e.g. C-s to search for a file name, or to jump to a particular file in dired, use j and then enter the name of the file or directory, and the cursor will jump to that point. You can also use > and < to move to the next or previous sub directory. Hitting ^ moves you up a level in the directory tree.

To open a file, move the cursor to the line containing a file and hit enter.

To create a new subdirectory, hit +, and to create a new file in the directory, just use C-x C-f as normal.

Hitting s will change the sorting of the directory listing between name and date.

g refreshes the view.