Screen

From The World according to Vissie
Jump to navigation Jump to search

My new Notes on Screen

https://liquidat.wordpress.com/2008/10/17/short-tip-gnu-screen-with-proper-scroll-session-management-support/ http://aperiodic.net/screen/quick_reference

Eventually I got my laptop to display the battery remaining time in the status bar of screen. Wow, nice! :-) First, I have a script that just formulates the battery status for me, gives it some color and a % text etc. It also displays if it is charging, or discharging. I then use a backtick to display that in screen. Here is how:

I now have two scripts. One uses /proc and the newer one acpi

acpi

vim: /usr/bin/battery.sh
#!/bin/bash
#
# Modified by Vissie
# battery status script adapted for screen's color codes
#

# To detect that the battery is not attached to the computer.
STATE=$(acpi -b)

if [ -z "$STATE"  ]; then
        echo "Exiting..."
        exit
fi

# Lets use another way to chech the time remaining. i know I could have done the whole script like this!
STATUS=`acpi | awk '{ print $3}'`
CHARGE=`acpi | awk '{ print $4}'`
TIME=`acpi | awk '{ print $5}'`

STATUS=${STATUS:0:-1}
if [ -z $TIME ]; then
        TIME="Full"
        CHARGE=100
else
        TIME=${TIME:0:-3}
        CHARGE=${CHARGE:0:-2}
fi
NON=''
BLD='\005{= W}'
RED='\005{= r}'
GRN='\005{= g}'
YEL='\005{= Y}'
SS='\005{= W}['
EE='\005{= W}]'

# Lets start at red and work out way up if we get more chared up.
COLOUR="$RED"

case "${STATUS}" in
        'Full')
                BATSTT="$BLD=$NON"
        ;;
        'Charging')
                BATSTT="$BLD+$NON"
        ;;
        'Discharging')
                BATSTT="$BLD-$NON"
        ;;
esac

# Set the different color options
if [ "$CHARGE" -gt "15" ]; then
        COLOUR="$YEL"
fi

if [ "$CHARGE" -gt "30" ]; then
        COLOUR="$GRN"
fi

echo -e "${SS}${BATSTT}${COLOUR}${CHARGE}% (${TIME})${EE}$NON"
# end of file

/proc

vim: /usr/bin/battery.sh
#!/bin/bash
#
# battery status script adapted for screen's color codes
#

BATTERY=/proc/acpi/battery/BAT1

# If you can not find the status of battery 1, then exit
STATE=`cat $BATTERY/state`
if [ ! "$STATE" == 'present: yes' ]; then
        exit
fi


REM_CAP=`grep "^remaining capacity" $BATTERY/state | awk '{ print $3 }'`
FULL_CAP=`grep "^last full capacity" $BATTERY/info | awk '{ print $4 }'`
BATSTATE=`grep "^charging state" $BATTERY/state | awk '{ print $3 }'`

# Lets use another way to chech the time remaining. i know I could have done the whole script like this!
# I only found this later. So for now I will leave the old code. I might chnage the whole thing later
TIME_LEFT=`acpi | awk '{ print $5}'`

CHARGE=`echo $(( $REM_CAP * 100 / $FULL_CAP ))`

NON=''
BLD='\005{= W}'
RED='\005{= r}'
GRN='\005{= g}'
YEL='\005{= y}'
SS='\005{= W}['
EE='\005{= W}]'

# Lets start at red and work out way up if we get more chared up.
COLOUR="$RED"

case "${BATSTATE}" in
        'charged')
                BATSTT="$BLD=$NON"
        ;;
        'charging')
                BATSTT="$BLD+$NON"
        ;;
        'discharging')
                BATSTT="$BLD-$NON"
        ;;
esac

# prevent a charge of more than 100% displaying
if [ "$CHARGE" -gt "99" ]; then
        CHARGE=100
fi

# Set the different color options
if [ "$CHARGE" -gt "15" ]; then
        COLOUR="$YEL"
