screen
I thought I’d blog about screen and my experience with it. I am no screen expert but since I find it useful, I thought I’d share how I use it. I mostly use screen for its detachability and multiplexing features, although it does have a number of other uses.
Detachability
Screen’s detachability feature has helped me out on a number of occasions. I love the fact that I can kick off a script in a terminal at work, detach from the terminal, go home, then reattach my screen session and it’s there untouched as if I’d never left! (Okay, love might be a little strong). There are obviously other ways to make your scripts run in the background, however I am yet to find any as elegant as this, but that’s not the only reason to use screen…
Multiplexing
Yes the beauty of screen is the fact you can, within one screen session effectively have multiple terminal windows open and switch through them. I dislike having multiple putty windows open so for me screen is so useful because I can have all my ssh sessions I need open in the one terminal and switch between them.
Useful screen commands:
To start a new screen session, you can just type ‘screen’ and screen will start automatically creating a name for your screen session. I prefer to define the name of my screen sessions so that when I reattach the screen I don’t need to guess which screen I was in.
FYI all screen commands require that you use the <ctrl + a> escape sequence prior to any commands.
screen -S <screen name>
Okay, so now you’re in screen.
To create a new window:
<ctrl + a> c
To jump to the next window use either of these commands:
<ctrl + a> space or <ctrl + a> n
If you want to jump to a specific window number, which is useful if you have 10 screens open and you want to jump around quickly, use this command:
<ctrl + a> <window number> e.g. <ctrl + a> 10
If you did have 10 windows open, it could become confusing as to what you are running in each window, to help you remember you can rename the windows, e.g. if you have an ssh session open to a server named yoda maybe call that window yoda. To rename, switch to the correct window and…
<ctrl + a> A
You will see a prompt appear with the window’s existing name. In this example it is named bash
Delete bash and type the new name for your window and press enter. As you can see in the image below the window has been renamed to yoda:
You can view all the windows you have open by typing this command:
<ctrl + a> ”
You can arrow through these windows and press enter onto the window you want to go to.
You will see in my images that I have the window bar visible, this can be set in your config but I won’t go into that in this post. So, if you don’t have the window bar visible, you can use this command to see it:
<ctrl +a> w
To view the complete list of keybindings use the following command:
<ctrl + a> ?
You can see the output here: (click on the image to see it full size)
To close screen use the following command:
<ctrl +a> d
This just detaches you from the screen session. Because of screen’s detachability feature, you can rest assured that you will be able attach to the screen again exactly as you left it.
To view the screen sessions that are open, simply use:
screen -ls
As you can see, the screen session called myScript is listed. To attach to this screen again you can type the following:
screen -r <nameOfScreenSession>
You can also attach to a screen session just by using it’s process id, shown below:
screen -r [processId]
To permanently close a window within screen, just type exit. If you exit every window in your screen then your screen session will be terminated. I don’t know of another way to make screen gracefully exit. You can use this command, but it is not recommended:
<ctrl + a> <ctrl + \>
Leave a comment







