I'm in a situation where I have to ssh into a machine and do all of my coding on there. For a while I would ssh in and then use tmux and emacs -nw mode. Recently, I discovered the term command. Now instead of using tmux, I open up normal Emacs on my desktop and use term to ssh in.

Once I'm in, I run emacs -nw from within term within Emacs on my desktop. This works great, and I have all of my pretty colors. The only problem is, I can't figure out how to cleanly close the emacs -nw within the Emacs on my desktop. C-x C-c does not work, as expected.

How can I close the innermost Emacs, and get back to term? Is there a better way to edit files via ssh?


There are a few answers here.

Quitting Emacs without C-x C-c

First, you can quit emacs by running the command that C-x C-c is bound to: M-x save-buffers-kill-terminal.

Alternatives to recursive Emacs

Emacs is able to edit files on remote servers, so you don't even need to ssh into the remote server, and you won't need an Emacs inside a term inside your original Emacs. This functionality is called TRAMP, an acronym for "Transparent Remote (file) Access, Multiple Protocol". To edit a file on a remote server, run C-x C-f like normal, but instead of finding the file normally, use a special format for the file path: /user@host:path/to.file

So if you want to edit the file located at /etc/hosts on the remote server my-remote-machine, and you need to log in as gragas, find the file this way:

C-x C-f /gragas@my-remote-machine:/etc/hosts

It will prompt you for a password if one is required. It'll transparently save the file across the network, so absent a bit longer time it takes to save, you won't even notice it's a remote file.

|improve this answer|||||
kill $(ps -o ppid= -p $$)

Which is a bit more general: when run from terminal started from Emacs it will look for the parent process of the current process ($$ is shell's way to get the process id of the current process). And kill it. You could put this in your bash profile as a function or alias to make it easier to type, eg:

ekill() { kill $(ps -o ppid= -p $$) }

In your ~/.bash_profile (on remote machine), and then you'd only need to type ekill once you want to kill Emacs from inside term run by Emacs.

|improve this answer|||||

Your Answer
