# Various GNU Screen tweaks and hacks.

## Leffe

Portage titles in GNU Screen

Requirements

Having GNU Screen installed.

Preferably running in console mode.

A text editor.

Perhaps some Python experience.

Some general hacking experience is never bad either.

Intended target audience

Screen users and X dislikers.

Contents

Screen titles when using emerge

Titles when using bash

Nicer titlebar

Copying and pasting

Links

Screen titles when using emerge

First of all, open up /usr/lib/portage/pym/output.py in your editor of choice.

Scroll down to xtermTitle on line 36.

Just add this piece of code after the line myt=os.environ["TERM"]:

```
    if myt.startswith("screen"):

        sys.stderr.write("\x1bk"+str(mystr)+"\x1b\\")

        sys.stderr.flush()

```

Save the file and emerge something, emerge moo perhaps. You will see that the title briefly changes during the emerge. Try emerging several real packages and see how useful this is  :Smile: 

If you have no titlebar, either split the window with C-a S or do a C-a : and enter caption always.

Possible problems

You might have to remove the output.pyc and or output.pyo from the directory where output.py is stored, I doubt it though.

Titles when using bash

Ah, nevermind, newer versions of baselayout's (?) /etc/skel/.bashrc already does this.

In case you do not have the newer version, here it is:

```
case $TERM in

    xterm*|rxvt|Eterm|eterm)

        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'

        ;;

    screen*)

        PROMPT_COMMAND='echo -ne "\033k${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'

        ;;

esac
```

Note that mine is different from the official version, if the official version does not work (which it really should not, but somehow it did once for me) try mine instead, I just added a * to screen and replaced an _ with a k.

Nicer titlebar

The default Screen titlebar doesn't look that good, this is what I use, it looks a bit better in my opinion:

```
caption string "%?%F%{= Bk}%? %C%A %D %d-%m-%Y %{= kB} %t%= %?%F%{= Bk}%:%{= wk}%? %n "
```

Put it in your ~/.screenrc or enter it with C-a :. I can't remember how to source a config file from inside Screen right now  :Sad: 

Copying and pasting

This is one of my favorite features of Screen, the ability to copy any text you see and paste it anywhere you can enter text. I can't say I've mastered it yet, but I know enough to use it.

To copy, press C-a [, then move around with the arrow keys or vi-style keys until you get to the place where you would like to start copying, press enter there and move to the end of your text, press enter there and the text will be copied. Something you'll want to do often is select to the end of a line, use $ for that.

To paste the text you have copied, go to where you want it, in an editor perhaps, and press C-a ], the text will be entered. If you are doing this in vim the autoindentation might play some tricks on you, it can be disabled though using :set paste.

Screen Links

GNU Screen homepage

A Screen FAQ

GNU Screen: an introduction and beginner's tutorial

man 1 screenLast edited by Leffe on Wed Dec 15, 2004 2:59 pm; edited 7 times in total

----------

## Leffe

age for quite a lot of new content.

----------

## Wormo

 *Leffe wrote:*   

> Portage titles in GNU Screen
> 
> Copying and pasting
> 
> This is one of my favorite features of Screen, the ability to copy any text you see and paste it anywhere you can enter text. I can't say I've mastered it yet, but I know enough to use it. Knowing how to select to the end of a line would really help.
> ...

 

The default cursor movements in screen copy/paste mode are vi-like. Moving to the end of line is done by '$', jumping to the next word is 'w', and so on. Very powerful!   :Very Happy: 

Also, since you brought up vim... a quick way to get vim to behave for pasting (whether gpm, X, or screen) is to first use 

```
:set paste
```

Then after the paste is done, you can get back any fancy indenting settings with 

```
:set nopaste
```

----------

## Leffe

Thanks a lot! Added it to the article.

----------

## adsmith

I use screen all the time.  A maximaized xterm running screen is my most common window open in X.  Fantastic program.

Anyway, one annoying thing is that screen doesn't properly use "Tab" as a key, apparently; I think it just sends a pile of spaces.  So, as far as I can tell, there is no way to bind Ctrl-Tab to switch between session windows.... grr...

Has anyone found a way around this?

Also, I like this baseline better

```
hardstatus alwayslastline "%{b}[ %{B}%H %{b}][ %{w}%?%-Lw%?%{b}(%{W}%n*%f %t%?(%u)%?%{b})%{w}%?%+Lw%?%?%= %{b}][%{B} %m/%d %{W}%C%a %{b}]"
```

Last edited by adsmith on Wed Dec 15, 2004 1:48 pm; edited 1 time in total

----------

## adsmith

By the way, here that file is in 

```
/usr/lib/portage/pym/output.py
```

