Customizing screen for Efficiency
Now that you now the basics of screen I’m going to talk about efficiency. In my humble opinion the default key binding of screen are inefficient, that’s why the first thing we will do is change them. The escape sequence will be the first thing to go. Instead of having to use 2 fingers to access screen commands we are going to use just one. The less keys we have to type the faster we will be, remember the KISS rule(Keep It Simple Stupid). As I touch type, and can reach the ` key(back tick) rather quickly, that is the key I chose for the escape sequence. You can of course chose a key that suits you better but remember that it should be a key not used frequently.
Now before I go any further I need to mention the .screenrc file. This file is your default configuration file for screen. Screen always looks in the home directory for this file, so all our changes will land there.
To change the escape sequence we add the following line to .screenrc:
The first ` is the escape sequence, while the other ` is the command used to send ` the window in focus.
The next key binding on our list is the change title command. Currently it’s set to A, this shortcut is confusing so I change it to t(like title, more logical). Splitting the screen requires me to use the shift key, better would be using a lowercase s, so that’s how we are going to have it. Quitting screen requires me to type the : command and entering quit into the command line, way too much typing. We are going to bind Q to the quit command. Also we are going to rebind the kill window to capital K, so that we don’t accidentally kill a window. The window list command also isn’t intuitive so we are going to rebind it to w. I also added a couple of other key binding. Here are the commands you to add to the .screenrc file to change the key bindings:
bind s split # Split window
bind - resize -5 # Decrease window region by 5
bind = resize +5 # Increase window region by 5
bind q only
bind Q quit
bind t title # Window title
bind T title # Window title
bind w windowlist -b
bind j focus down # Switch to window below
bind k focus up # Switch to window above
bind K kill
If you haven’t configured screen yet, you probably noticed the annoying information notice that shows up at startup. This is annoying so we are going to disable it:
startup_message off
Automatic Titles
Having windows without descriptive descriptions isn’t helpful, but changing the titles manually is tedious. Wouldn’t it be great if somehow the titles updated automatically to the current application running… Well it possible, and very helpful. Screen has thought about dynamic updating of titles, and it is done using a terminal escape sequence (a chain of character that are invisible but tell screen to change the title). The escape sequence is as follows: \[\ekNewTitle\\\]. If you echo this in your shell like this:
echo –e ‘\[\ekMy New Title\\\]’
The current window title in screen will change to ‘My New Title’. Now this isn’t automatic as I said before. Well this is the first step. This is the way to manually change the title from the shell or any other application. If you put the previous escape sequence to the end of your command prompt, without a new title screen will actually use heuristics to determine the current running application! Let’s say this is your shell prompt:
You would change it to this:
PS1=’\u@\h \W
\[\ek\\\]# ‘
Unfortunately changing just that won’t actually give you dynamic titles, you will need to make an additional change in your screenrc file. You will need to change the default shell title, and add the character that end you shell prompt. Those characters, taking the previous example, would be ‘# ‘ (hash and space – without the quotes). To change the default shell title you have to append shelltitle to the screenrc. Now the format of shelltitle for dynamic titles is as follows: shelltitle=”PROMPT_END_CHARS|SHELL_NAME”. Here is a practical example:
The “# “ are the characters that end the shell prompt, the | says that dynamic titles are to be used and bash is the name of the title if no commands are running. There is actually a variation of the command above. If you use the above title, whenever you run a command the ‘bash’ title will be replaced with the currently running command, and once the command finishes it reverts back to ‘bash’. Appending a colon (:) to bash will actually append the currently running command to the name. So say you are running vim in bash, the title would be “bash: vim” instead of just “vim”. Here is a example:
The Hardstatus and status line
Using the window listing command gives you an overview of the currently opened windows, but you have to use the command to see something, wouldn’t it be better if could see the list of windows at the bottom of the screen. Yes it is possible, and I love this feature, because instead of scratching my head trying to remember what the next windows is I just glance at the bottom of my screen. Screen has 2 types to status lines, one called the hardstatus while the other is called caption. The hardstatus is a status line at the very bottom of your screen, while caption is local to the window. I always want to see the hardstatus so I have added the following to the config file:
hardstatus alwayslastline
Now both the status lines are actually special strings that contain different types of variables that change with time and events. The actual syntax is quite complex and way beyond this guide, so if you really want to understand it I highly recommend you look at screen’s man page under STRING ESCAPES. I’m going to provide you with the following 2 status lines:
hardstatus string '%02=%0>%{= kw}%-Lw%50>%{=b bw} %n %t %{= kw}%+Lw%{= kw}%-1<%{-}%{=dd}'
caption always "%?%F%{=b bW}%:%{= bW}%? [%f%] %t - %h %-050=%H%-027= %D %d %M %Y, %C %A %{= .b}%{-}%{=dd}"
Here is a screenshot of how it actually looks like:
hxxp://forums.remote-exploit.org/picture.php?albumid=2&pictureid=3
Much better don’t you think?
The last line of the display is the hardstatus line, it basically displays all the currently open windows and highlights the window in focus. The before last line and the line on the middle of the screen is the caption status line. It displays the window number in brackets, then the name, then the status of the window, next the hostname, and lastly the current time and date.
Some additional settings
By default screen opens up with only one window, usually you will be using more than one window so lets open 4 windows at startup instead of just one:
screen 0
screen 1
screen 2
screen 3
screen 4
Now because the last window created is the one in focus, I always switch to the next window so that the window in focus is the first one created. Here’s how:
I also always set auto detach on, so when I’m working through a ssh session if I lose the connection I won’t lose my work.
The Entire .screenrc file
Code:
startup_message off
vbell off
escape ``
defscrollback 10000
deflogin off
# Default shell
shell bash
# Defaul shell title - dynamic
shelltitle "] |bash"
screen 0
screen 1
screen 2
screen 3
screen 4
# Swiching to first screen
next
#backtick 9 0 0 hostname
autodetach on
hardstatus alwayslastline
hardstatus string '%02=%0>%{= kw}%-Lw%50>%{=b bw} %n %t %{= kw}%+Lw%{= kw}%-1<%{-}%{=dd}'
caption always "%?%F%{=b bW}%:%{= bW}%? [%f%] %t - %h %-050=%H%-027= %D %d %M %Y, %C %A %{= .b}%{-}%{=dd}"
windowlist title "Num %10=Title %=Flags%"
windowlist string " %n - %10= %t %f"
#Timeout for displaying messages
msgwait 5
activity " Activity has appeared in window %n - %t"
#Key bindings
bind s split # Split window
bind - resize -5 # Decrease region by 5
bind = resize +5 # Increase region by 5
bind q only
bind Q quit
bind t title # Window title
bind T title # Window title
bind w windowlist -b
bind j focus down
bind k focus up
bind K kill
#terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'