Cool Emacs Things: Edit Files Over SSH
This is the first installment of an indefinite series calling (in lieu of a better name) “Cool Emacs Things,” little things about Emacs that makes it a very handy editor.
Today’s entry: Editing files over SSH.
This was something I discovered recently by accident. I run a private email server and the IMAP portion of the server is handled by Dovecot. Dovecot is great but by default the configuration is spread across dozens of files, and, while trying to tediously edit multiple files over SSH in Vim, I thought, “Wouldn’t it be nice if I could do this with Emacs?” Well guess what!
C-x C-f
(or SPC f f
in Doom, which is what I use) will open a “find file” dialog, obviously letting you open files and peruse your file system. However you don’t have to just browse for files. I use Ivy, so your experience may vary, but when I type /ssh: TAB
I’m shown a list of my SSH connections. /ssh:ryan@email-server:/
then will cause Emacs to presumably run some commands using SSH (or a related program) and treat it as a directory on your own computer. You can then used Dired (like an interactive ls
, if you aren’t an Emacs user!) to explore your remote files and folders and even open files and edit them.
The Emacs package that handles this is called TRAMP, and allows you to do a lot more than just what I’m talking about in this post.
There is one issue with this, however: my Dovecot configuration requires root permissions to edit them. TRAMP can handle this too with something called multiple hops. The idea is that I’ll first connect to the host via SSH and then run any further commands using doas
(the OpenBSD equivalent of sudo
more or less, as my email server is hosted on OpenBSD).
The final command to do all this looks like:
/ssh:ryan@email-server|doas:ryan@email-server:/
Now I can browse and edit my email server with elevated permissions. Cool!