Note the /pym/

----------

## Leffe

 *adsmith wrote:*   

> By the way, here that file is in 
> 
> ```
> /usr/lib/portage/pym/output.py
> ```
> ...

 

That would be a typo  :Smile: 

I updated the section on the bash title to hopefully make it work. I can't understand how the original code actually worked for me, it's impossible.

----------

## Wormo

 *adsmith wrote:*   

> 
> 
> Anyway, one annoying thing is that screen doesn't properly use "Tab" as a key, apparently; I think it just sends a pile of spaces.  So, as far as I can tell, there is no way to bind Ctrl-Tab to switch between session windows.... grr...
> 
> Has anyone found a way around this?
> ...

 

Actually, screen has no problem with "Tab" at all. There is a default binding for Ctrl-Tab to switch between "regions" rather than "windows", but I was able to re-bind it to switch between windows just fine:

```
:bind ^I next
```

Speaking of "regions", that is another nifty screen feature that I have recently discovered: ability to split the screen so you can see multiple sessions at once. This is the equivalent to using 

```
:sp
```

 command in vim (which I find extremely useful).  Default keybinding for regions includes 

```
ctrl-a S
```

 to split the window, 

```
ctrl-a X
```

 to delete current region, and 

```
ctrl-a TAB
```

 to switch focus between regions (assuming you didn't re-bind tab).

----------

## Xk2c

 *adsmith wrote:*   

> I use screen all the time.  A maximaized xterm running screen is my most common window open in X.  Fantastic program.
> 
> <snip>
> 
> Also, I like this baseline better
> ...

 

In my opionen it is better to use the feature caption always instead of hardstatus.

The advantage of caption always is, that the Xterm Title

still behave like common.

If your interrested here is my ~/.screenrc:

https://forums.gentoo.org/viewtopic.php?p=1886655#1886655

----------

## Wi1d

Argh. I searched, wrote the howto to, then searched one more time and found this thread.  :Sad:  No need to start a new post I guess and I don't want to waste the howto. Btw, excellent tips.

My simple,  yet semi-thorough, tutorial on using gnu-screen

What is it and what does it do?

Screen is often described as a terminal multiplexer. It can be found on any *nix distro and cygwin even has a port. So what is it useful for?

It's great if you like to use many command-line applications. If you have to remote connect a lot it's like you can access your desktop from anywhere.Gives you the ability to disconnect from a "session", log off the machine, and reconnect later on. This is great when your updating a nix box or running any time consuming command.You can customize it's looks and create your own keybindingsIt eliminates the need to have several terminals opened.It has an edit mode which is damn useful when reading system logs, lots of output, or other big files.

Examples of screen in action

One of my screen sessions

[img:e9c898c66f]http://xs45.xs.to/pics/05364/example.jpg.xs.jpg[/img:e9c898c66f]

A split screen session

[img:e9c898c66f]http://xs45.xs.to/pics/05364/exampleSplit.jpg.xs.jpg[/img:e9c898c66f]

Putting screen in action

First thing first. Install screen:

```
# emerge -av screen
```

Now that it's installed you can simply start a session by typing "screen" from the command prompt. This sucks though because you are letting screen create it's own names  for each session and they can be unusual. I usually start a session like "screen -S main", "screen -S update", or name it after whatever commands the session will be executing.

Once you've started you can open a new session inside of screen by typing "ctrl-a, c". Once your ready to disconnect from the session type "ctrl-a, d". When you are ready to reconnect type "screen -r" at the command line or "screen -x" if the session is already attached and you've ssh'd in and want to connect the that particular session.

Here is a short list of other default keybindings:

ctrl-a ctrl-a  (jumps back and worth between two sessions.)ctrl-a shift-s  (splits screen)ctrl-a shift-x (undo split)ctrl-a " (give list of screens)ctrl-a spacebar OR n (move to next screen)ctrl-a backspace OR p (move to previous space)ctrl-a shift-? (help)

Customizing

You can change the way screen looks and behaves by modifying your ~/.screenrc file. One of the first things I do is add some of my own keybindings. Examples:

```
bind C screen -t "ncmpc" 0 ncmpc

bind M screen -t "mutt" mutt -y

bind i screen -t "irssi" irssi

bind R screen -t '%{rb} |root' sudo -s -H

bind Q screen -t "qjots" vi ~/.quickjots

bind Y screen -t "yafc" 9 yafc

bind a screen -t "abook"  abook

bind t screen -t "torrent"

bind T screen -t "top" sudo top

bind s screen -t 'mysql' mysql -p

bind D screen -t "dict" w3m -cookie www.freedictionary.org

bind E screen -t "encyc" w3m -cookie en.wikipedia.org/wiki/Main_Page

bind N screen -t "note" note

bind P screen -t "lookup" w3m -cookie http://local.google.com/lochp

bind W screen -t "www" w3m -cookie -B

bind V screen -t 'vim' vim

bind L screen cmatrix -f -s -C blue

bind A screen -t 'mixer' alsamixer

bind m screen -t 'metasploit' ~/tools/exploits/framework-2.4/msfconsole

bind v screen -t 'vpn' sudo ~/vpn.sh
```

Once thing that gets old really quick if you use screen is having to type "ctrl-a n" and "ctrl-a p" to navigate between your screens. I've changed this in my config file by adding a smoother keybinding like "ctrl-left arrow" and "ctrl-right arrow. Since the keybinding is often different  I'll show you how to do in your configuration file.

 open up in and editor ~/.screenrc. I think this will work with any editor but know it will for sure with vim.

 Now you want to type something similiar to this:

```
bindkey "^[Oc" next

bindkey "^[Od" prev
```

 The thing that might be different is the "^[Od" part. To get the right code you need to press "ctrl-v" then keep holding down the ctrl key and press one of the arrow keys.

Another thing you can do is modify the "hardstatus" in your screenrc file. This is how you can add the tabbed menu at the bottom like in my examples above. Here is what my hardstatus line looks like. To get a grasp on how to change the variables and colors read the man page.

```
hardstatus alwayslastline "%{dw} \\\\ %{dB}%50`%{dw} \\---\\\\ %{dB}%-w%-u%50>%{Kw}%n-%t%{dB}%+u%+w%<%> %{dB}\\%=%{dw}\\\\%{dB} Time:%{dw}%C %{dB}%D%{dw} \\"

sorendition = wb
```

You can also execute commands and use the output in the hardstatus by using "backtick". Add somthing like this in your .screenrc:

```
backtick 50 0 0 hostname
```

Now anywhere in the hardstatus like you put "%50`" the output of the command hostname will magically appear there.

Here is another creative example in how to put your outside weather in screen  :Wink: . 

Add a similiar entry in your crontab: 

```
  */15    *       *       *       *       curl -s http://weather.noaa.gov/weather/current/ABCD.html | grep 'F (' | head -n 1 | awk {print $3 " " $4}' > /tmp/temperature
```

Create a similiar script:

```
#!/bin/sh 

while :

do

    cat /tmp/temperature

    sleep 16m

done
```

And then add a "backtick" into your screenrc kind of like this:

```
backtick 52 0 0 watch-weather
```

Now where every you put "%52`" in the "hardstatus line" your current weather will be displayed. example:

```
hardstatus alwayslastline "%{dr} %D %C%A %{dd} %{Bw}%-w%{db}%50>%n %t%{dw}%+w%<%>%{dd} %=5%{wB} (%{wr}%52`%{wB})%{wr}   %50` "
```

