Short answer: Use 'screen'.

UNIX screen Tutorial

Have you even been performing a time-consuming operation on a remote server through SSH and your connection gets terminated right in the middle of the operation? How about wanting to switch to another "tab" without initiating a whole new SSH session? The GNU screen utility is a "virtual terminal multiplexer", an incredibly useful tool to know if you spend any amount of time in a UNIX system.

One of the major advantages of the screen utlility is the ability to start an application on a remote system, and have the ability to reconnect to the same application from a different system without having to restart the program. You can also detach from your screen session and reattach at any time and resume your work right where you left off. This is because your screen windows are persistent, you can "detach" from a window and it will continue to live in the background on the system.

Detaching and Reattaching to screen Windows.

Let's walk through how to do exactly this. Firstly, you can create a screen window by simply typing "screen" at the command prompt. Once you press enter, you'll notice that your screen will clear and you will get a fresh shell prompt.

A fresh screen session

This is your new window. Let's demonstrate how this window remains persistent even if we close our shell.
We'll begin by typing "top" into the command prompt to show a list of running processes.

The top application running in our new screen window

Now let's detach from our screen session and return to our original shell. All commands for screen are prefixed by control+A. So to detach from our current window, let's press Control+A followed by Control+D (d is for detach).

Although it seems that "top" has been killed and we've been dropped into our original shell, what has actually happened, is that top continues to run in the screen window we created, and we've merely detached from that screen window temporarily.

Let's reattach to that window to resume observing our "top" application at work. To do this, type "screen -r". The -r switch means "reattach" to an existing window. You will notice that we have now resumed our screen session, and the window is focused and interactable. The beauty of this feature is that this works the same if you are accidentally disconnected from a remote system using SSH as well. To continue right where you left off, you would simply have to reconnect to the server and type "screen -r" to reattach just as we did above.

Multiple Windows

Let's say you're editing a file in a text editor such as nano and you need to copy a string from another file without closing the text editor. This is possible by spawning another window using screen.

To demonstrate this, I will start a new instance of screen, and start nano inside that screen.

nano running in our new screen window with some text inputted

Now let's say we need to look at another file on the filesystem without closing the text file I'm currently editing. We can do this in screen by pressing Control+A Control+C (c for create new window).

Our new window with a fresh BASH prompt

You'll now notice that we have another "tab" representing the new window we just created. Let's copy some text from one window to the other. First, I'm going to print the contents of the file we want to copy from.

To copy the text from this window, press Control+A followed by ESC. This puts us in "copy mode", and we can now move our cursor anywhere around the screen. You can then press space to set the first mark of the part we're going to copy. Then press space again to set the end point of the copy range.

Now let's switch back to our other window by pressing Control+A Control+A (this is the toggle command. It will toggle between the current window and the window displayed previously).

To paste the contents of our copy buffer into our text document in nano, press Control+A ] (right square bracket).

Our text file is now complete!

screen Session Sharing

Another one of the best features of screen is the ability to have multiple users/machines connect to the same screen window. This is an invaluable tool for collaboration, and is rather easy to use.

To set up a screen sharing session, create a new screen with a socket called "remote" by typing "screen -S remote". This will create a new screen session with a common socket called "remote". Another machine, window or user can attach to this same screen by typing "screen -x -r sharing" (-x is to attach to a not detached screen session, and -r is to resume the session on this machine)

Now we have one screen session that two users can interact with at the same time! Cool!

Two users can interact with a single screen session