If your a SSH, or command line user and haven’t used screen before be sure to look into it. Ever been transferring a file or running a program in a command line interface and accidentally close it or disconnect? The result will be a termination of the running application and possible loss of work. When Microsoft released Windows 3.1, the world was wowed with the fact that you can just open another window and run multiple applications at the same time. Well, you can easily do this on the command line as well. By launching each application or command in a screen session, you can easily detach the screen and put it away for later.
Interested? Of course you are. It’s always best to start learning by checking out the man page for screen.
Installing screen is easy, if your on a system like Ubuntu or Debian run the command sudo apt-get install screen or find it in your favorite application package installer. If your on OS X you may already have it installed.
Once screen is installed, start up a new screen session by just typing screen in the command line.
You should notice a fresh window appear possibly with a message from the application telling you what is going on. For leaning sake, lets run a simple command to show how this system works. In the new screen window that has started type ls to simply list the current directory contents. Now with that command done and the output visible, hit CMD+A and then D. You should be brought back to the original command line you had open and ran screen from. You should also see that the screen [detached] - what has happened is that your screen session (aka. the command line you wrote ls into) has been put into the background and is waiting for you to reattach it. This is where the power comes in, but you need to be able to reopen that command line again to make it actually useful. So just type screen -r and hit enter. You should be brought back into your screen session that your ran ls from. Behold the POWER! Type exit in the command line to close out this screen session for good now.
The next step in screen mastery is setting up a configuration to your liking. By default screen reads it’s configuration from a .screenrc file in your home directory. So lets create one now, we will use my personal config as a starter – open up the text editor of your choice and write the following to .screenrc in your home directory (source is also on Gist).
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
Close the file and fire up a new screen session by typing screen again. You should notice some fancy footer on the bottom of the window now. Most likely in the centre it says (0*$bash), this is the screen label for this current screen. Try hitting CTRL+A and then C. This should open up another command line inside the same screen session or effectly (1*$bash). Now you can multitask like a mad-man and easily switch back and fourth between multiple command lines in the same screen session that can be closed and re-open at any time. To shuffle to the next command line in the list hit CTRL+A and then N. Do it again and back you go to the other window.
By now you should have a understanding of the power of this application. I use it everyday and love showing command line new comers it as they usually have to pick their jaw off the floor when they realize what it will do for them. Be sure to read into the man pages for a ton more configurations to set to your liking!