How to use Emacs like GNU Screen

Published on 2011-06-09
Tagged: emacs

View All Posts

I tried out GNU Screen a couple days ago. It's a pretty neat tool. Basically, it's a command line program which lets you create a session in your terminal. You can detach from the session at any time, and reattach to it later, even from another terminal.

This is useful in a lot of situations. For instance, maybe you want to kick off a long build at work before you go home. It's really annoying to come in first thing in the morning and find that the build failed an hour after you left. With Screen you can detach from your session at work without closing the shell. When you get home, you can ssh into your machine and resume the session to see if everything's copacetic.

Screen has a lot of features. It supports multiple windows within each session, so you can have multiple shells. You can rename windows, and switch between them. You can split the screen to show multiple windows at once.

Emacs also has these features though. Personally, I spend most of my time in Emacs with lots of shells (started with M-x shell). I'd rather not learn a new set of keyboard commands for Screen if I can get the same session functionality from Emacs. Fortunately, there's no need.

Recent versions of Emacs have client-server functionality built in. The server maintains all of the open buffers and child processes (like shells). The client basically functions as a frame to display these buffers. To make an already running instance of Emacs into a server, run M-x server-start. You can also add the following line to your .emacs file if you always want to launch the server:


This is fine as long as you don't quit Emacs. If you want to start the server in the background, run this command:

emacs --daemon

To connect to the server within a terminal:

emacsclient -t

This command is analogous to screen -r. It will create a new frame within your terminal. You'll have access to any buffers open in the server. If you're in a graphical environment and you want to open a new graphical frame outside of the terminal (most people would call this a "window"):

emacsclient -c

When you're done, you can do C-x 5 0 to close the frame. You can also do C-x C-c. Normally this would close all of your buffers and quit Emacs. However, if you started the server as a daemon, the server will continue running, and you'll be able to open a new frame later.