>> Index

Yet another GNU Screen tips & hacks page

by Artur Skonecki
Last modified:   09 February 2012
(tested on current development sources)

Contents:

  1. how to: use "ALT + key" combination as an escape
  2. how to: use the ALT key as a modifier with bind
  3. how to: indicate pressed escape key in hardstatus
  4. how to: tmux display-panes + swap-regions + rotate-regions in Screen
  5. how to: move a process into a Screen session>
  6. how to: save Screen sessions
  7. how to: start a new window with the same working directory
  8. how to: create a resize mode



# GNU Screen how to: use "ALT + key" combination as an escape

It is possible to work around :escape command limitations using registers and :bindkey command. Just put this in .screenrc:

# Alt + space as an escape key in GNU Screen

# reset escape key to the default
escape ^Aa

# auxiliary register
register S ^A

# Alt + space produces ^A and acts as an escape key
bindkey "^[ " process S




# GNU Screen how to: use the ALT key as a modifier with bind


# ^[ escape goes first whenever one presses Alt + key
# bind ^[ to activate a command class
bind "^[" eval  "command -c class_escape" "echo \"command class: escape\""

# press Alt-? to show class_escape bindings
bind -c class_escape ? help -c class_escape 

# press Alt-r to resource .screenrc
bind -c class_escape r source "$HOME/.screenrc"




# GNU Screen how to: indicate pressed escape key in hardstatus

I got often baffled in Screen because I didn't know whether the escape key was pressed. This was especially frustrating when dealing with nested sessions. Fortunately, Ben Love came up with a patch which adds a new hardstatus escape for escape key ( bug >> #27713 ). Download >> the patch and apply it to the recent Screen sources. The patch adds a new hardstatus escape %E.
Example:

# invert colors when escape key active
# %{+s} is activate when %E is true

hardstatus string %?%E%{+s}%?%w

hardstatus alwayslastline



# GNU Screen how to: tmux display-panes + swap-regions + rotate-regions in Screen

Update:
An improved version of this tool is now included in >> screen-session (a collection of utilities for GNU Screen).
$ screen-session regions

Recently I got envious of tmux display-panes functionality, so I have decided to bring it to GNU Screen.
tmux display-panes screenshot
  < tmux display-panes   



   screen-display-regions >  
