7

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?

2

2 Answers 2

15

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.

2
  • 1
    As a complete beginner, I was confused at what C meant. Please add to your description that C is ctrl on an ordinary PC. So it is ctrl+x, pause, and ctrl+c. The esc button can break this sequence, so don't use it while quitting.
    – t7e
    Commented Sep 17, 2020 at 2:36
  • @t7e The first thing any beginner in emacs should do is run the built-in tutorial. Press and hold Contral and then press h, followed by pressing t (for tutorial). On the very first page it explains The C-x C-x style keystroke labels. This is all very commonly used in emacs. Commented May 30, 2022 at 21:21
1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.