Symlink Editing with dired and iedit

I recently used a little trick to manipulate symlinks from within emacs using dired and iedit, which may come in handy in some situations, so I thought I'd share it here.

I had a directory containing all sorts of binaries, and another directory which was in the $PATH, and which had symlinks to many of those binaries. Let's say it looks like this:

lrwxr-xr-x     1 root   root        71 Jul 16 23:49 executable_1 -> /some/bin/directory/executable_1
lrwxr-xr-x     1 root   root        71 Jul 16 23:49 executable_2 -> /some/bin/directory/executable_2
lrwxr-xr-x     1 root   root        71 Jul 16 23:49 executable_3 -> /some/bin/directory/executable_3

Now let's say you want to rename /some/bin/directory to /my/other/directory, but maintain the above symlinks (I mentioned only three here, but in my case there were a few dozens of them).

Luckily emacs is awesome, so it lets you do that in a neat way.

  1. First, open the symlink directory in emacs (in dired mode - by pressing C-d while in the minibuffer)
  2. Enter read/write mode, enabling you to edit the directory contents (C-x C-q)
  3. Select the portion of the path that is about to change in the first file (i.e. marking the string "/some/bin", since we want to change it to "/my/other")
  4. With the string selected, enter iedit-mode. I bound it to C-x i, but you might have a different binding. If you don't have iedit-mode, I strongly suggest that you get it here.
  5. Delete the selected string, and replace it with "/my/other". All occurrences of "/some/bin/" should change as you type.
  6. Save the changes to the directory (C-x C-s)
  7. Et Voilla! you're done!

You can apply this trick on many batch modifications in dired that can be based on substring manipulations, this is only one example. You can also use Magnar Sveen's awesome multiple-cursors.el for the concurrent editing of the replaced paths.


blog comments powered by Disqus