Is it possible to edit a symlink with a text editor?

It's not possible directly, as others have already pointed out, but of course you can write a script for it. Here's one I came up with when I had to change lots of symlinks

#! /bin/bash

tmp=$(mktemp)
trap "rm $tmp" EXIT

while [ ! -z "$1" ]; do
    filename="$1"; shift
    if [ ! -h "$filename" ]; then
        echo "Not a symlink: $filename";
        continue
    fi
    stat -c "%N" "$filename" >> $tmp
done
emacs $tmp

while read filename linkname; do
    ln -sf "$linkname" "$filename"
done < <(sed "s/'\(.*\)' -> '\(.*\)'/\1 \2/" $tmp)

It worked for me, but it's certainly not perfect, so use at your own risk...


It's possible in principle, but the editor would need to specifically support it, since reading the destination of a symlink requires a special system call: readlink().

You're unlikely to find any editors that actually do this, since it's not very useful, and conflicts with what most users want the editor to do when asked to open a symlink: open the file that it points to.


As per the Storage of symbolic links section in Wikipedia's article Symbolic Links, the symlinks are stored in an inode. This inode is a data structure containing several information about the file in question - as per this thread, the touch command can be used to change some of its values. So, it may not be possible to modify it by using a text editor, due to the problems that @Wyzard mentioned, but it might be modifiable by using some other command-line tools like touch.

I hope this helps!


Yes, in Emacs this is possible in dired-mode, specifically wdired (writable dired) mode.

Note, dired and wdired both are built-in packages.

Here's an example...

wdired editing a symlink

(BTW: I'm using Smex to give Emacs M-x command search & execute a more ergonomic UI + fuzzy matching)