screen-display-regions script screenshot
screen-display-regions consist of 3 files:
  1. >> file screen-display-regions

    - wrapper around screen-display-regions.py, it is necessary to wrap some GNU Screen scripts
  2. >> file screen-display-regions.py

    - the main program
  3. >> file screen-display-regions-helper

    - starts in every region, prints region numbers using "toilet --gay" and "toilet --metal" (I couldn't find anything better)
Put them all in path and make executable. Install "TOIlet". Active layouts before running this script with ":layout new" and turn off zombie mode with ":zombie"

One more thing - you need to be running screen 4.1.0. Because it is unreleased yet, you have to get it with git.
Download Screen sources, compile and install:

    $ git clone git://git.savannah.gnu.org/screen.git screen
    $ cd screen/src; ./autogen.sh ; autoheader ; ./configure && make
    $ sudo make install
Done!
You can also create a binding:

bind n eval "screen sh -c 'sleep 0.01; screen-display-regions'" "other"
Usage:
goto frame:   [number]
goto frame:  ' [number] [ENTER]
swap frames:  s [number]
swap frames and goto:  s' [number]
rotate left:  l
rotate right:  r



# GNU Screen how to: move a process into a Screen session

  1. >> Download injcode and install it in the $PATH ( >> more about injcode ).
  2. Put this script in your path:

    >> file screen-move-process - move a process between ttys

    
    
    
  3. To move a process into Screen session:
    
        $ screen-move-process PID
        
    In the process parent shell type:
    
        $ disown
        



# GNU Screen how to: save Screen sessions

a restored GNU Screen window
screen-session ( aka scs ) is a collection of utilities for GNU Screen. It includes a session saver.
  1. >> Download screen-session and install it according to instructions in INSTALL. First install screen-session itself:
    
        $ tar xf screen-session-VERSION.tar.gz
        $ cd screen-session-VERSION
        $ sudo make install
        
  2. Proceed with compilation and installation of the modified GNU Screen ( build-essentials and libncurses-dev should be installed on your system ).
    
        $ cd screen-session-VERSION/screen-4.1.0
        $ ./configure
        $ make
        $ sudo make install
        
    Check whether the Screen version string matches with screen-session:
    
        $ screen --version
        Screen version 4.01.00scs_0.6.4 (screen-session project) 27-August-11
    
        $ screen-session --version
        screen-session 0.6.4 - a collection of tools for GNU Screen.
        
  3. Make sure to restart running instances of Screen before using screen-session. You might want to take a look at the full screen-session documentation.

    To save a GNU Screen session including all its windows, layouts and processes run:
    
        $ screen-session save save_file
        
    list saved sessions:
    
        $ screen-session ls
        
    and restore ( inside another Screen session ):
    
        $ screen-session load save_file
        
    by default screen-session does not restore window numbers exactly. If you want the loaded session to reassemble closer the original, add --exact flag ( renumber all windows to make room for new ones ) or, to be even more precise, use --exact-kill flag ( which will kill all already existing windows ).
    
        $ screen-session load --exact save_file
        
    It would be very risky to let saved programs start immediately. Instead, every new window displays a prompt and waits for the confirmation.
    screen-session-primer prompt with 'zsh -> vim test'
    It is possible to skip this prompt entirely using "--force-start [win_nums]" option. For example, to instruct screen-session to immediately start programs in windows 1, 2, 3, 12 run:
    
        $ screen-session load --exact --force-start 1-3,12 save_file
        



# GNU Screen how to: start a new window with the same working directory

Update:
An improved version of this tool is now included in screen-session (a collection of utilities for GNU Screen).
$ screen-session new-window

The goal is to be able to start a new window with the same working directory as the current one. It is easy to do with a shell (described >> here) but otherwise, it requires more effort (recompilation of Screen necessary).
  1. Create $HOME/bin/screen-in-dir and make it an executable:

    >> file screen-in-dir - starts Screen window in the same directory (this simple script is not perfect but still does its job)

    
    
    
  2. Append to your .screenrc:
    
            #start another screen window with the same working directory
    bind C exec "$HOME/bin/screen-in-dir"
  3. Apply patch and recompile screen:

    >> file command-tty.diff - adds tty command which prints the filename of the current terminal ( bug 30563 )

    
    
    
  4. Start Screen. Press "Ctrl-a C" to create a new window with the same working directory.



# GNU Screen how to: create a resize mode

Resizing regions in Screen is rather troublesome. Fortunately, it is very easy to create a "resize mode".

Just append this to .screenrc:

# EscapeKey+r puts Screen into resize mode. Resize regions using hjkl keys, toggle focus with Tab and arrow keys.

bind r eval "command -c classresize" # enter resize mode

# use hjkl keys to resize regions
bind -c classresize h eval "resize -h -5" "command -c classresize"
bind -c classresize j eval "resize -v -5" "command -c classresize"
bind -c classresize k eval "resize -v +5" "command -c classresize"
bind -c classresize l eval "resize -h +5" "command -c classresize"

# quickly switch between regions using tab and arrows
bind -c classresize \t    eval "focus"       "command -c classresize" # Tab Key
bind -c classresize -k kl eval "focus left"  "command -c classresize" # Arrow Left
bind -c classresize -k kr eval "focus right" "command -c classresize" # Arrow Right
bind -c classresize -k ku eval "focus up"    "command -c classresize" # Arrow Up
bind -c classresize -k kd eval "focus down"  "command -c classresize" # Arrow Down



>> Index