Category Archives: dired

Double dired with sunrise commander

I’ve written several times about managing files in emacs with dired. However, if I am copying or moving files between two directories, I have found that I like a two-paned file browser showing the two directories side by side. The package sunrise commander provides this very nicely, giving a great interface for managing files and even copying files between remote and local directories.

It is a tiny bit fiddly to install as it is not in one of the standard package archives. I would suggest evaluating the following code snippet to add the required repository:

(add-to-list 'package-archives '("SC" . "") t)

You can then install sunrise-commander using M-x package-list-packages as normal. The repository will be forgotten next time you restart emacs, which is fine as sunrise-commander is stable and rarely updated, and the repository can be slow to respond sometimes, which can affect the package listing.

While you are at it, also install sunrise-x-buttons and sunrise-x-modeline from the same place.

Once you have done this, use M-x sunrise to start sunrise-commander. This will open a two-paned dired style file browser as illustrated in this screen shot:


The top left and right panes are two different directory listings, each of which behave very much like dired. You can switch between them with TAB, and if you try to copy or move files, it will default to copying them to the opposite pane, which is just what you want.

This works particularly well when using emacs to manage remote files, indeed in the screenshot above, the left pane is a directory on a remote machine, so it is easy to copy files back and forth between the local and remote machines.

The bottom panel is a set of buttons to perform common tasks (provided by the sunrise-x-buttons extension); I don’t use the buttons but I find them a useful reminder of the keys for those tasks.

The sunrise-x-modeline extension updates the modeline of each directory pane to show a clickable path to the current directory.

There are several other extensions available, as described on the wiki page, and there is a second useful wiki page of tips for sunrise commander.

I have a couple of tweaks that I make:

;; sunrise commander                                                      ;;
(require 'sunrise-commander)
;; disable mouse
(setq sr-cursor-follows-mouse nil)
(define-key sr-mode-map [mouse-1] nil)
(define-key sr-mode-map [mouse-movement] nil)

;;tweak faces for paths
(set-face-attribute 'sr-active-path-face nil
                    :background "black")
(set-face-attribute 'sr-passive-path-face nil
                    :background "black")

;;advise sunrise to save frame arrangement
;;requires frame-cmds package
(defun bjm-sc-save-frame ()
  "Save frame configuration and then maximise frame for sunrise commander."
(advice-add 'sunrise :before #'bjm-sc-save-frame)

(defun bjm-sc-restore-frame ()
  "Restore frame configuration saved prior to launching sunrise commander."
(advice-add 'sr-quit :after #'bjm-sc-restore-frame)

This code disables mouse interaction, and changes the background colour of the directory paths.

More interestingly, I want sunrise commander to run full screen, but then I want my original frame size back when I finish with it. To do this I wrote two simple functions above, that use the frame-cmds library mentioned recently. The first saves the current frame configuration and then maximises the current frame – sunrise commander is advised to call this function when it starts. The second restores the saved frame configuration, and sunrise commander is advised to call this when it quits.

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 /

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.


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

C-x C-f /

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.


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: 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: 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.