fi

if [ "$CHARGE" -gt "30" ]; then
        COLOUR="$GRN"
fi

echo -e "${SS}${BATSTT}${COLOUR}${CHARGE}% (${TIME_LEFT})${EE}$NON"
# end of file


vim: /etc/screenrc
# no welcome message
startup_message off

# Save split screens when you exit
layout save default

## 256 colors
# Tell screen how to set colors. AB = background, AF=foreground
attrcolor b ".I"
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
defbce on

# Enable 256 color term
term xterm-256color

# Cache 1000 lines for scroll back
defscrollback 1000

# Allow editors etc. to restore display on exit
# rather than leaving existing text in place
altscreen on

# mouse tracking allows to switch region focus by clicking
mousetrack on

# get rid of silly xoff stuff
# if screen freezes, can be resolved with C-a q
bind s split

# automatically detach on terminal hangup (so that one can attach again and
# continue working)
autodetach on

# ignore case on searches in copy-mode
ignorecase on

# assume UTF-8 encoding
defutf8 on

# Bind F11 and F12 (NOT F1 and F2) to previous and next screen window
bindkey -k F1 prev
bindkey -k F2 next

# set color of borders and command-line (cyan background, black foreground)
#rendition so = ck

windowlist string "%4n %h%=%f"
altscreen on

# mouse tracking allows to switch region focus by clicking
mousetrack on

# get rid of silly xoff stuff
# if screen freezes, can be resolved with C-a q
bind s split

# automatically detach on terminal hangup (so that one can attach again and
# continue working)
autodetach on

# ignore case on searches in copy-mode
ignorecase on

# assume UTF-8 encoding
defutf8 on

# Bind F11 and F12 (NOT F1 and F2) to previous and next screen window
bindkey -k F1 prev
bindkey -k F2 next

# set color of borders and command-line (cyan background, black foreground)
#rendition so = ck

windowlist string "%4n %h%=%f"


# %{= kG} 	: set the background colors to black (k) and foreground to green (G) and keep current text styles (=)
# [     	: opening bracket character
# %{g}		: light green
# %H      	: hostname
# %? %1`%?    	: print output of 'backtick' command 1 (defined elsewhere in .screenrc)
# %{G}]		: Bright green
# ]     	: closing bracket character
# [     	: opening bracket character
# %{W}]		: Bright white
# %-Lw    	: list windows before current window (L [optional] = "include flags")
# %{= bW} 	: set colors to bright white (W) on blue (b) and keep current text styles (=)
# %n            : window number
# %f      	: window flags
# %t      	: window title
# %{-}		: Restore color
# %+Lw    	: list windows after current window (L [optional] = "include flags")
# %{-}		: Restore color
# %=      	: expand to fill all space (used here to make remaining content flush right)
# ]     	: closing bracket character
# [     	: opening bracket character
# %{y}		: non bright yellow
# %M/		: Month (Jan else %m 01) with slash
# %d/		: day with slash
# %Y		: 4 diget year
# %{W}		: White with space
# %c		: current time (24-hr; 12-hr is %C
# %{G}		: Bright Green
# ]       	: closing bracket character

# the following two lines give a two-line status, with the current window highlighted
hardstatus alwayslastline
hardstatus string '%{= kG}[%{g}%H%? %1`%?%{G}][%{W} %-Lw%{= bW}%n%f %t%{-}%+Lw %{-}%=][%{y}%M/%d/%Y %{W}%c%{G}]'


Screen shell title update with vim (and file name)

All you have to do is to update your vimrc file as per http://vim.wikia.com/wiki/Automatically_set_screen_title

PLEASE remember to add the escape sequence else it will not work.

I also found out how to get rid of the annoying "Thanks for flying Vim" message after a vim edit.

You will see the ResetTitle function that I call on VimLeave. Magic stuff