Well anyhow that's it for my "simple yet semi-thorough" turorial on using screen. It's just one of those command line applications, like vim for example, that the more you play with it the more you see what it can do.

----------

## lucapost

I have a problem to set background/foreground color via hexadecimal syntax in ~/.screenrc .

Can you help me?

This is my caption:

```
caption always '%{wb}%?%-Lw%?[%n*%f%t%?(%u)%?]%?%+Lw%?%? %-= @%H'
```

What can I change this in hex ???

```
%{wb}   >>> %{ ???????? }   
```

----------

## eccerr0r

I don't know how people are dealing with this, but I think I inadvertently found a cute way to deal with world updates that involve gnu-screen.

The problem:

# note that there's a gnu-screen update hidden in the mix of world updates...

screen

emerge -uDN @world

<detach>

logout

<wait until tomorrow>

screen -r

OH NO!  I can't connect back to my gnu-screen session because it updated!!! :(

I inadvertently found my current favorite way to prevent this problem by abusing portage's protection:

Simply:

# cd ~root

# rm -f screen-old; ln /usr/bin/screen-* screen-old

This will create a hardlink of the SUID screen binary which is VERY evil.  However *You* did it, and you did it explicitly, so that's not a problem.  With the link in place, portage will find this hardlinked SUID binary when it tries to unmerge the old version and FAIL -- preserving your old screen binary and you can still use the old version to reattach to your screen session!

Then when your emerge is done, remove the ~root/screen-old and emerge screen by itself outside of gnu-screen... problem averted!

Hope this tip saves some heartaches if you forget to emerge --exclude screen or mask the current version.  In fact it's probably safe to just keep that hardlink around, you'll know what the problem is when it breaks your emerge and can --keep-going.

[Edit: Make sure that you link the actual binary and not a symbolic link.]

----------

