GNU Screen
GNU Screen helps you get most out of your desktop's real estate, in cases where you need to work on more than one terminal simultaneously. Using screen, you can have as many processes as you need, such as editors, web browsers, shells etc. all within a single terminal window. Every desktop system allows you to open multiple terminals in different windows, and most terminal programs let you run multiple sessions at once using tabs, but GNU Screen is often easier to manage and less confusing when you have many sessions. Additionally, GNU Screen offers a copy-paste mechanism to transfer pieces of text easily within the multiplicity of sessions handled by it.
Start by typing screen in your command prompt.
$ screen
You will get a welcoming message and some versioning information.
If you press return you will get a shell prompt, just like before you invoked Screen. You are now running within a single session of Screen. In order to create a second session with its own shell, press CTRL-A followed by c (release the CTRL key before pressing the c). You will now go to a new shell.
If you want to run just one command in a new screen and then close the screen, specify the command as an argument:
$ screen irssi
Switching Sessions
To switch to the previous shell, enter CTRL-A p. To go forward again, enter CTRL-A n. You can see a list of all the sessions you have created using CTRL-A ". This presents a scroll-down menu listing all open sessions (very helpful when you have created many sessions!). If you want to save even the half second it takes to scroll down the menu, while being able to see a list of available sessions and instantly jump to another session, enter CTRL-A w. This keeps you in your current window, but adds, at the bottom of the screen, a list of sessions with a different number for each. Then enter CTRl-A session_number to jump to the session of your choice.
Suppose you created a few sessions under GNU screen. In one session you have a Vim editor open, a couple more sessions you use for logging in to different remote servers, another session you use to run FTP, so forth. By default, CTRL-A " shows the program you used to start each session. Normally you started it with the shell, so CTRL-A " shows "bash" for each session (or whatever your shell is). This isn't very helpful if you want to quickly find the session that's running Vim or FTP.
It turns out that customizing the display is easy. While you are in a session--say, editing in vim--press CTRL-A A and you will get a line at the bottom of your window with the name of your session, which you can edit to your liking.
Copy & Paste
Now say you are creating a shell script within Vim, and you want to test a single line from it containing a few shell commands to see how they would actually behave when run in a shell. That means you want to copy the text from the vim and paste it in a Bash shell prompt. With GNU screen, you can do this easily.
- While in your vim session, press CTRL-A [ to put you in screen copying mode.
- Navigate through the text anywhere in your window using Vim positioning commands. After you get to the line of interest, press the Space key.
- Select specific lines to be copied by simply moving though the text using Vim's positioning commands until you reach the end of what you want to copy (text will be highlighted as you proceed with your selection).
- Once you finish selecting text, press the Space key again to copy the text to the clipboard.
- Using the session switching commands described earlier, navigate to a session containing a Bash shell and press CTRL-A ]. This will paste the selected text, which in this example are some shell commands, right into the Bash command line.
In this way you can very easily test command, then go back to the Vim session to edit and save your shell script.
Splitting The Screen
Besides multiple full-screen windows, screen can also allow two or more programs to share the screen at once.
Use CTRL-A S (capital S) to divide your screen into two parts. You're original session will be at the top and a new blank session at the bottom. (Be careful you don't accidentally press CTRL-S. This can lock up your terminal. If you do accidentally hit CTRL-S, you can unlock the terminal with CTRL-Q.) By default, there will be no program running in this new region, but you can start one by using CTRL-Tab to move to that region and then typing CTRL-A c.
Each region acts as an independent session, and you can switch between sessions just as you as in fullscreen mode.
You can remove the current region by using CTRL-A X. This won't destroy the session or what's running in it. It will just turn the other session back into a full-sized window.
Detaching A Session
One of Screen's most powerful features is the ability to halt and restore sessions. Say you're doing something really interesting on your computer, but you have to leave and go to work. Now say that while away you want to access what you were working on. If both machines are accessible through the Internet, you can do that with Screen.
Type CTRL-A d in your screen session. You will return to your original terminal and the screen will exit, printing [detached]. Now if you execute ps, you'll find that the screen is still running in the background. Get a list of all running session by passing screen the -list option.
$ screen -list
There is a screen on:
12056.pts-0.hostname
(Detached)
1 Socket in
/var/run/screen/S-user_name.
$
You can reconnect to this running session by entering:
$ screen -r 12056.pts-0.hostname
Or you can just use:
$ screen -R
This will reconnect to the first session it finds.
Now all you have to do is log into your home machine from any remote machine and you will be able see whatever you were working on, exactly as you left it. Use the same procedure to put a download into the background with wget, or ftp. A detached session will persist even if you log out off the machine.
Quitting Screen
If you have only a few programs open, you can exit screen simply by quitting them all. However, if you have many different application and windows open, you can exit them all by typing CTRL-A \. You'll be prompted for confirmation, and if you select yes, screen will terminate all its programs and exit.