vim: /etc/vim/vimrc.local
function! ResetTitle()
       " disable vim's ability to set the title
       exec "set title t_ts='' t_fs=''"
       " and restore it to 'bash'
       exec ":!echo -e '\033kbash\033\\'\<CR>"
endfunction

au VimLeave * silent call ResetTitle()

" add_title_sting_screen
let &titlestring = " [ vim " . expand("%:p") . "]"
if &term == "screen"
        set t_ts=^[k
        set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
                set title
endif

Some useful short cuts:

ctl-a + c Create a new session

ctl-a + n move to the next sessions

ctl-a + p move to the previous sessions

ctl-a + d detach

ctl-a + K Kill

screen -ls list all available screens

ctl-a : layout save default

Then when you detach and re-attach to the session the layout should be restored.

Just wanted to mention to other cool tricks you can do with screen. Screen can monitor a window for activity or lack thereof. This is great if you are downloading large files, compiling, or watching for output. If you are downloading something or compiling, you can watch for silence. To start the monitor, go to the screen you want to monitor and use “Ctrl-A” “M” to look for activity or “Ctrl-A” “_” to monitor for silence. Then open or switch to a new window. When the monitor detects activity or silence, you will get an alert at the bottom with the window number. To quickly go to that window, use “Ctrl-A” “ (thats a quote mark, ctrl-a then a “). After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. For example, to stop monitoring for activity you would use “Ctrl-A” “M” again.


Entering Copy Mode and Scrolling

By default, enter copy mode with

CTRL+a then

ESCAPE or [ You can use vi like commands to navigate through the scollback buffer. The arrows and

PAGE UP/DOWNshould work as well.

Copying Text

Once the cursor is at the beginning location, press

SPACEBAR to set your first mark. Next navigate to the ending position and hit SPACEBAR again. The second SPACEBAR will set your end mark and exit copy mode. One neat and often useful feature is selecting a rectangle on the screen rather than full lines. Use

c and C to set the left and right column margins to the cursor’s current location.

Pasting

Pasting is easy, just hit

CTRL+a to activate screen then ] to paste.

How to mirror or share one screen with others:

CTRL-A

multiuser on

screen -x

Then let the other user use this command to connect:

screen -x shareduser/shared

Screen shell title update with vim (and file name) (OLD!)

Wow, another full day dedicated to one little bit of code. But it did it in the end :-). I wanted to display the "file that is being edited" by vim in my screen. It took lots of reading and lots of trail and errors!

The biggest mistake I made was to think that this is fully done in screen. This statement is wrong! You have to send a escape sequence from your bash shell prompt so that screen can pick this up, then only start parching the file names across....

This is what I found: http://sunsite.ualberta.ca/Documentation/Gnu/screen-3.9.4/html_chapter/screen_10.html

Here's how it works: you must modify your shell prompt to output a null title-escape-sequence (ESC k ESC \) as a part of your prompt. The last part of your prompt must be the same as the string you specified for the search portion of the title. Once this is set up, screen will use the title-escape-sequence to clear the previous command name and get ready for the next command. Then, when a newline is received from the shell, a search is made for the end of the prompt. If found, it will grab the first word after the matched string and use it as the command name. If the command name begins with `!', `%', or `^', screen will use the first word on the following line (if found) in preference to the just-found name. This helps csh users get more accurate titles when using job control or history recall commands.

Bash users will probably want to echo the escape sequence in the PROMPT_COMMAND:

vim: /etc/bash/bashrc
PROMPT_COMMAND='echo -n -e "\033k\033\0134"'

My prompt ends with a "#", so the search field below starts at "#"

vim: /etc/screenrc
shelltitle '# |bash'

Split the screen

Ctrl+A+S # Create another window
Ctrl+A+Tab # Moving from another window
Ctrl+A+c # Create new session
Ctrl+A+| # Splitting vertical
Ctrl+A+X # Kill current split sesion

screen and ssh

vim: ~/bas.profile
if [ -z "$STY" ]; then screen -DR Vissie; fi