This is still the header! Main site

Emacs as tmux

2024/08/24

You surely know that tools like tmux and screen exist.

(If you do not know that they exist, I am happy to announce that they do! They let you use a single terminal as multiple terminals, essentially functioning as a little window manager. They also come with the ability of detaching sessions while you disconnect, allowing you to run long-running processes on a remote server while you pack up your laptop and put it into a bag.)

We do need to address this on our long-running quest to show that terminals are not that superior. Specifically, there was already a mention of Tramp, the Emacs library that lets you edit files and run commands remotely via SSH, for example, making it fairly transparent. It doesn't quite solve the "compilation while laptop is in bag" problem especially well.

Meanwhile, if you just want an editor on the remote server, you can use Emacs perfectly well the way you would use Vim, by launching it from the terminal (maybe in an ssh session) to edit a single file and exiting once you're done (with likely much worse startup times though). You could technically even do this within tmux, hoping that all the keybindings do not end up being mixed up.

Fortunately, there is not much need to do so though.

Daemon mode

As it happens, Emacs also comes with a library that makes it a surprisingly good tmux replacement.

If your server happens to have Emacs installed, you can just...

 ~$ emacs --daemon
Starting Emacs daemon.
 ~$
          

This starts an Emacs in the background; then you can get an actual user interface by...

~$ emacsclient -t
          

... which will give you a terminal mode Emacs, which is pretty close in functionality to the GUI version. (Another use of emacsclient is to open up a file in an already existing Emacs instance... in our case though: is there anything interesting outside of Emacs?)

Once you're done, you can just either "quit" by C-x C-c, or close the current frame (C-x 5 0)... but then if you just close the SSH window (or hop off the network), the same thing will happen: your session will still be waiting for you the next time you connect, just like with tmux.

You can even start your SSH sessions with emacsclient right on them by


~ $ ssh -i our_server.name emacsclient -t
          

... in case you're not doing much of anything else outside of these sessions anyway. (-i is there to convince the SSH session that it is in fact still an interactive one, despite the presence of a batch-processing-looking command; it changes a bit how the terminal device is handled, giving you an Actual Interactive UI instead of a stream of bytes.)