Emacs Tramp

So Emacs has a ridiculously useful feature built-in[1] called TRAMP. TRAMP allows Emacs to edit a file, no matter where the file is located physically. It also does some deep elisp magic to make it so Emacs will behave as if it were editing the file locally. This means that if you create a shell buffer in your emacs session, it'll actually open the shell in the remote environment, not locally.

Other Emacs features will also work as though they were running on the same host. For instance if you invoke M-x compile, not only will the compile run on the remote host, it'll use the remote host's compiler.

TRAMP can connect to the remote host over a number of different protocols. It can connect over:

Just to name a few.

A common annoyance

So stop me if you've ever done this. You open a config file in your favorite $EDITOR and start editing. After making a few changes you go to save and you get an error. You don't have permission to edit that file as it's owned by root (or you otherwise just don't have write permissions.)

In any other editor, you'd be forced to quit without saving and then re-invoke your editor with sudo. In Emacs however, you can use TRAMP to reopen the file with sudo (assuming you have sudo rights on the machine.)[2]

C-x C-f RET /sudo::/path/to/file

will prompt you for your sudo password and open the file with the correct permissions.

Other user switching methods

TRAMP also supports other user switching methods besides sudo (although that's the most common.) You can use doas (BSD) or simply su to switch users.

If you want to switch to someone besides root, the syntax is:

C-x C-f /sudo:user@:/path/to/file

full documentation for various protocols can be found in the TRAMP manual.

TRAMP Manual

you can also view the manual within emacs by typing:

M-: (info "TRAMP") RET

multihop editing

Another very useful feature of TRAMP is multi-hop editing. TRAMP can in fact jump through any number of boxes/protocols to get to the file that you want to edit. to do so you use the normal syntax for making a TRAMP call but when you get to the file path part you insert a | character. For example:

C-x C-f /ssh:user@jump|ssh:user@devbox:/path/to/file RET

This is super useful if you're working in a cloud environment that utilizes ssh jump boxes. You can easily jump around to the various boxes in the environment, edit configurations, start subprocesses, etc. without needing to leave the comfort of your home emacs config (or *gasp* use vim 😏)

Combining multiple protocols in multihop editing

TRAMP also allows you to use various different protocols when doing multihops. To re-use the previous example, this:

C-x C-f /ssh:user@jump|ssh:user@devbox|sudo::/path/to/file: RET

is perfectly valid and would open the file in question as root.

Non-standard TRAMP methods

In addition to the methods already mentioned and a few others I've left out, TRAMP, or some extensions of it, have support for doing crazy things like using kubectl to access a Kubernetes pod thats currently running, attaching to a docker container, or even directly using vagrant to connect to a VM. I havent personally used these methods so I won't discuss them at length, I just thought I'd mention it in case it sounded useful to you.

You can find such packages on GNU Elpa, the emacs package repository. You can install them by running:

M-x package-install RET <package-name> RET

If you want to browse the available packages you can do so by typing:

M-x package-list-packages RET

Footnotes

[1] - Technically, it's a separately developed plugin but it's been shipping with Emacs for a long time.

[2] - If you're unfamiliar with this notation, see the explanation of it in the emacs manual:

Emacs User input notation