emacstrampeshell

eshell TRAMP find remote file with relative path (or at least less than the full Tramp path)?


I love eshell's TRAMP integration. With it I can do cd /ssh:foo:/etc to ssh into a remote machine and visit its /etc/ directory. I can also do find-file motd to open this file in my local emacs. However, what if I need to use sudo to change the file? I know I can give the full path, like so:

find-file /sudo:foo:/etc/motd

but is there a way to open the file via TRAMPs sudo support, without having to type the full path?


Solution

  • I managed to came up with the following eshell alias that works for me:

    alias sff 'find-file "${pwd}/$1"(:s/ssh/sudo/)'
    

    It should be fairly obvious what it does. It prepends the working directory path, but with the string ssh replaced by sudo. Thus it only works for remote files accessed over ssh. I rarely edit files using sudo locally, so that's not a problem for me. However, we can make it work for local files too, at the cost of complexity:

    alias sff 'find-file "${pwd}/$1"(:s,^,/sudo::,:s,::/ssh:,:,)'
    

    That is, prepend /sudo:: (which is how to sudo for local files) and subsequently replace any ocurrence of ::/ssh: with :. (I would have just removed :/ssh:, but eshell's :s/// construct didn't accept an empty replacement.)