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 /host.server.com:/home/bjm/myfile.org

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.

Update

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

C-x C-f /user@host.server.com:/home/bjm/myfile.org
  • Nagora

    Worth mentioning that if your login on the remote machine is a different name from local, you need to supply it in the filename:

    C-x C-f /nagora@host.server.com:/home/bjm/myfile.org

    • Good point – I’ve updated the post. Thanks!

  • Patterns to remember (user and port are optional):
    /ssh:user@host#port:/path/to/file
    /sudo::/etc/root_owned_file

    Don’t forget that you can use C-x C-v to re-open for writing a root-owned file by adding “/sudo::” to the beginning of the path.

  • Anyone know how to get desktop.el to remember tramp buffers? Or is there a newer, shinier session saver I should be using? I use scripts and ssh keys to allow me to edit locally via tramp remote files by running “e file” on the remote machine. It sshes to my laptop and then uses emacsclient to open the remote file using tramp.

  • You can also use pipe to use sudo to open a file on a remote system:

    /ssh:user@host|sudo:root@host:/path/to/file

    The only thing to remember is that ‘host’ has to be the same for both sides of the pipe.

    I don’t know if it’s a package or part of Tramp, but my Emacs also reads in the hosts I’ve defined in ~/.ssh/config, so I can enter

    /ssh:host1|sudo:root@host1:/path/to/file

    Where ‘host1’ is a host defined in ~/.ssh/config

    • Phil

      Absolutely right about HOST needing to be explicit for the sudo: part (see StackOverflow for more details), but FYI root@ is not required, as root is the default username for the su: and sudo: methods.

  • Pingback: 2015-10-26 Emacs News - sacha chua :: living an awesome life()