How to Edit a Symlink with a Text Editor

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

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)

Is there a way to edit a symlink without deleting it first?

You could create the new link with a different name, then move it to replace the old link.

ln -s /location/to/link linkname

Later

ln -s /location/to/link2 newlink
mv newlink linkname

If newlink and linkname are on the same physical device the mv should be atomic.

Removing and adding the same symlink in perforce

To modify a file in a single revision, use p4 edit instead of p4 add + p4 delete.

Can you change what a symlink points to after it is created?

AFAIK, no, you can't. You have to remove it and recreate it. Actually, you can overwrite a symlink and thus update the pathname referenced by it:

$ ln -s .bashrc test
$ ls -al test
lrwxrwxrwx 1 pascal pascal 7 2009-09-23 17:12 test -> .bashrc
$ ln -s .profile test
ln: creating symbolic link `test': File exists
$ ln -s -f .profile test
$ ls -al test
lrwxrwxrwx 1 pascal pascal 8 2009-09-23 17:12 test -> .profile

EDIT: As the OP pointed out in a comment, using the --force option will make ln perform a system call to unlink() before symlink(). Below, the output of strace on my linux box proving it:

$ strace -o /tmp/output.txt ln -s -f .bash_aliases test
$ grep -C3 ^unlink /tmp/output.txt
lstat64("test", {st_mode=S_IFLNK|0777, st_size=7, ...}) = 0
stat64(".bash_aliases", {st_mode=S_IFREG|0644, st_size=2043, ...}) = 0
symlink(".bash_aliases", "test") = -1 EEXIST (File exists)
unlink("test") = 0
symlink(".bash_aliases", "test") = 0
close(0) = 0
close(1) = 0

So I guess the final answer is "no".

EDIT: The following is copied from Arto Bendiken's answer over on unix.stackexchange.com, circa 2016.

This can indeed be done atomically with rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states:

If newpath refers to a symbolic link the link will be overwritten.

In the shell, you would do this with mv -T as follows:

$ mkdir a b
$ ln -s a z
$ ln -s b z.new
$ mv -T z.new z

You can strace that last command to make sure it is indeed using rename(2) under the hood:

$ strace mv -T z.new z
lstat64("z.new", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
lstat64("z", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
rename("z.new", "z") = 0

Note that in the above, both mv -T and strace are Linux-specific.

On FreeBSD, use mv -h alternately.

Editor's note: This is how Capistrano has done it for years now, ever since ~2.15. See this pull request.

vscode edit symlinks path

No, it looks like that isn't possible at the moment. The short answer is no, because it's not currently possible without customizations that (as far as I'm aware) don't exist yet and may be non-trivial to make.

The long answer is, according to this similar question posted earlier (among other sources):

  1. The simplest way to edit a symink for you (assuming a Unix-like environment) would be to right-click it in the explorer, then select "Open in Integrated Terminal" and use something like ln -sfn source.ext link.ext within the terminal (-f overwrites & -n allows folders).
  2. If you're on Windows 10 (possibly 11 as well?) or earlier, you can use this shell extension to add a right-click option to edit symlink properties in a 'text editor' of sorts, as mentioned here.
  3. Something similar to the above extension probably exists or would be possible to make on Linux or even VS Code specifically, but would probably take some tinkering.
  4. Emacs' writable dired mode (wdired), shown in one of the answers linked to the previous question, seems to be the closest thing to what you're asking. Not possible in VS Code unless there's a custom extension or setup somewhere that I missed.

Git symbolic links in Windows

You can find the symlinks by looking for files that have a mode of 120000, possibly with this command:

git ls-files -s | awk '/120000/{print $4}'

Once you replace the links, I would recommend marking them as unchanged with git update-index --assume-unchanged, rather than listing them in .git/info/exclude.

Vim edit a symlink

You can insert any Vimscript expression into the command-line via <C-r> and the expression register =:

:e <C-r>=resolve(expand("~/.vimrc"))<CR><CR>

Alternatively, for files, there's the rather obscure backtick expansion of Vimscript:

:e `=resolve(expand("~/.vimrc"))`<CR>

windows symbolic link target

As Harry Johnston said dir command shows the target of symbolic links

2014/07/31  11:22    <DIR>          libs
2014/08/01 13:53 4,997 mobile.iml
2014/07/31 11:22 689 proguard-rules.pro
2014/09/28 10:54 <JUNCTION> res [\??\C:\Users\_____\mobile\src\main\res]


Related Topics



Leave a reply



Submit