Talk:Screen

From Gentoo Linux Wiki

(Redirected from Talk:TIP Using screen)
Jump to: navigation, search
Note: From time to time I do delete stuff from this page if the input has been --somehow-- processed. Please refer to this pages history.

If you have something to contribute, don't hesitate to put it in the article!

Cprior 20:06, 2 February 2006 (GMT)

Hi there, great Tip! I've two things I would like some comments on:
First, I always find it annoying if I use stuff like

screen -t E-Mail 2 mutt -y

and the tab gets closed if I quit mutt accidentally. Thus I always use

screen -t E-Mail 2
stuff "mutt -y\015"

(Please specify the package providing "stuff". -Roger 06:33, 25 April 2008 (UTC))

Second, I'am a bash-user and utilize $PROMPT_COMMAND to update the name of the current screen-tab .. depending on the user@host I'am.

# ~/.bashrc
function rename_screen_tab () { echo -ne "\x1bk$@\x1b\\"; return 0; }

if [[ "$TERM" = screen* ]]; then
  PROMPT_COMMAND='rename_screen_tab ${USER}@${HOSTNAME%%.*}'
  [ "$HOSTNAME" = "hostname-of-mainbox" ] && PROMPT_COMMAND='rename_screen_tab ${SCREEN_TAB:-$USER}'
fi

Thus my root-shells in screen are all titled "root", and the user shells "appro" (my username). If I ssh out to a server (I keep my bashrc synced on all boxes I've access to), the shell is titled username@hostname (in example, "appro@pitr", "root@isidor"). It's pretty convinient imho.

The only downside is that for things like the "irssi"-tab I've to use

screen -t IRC                 0       
stuff "export SCREEN_TAB='IRC'\015"
stuff "irssi\015"

So, what do you thing? Could anything of the above be useful? --appro 19:13, 12 November 2005 (GMT)


Indeed, and although I saw your contribution already some days ago I only found time now to change the article. I have so far only included the first part, and included your user name in the summary of that particular change. The second part already calls for a TIP_Advanced_GNU_Screen_Usage ! I feel I have already stretched my original idea of a "linear" walkthrough quite a bit, and I think an advanced article could do without all that holding of newbies hands... Why don't you start one? ;)

We could throw snippets from our $HOME/.*rc files in...

--Cprior 13:31, 29 November 2005 (GMT)


well, here's some snippets from mine.

first, I set up screen to use a caption/hardstatus line combo (which I find aesthetically pleasing with the appearance of a drop down tab for my current window) that shows the window hardstatus on the caption line, and the window names on the hardstatus line. (WORD OF WARNING: bright black may not look grey on all systems, so the black on bright black contrast might not work).

# ~/.screenrc
# don't use the hardstatus line for system messages, use reverse video instead
# (we'll be using it for the list of tab windows - see hardstatus alwayslastline
# below)
hardstatus off

# use the caption line for the computer name, load, hstatus (as set by zsh), & time
# the caption line gets repeated for each window being displayed (using :split),
# so we'll use color cues to differentiate the caption of the current, active
# window, and the others.
#    always                  - display the caption continuously.  Since
#                              hardstatus is 'alwayslastline', it will be on the
#                              next to last line.
#    "%?%F"                  - if (leading '%?') this region has focus ('%F') 
#                              (e.g. it's the only region being displayed, or,
#                              if in split-screen mode, it's the currently active
#                              region)
#      "%{= Kk}"               - set the colorscheme to blac[k] on grey (bright blac[K]),
#                                with no other effects (standout, underline, etc.)
#    "%:"                    - otherwise ('%:' between a pair of '%?'s)
#      "%{=u kR}"              - set the colorscheme to [R]ed on blac[k], and
#                                underline it, but no other effects (bold, standout, etc.) 
#    "%?"                    - end if (trailing '%?')
#    "  %h "                 - print two spaces, then the [h]ardstatus of the
#                              current tab window (as set by zsh - see zshrc) and
#                              then another space.
#    "%-024="                - either pad (with spaces) or truncate the previous
#                              text so that the rest of the caption string starts
#                              24 characters ('024') from the right ('-') edge of
#                              the caption line.
#                              NOTE: omitting the '0' before the '24' would pad
#                              or truncate the text so it would be 24% from the
#                              right.
#    "%{+b}                  - add ('+') [b]old to the current text effects, but
#                              don't change the current colors.
#    " %C:%s%a %D %d %M %Y"  - print the [C]urrent time, a colon, the [s]econds,
#                              whether it's [a]m or pm, the [D]ay name, the [d]ay
#                              of the month, the [M]onth, and the [Y]ear.
#                              (this takes up 24 characters, so the previous
#                              pad/truncate command makes sure the clock doesn't
#                              get pushed off of the caption line)
#    "%{= dd}"               - revert to the [d]efault background and [d]efault
#                              foreground colors, respectively, with no ('= ')
#                              other effects.
caption always           "%?%F%{= Kk}%:%{=u kR}%?  %h %-024=%{+b} %C%a %D %d %M %Y%{= db}"
# use the hardstatus line for the window list
#    alwayslastline      - always display the hardstatus as the last line of the
#                          terminal
#    "%{= kR} %-Lw"      - change to a blac[k] background with bright [R]ed text,
#                          and print all the tab [w]indow numbers and titles in
#                          the [L]ong format (ie with flags) upto ('-') the
#                          current tab window
#    "%{=b Kk} %n%f %t " - change to grey (bright blac[K]) background with
#                          [b]old blac[k] text, with no other effects, and print
#                          the [n]umber of the current tab window, any [f]lags it
#                          might have, and the [t]itle of the current tab window
#                          (as set by zsh - see zshrc).
#                          NOTE: the color match with the caption line makes it
#                          appear as if a 'tab' is dropping down from the caption
#                          line, highlighting the number & title of the current
#                          tab window.  Nifty, ain't it)
#    "%{-}%+Lw "         - revert to the previous color scheme (red on black)
#                          and print all the tab [w]indow numbers and titles in
#                          the [L]ong format (ie with flags) after ('+') the
#                          current tab window.
#    "%=%{= dd}"         - pad all the way to the right (since there is no text
#                          that follows this) and revert to the [d]efault
#                          background and [d]efault foreground colors, with no
#                          ('= ') other effects.
hardstatus alwayslastline "%{= kR} %-Lw%{=b Kk} %n%f %t %{-}%+Lw %=%{= dd}"

Then, I use zsh's preexec() and precmd() functions to set my window name and hardstatus automatically. For the non-zsh inclined, preexec() is called before printing a prompt, and precmd() is called before running a command.

This way I can have my window name change to the name of the running command, and revert to something else when a command completes.

I hardly ever want to manually rename a tab (though it is a bit harder to do so when I do), since these names accurately describe to me what I'm doing in that window. Your mileage may vary.

# ~/.zshrc
# if using GNU screen, let the zsh tell screen what the title and hardstatus
# of the tab window should be.
if [[ $TERM == "screen" ]]; then
  # use the current user as the prefix of the current tab title (since that's
  # fairly important, and I change it fairly often)
  TAB_TITLE_PREFIX='"${USER}$PROMPT_CHAR"'
  # when at the shell prompt, show a truncated version of the current path (with
  # standard ~ replacement) as the rest of the title.
  TAB_TITLE_PROMPT='`echo $PWD | sed "s/^\/Users\//~/;s/^~$USER/~/;s/\/..*\//\/...\//"`'
  # when running a command, show the title of the command as the rest of the
  # title (truncate to drop the path to the command)
  TAB_TITLE_EXEC='$cmd[1]:t'
 
  # use the current path (with standard ~ replacement) in square brackets as the
  # prefix of the tab window hardstatus.
  TAB_HARDSTATUS_PREFIX='"[`echo $PWD | sed "s/^\/Users\//~/;s/^~$USER/~/"`] "'
  # when at the shell prompt, use the shell name (truncated to remove the path to
  # the shell) as the rest of the title
  TAB_HARDSTATUS_PROMPT='$SHELL:t'
  # when running a command, show the command name and arguments as the rest of
  # the title
  TAB_HARDSTATUS_EXEC='$cmd'
 
  # tell GNU screen what the tab window title ($1) and the hardstatus($2) should be
  function screen_set()
  {  
    #  set the tab window title (%t) for screen
    print -nR $'\033k'$1$'\033'\\\

    # set hardstatus of tab window (%h) for screen
    print -nR $'\033]0;'$2$'\a'
  }
  # called by zsh before executing a command
  function preexec()
  {
    local -a cmd; cmd=(${(z)1}) # the command string
    eval "tab_title=$TAB_TITLE_PREFIX$TAB_TITLE_EXEC"
    eval "tab_hardstatus=$TAB_HARDSTATUS_PREFIX$TAB_HARDSTATUS_EXEC"
    screen_set $tab_title $tab_hardstatus
  }
  # called by zsh before showing the prompt
  function precmd()
  {
    eval "tab_title=$TAB_TITLE_PREFIX$TAB_TITLE_PROMPT"
    eval "tab_hardstatus=$TAB_HARDSTATUS_PREFIX$TAB_HARDSTATUS_PROMPT"
    screen_set $tab_title $tab_hardstatus
  }
fi



I have a small suggestion to simplify some of the zshrc stuff above: take advantage of zsh's % escapes. I'm using:

TAB_TITLE_PROMPT='`print -Pn "%~" | sed "s:\([~/][^/]*\)/.*/:\1...:"`'
TAB_HARDSTATUS_PREFIX='`print -Pn "[%~] "`'

to have screen display the same names that zsh does (~ substitution, named directories, etc.). That little bit of sed will shorten things like /usr/local/lib/perl to /usr...perl, and it'll handle paths starting with ~.

I'm also using this:

TAB_TITLE_EXEC='`case $cmd[1]:t in ; "sudo") echo $cmd[2]:t;; *) echo $cmd[1]:t;; esac`'

to avoid putting "sudo" in my window titles. This snippet will put the correct command name in the window title when running via sudo.

--Neh 19:10, 6 August 2007 (UTC)


A bonus is that I get a free silence monitor on all of my running programs. Once it's finished, the application name disappears, so I know as long as I see the name of the application in my list of window names, it isn't done yet.

Here's what it looks like:

Image:Gnu_screen-rampion.1.jpg

and with a triple split (though I rarely even use a single split - this is just to show off the color changing caption):

Image:Gnu_screen-rampion.2.jpg


  • Set up a password in your screenrc: It took me a bit to figure out how to generate an encrypted password. The easiest way I came across was to use the htpasswd utility. Just take it's default encrypted password if you're not looking for something too complex.

Contents

[edit] Switching windows in URxvt: The easy way

File: ~/.screenrc
bindkey "\033\133\066\136" next #CTRL+PageUp
bindkey "\033\133\065\136" prev #Ctrl+PageDown
File: ~/.Xdefaults
!Use Control-Tab to switch windows
URxvt*keysym.Control-Tab: ^An

- TiCL 17:55, 12 February 2007 (UTC)

Thank you so much for providing this howto on screen. Of all the internet, I got the most for GNU screen out of this howto. Thanks again. Regards, Mike (haun.mike at g mail)


Does anyone know how to do this URxvt-like Ctrl+Pg{Dn,Up} scrolling using gnome-terminal or any other terminals? I'd be very curious to learn how!


I run Gentoo at home and on my laptop at work, but the cpu servers run Solaris. The script presented in the "More about attaching to Screen sessions" section does not work on these machines, the sed syntax is different. Yes this is a wiki for Gentoo, but this useful guide could easily apply to any OS running screen. You can get it to work with an awk command though, which is a little simpler than the s/// regex patterns currently in the script. Just replace:

sed -ne 's/[[:space:]]//' -ne 's/\((Attached)\|(Detached)\)// p'

with:

awk '{if(/tached/) print $1}'

Script works great after this change! I think this is what the sed command is trying to achieve? You can probably get the sed command to work as well with some changes I guess, I'm not an expert in sed or awk (or Solaris) but know more about awk than sed to get me in enough trouble...

--Joshr 11:30, 11 July 2007 (UTC)


[edit] Escape Key

Many Thanks for this HOWTO!!! It clarifies things with screen for me especially the escape command was my point of interest. I need to have mine configured when in a serial connection with minicom because minicom also uses C-a and its default Control Key seems not be able to change in such an easy way.

so I use

screen -mS session -e ^Ya which points to C-y.

how would it look in .screenrc File?

--T3kkn0kr4t 09:16, 22 September 2007 (UTC)

Answer: it looks like this: escape "^Y^a"


[edit] Screen and Emerge

Question: How do you get emerge title changing capabilities while using GNU screen?

Currently, I'm using a patch to /usr/lib/portage/pym/output.py. I'm not sure if it's the right solution, but anyway the patch is below. There's an issue (though it's not related to the patch), that for inactive windows title is truncated to ~20 characters. How could we change this default behaviour?

--- output.py.orig      2008-03-07 14:09:46.000000000 +0100
+++ output.py   2008-03-27 17:13:40.000000000 +0100
@@ -237,13 +237,21 @@
                if len(mystr) > max_len:
                        mystr = mystr[:max_len]
                myt=os.environ["TERM"]
-               legal_terms = ["xterm","Eterm","aterm","rxvt","screen","kterm","rxvt-unicode","gnome"]
+               legal_terms = ["xterm","Eterm","aterm","rxvt","kterm","rxvt-unicode","gnome"]
                for term in legal_terms:
                        if myt.startswith(term):
                                if not raw:
                                        mystr = "\x1b]0;%s\x07" % mystr
                                sys.stderr.write(mystr)
                                sys.stderr.flush()
+
+               legal_terms = ["screen"]
+               for term in legal_terms:
+                       if myt.startswith(term):
+                               if not raw:
+                                       mystr = "\x1bk%s\x1b\\" % mystr
+                               sys.stderr.write(mystr)
+                               sys.stderr.flush()
                                break

 default_xterm_title = None

--85.145.237.52 04:44, 28 March 2008 (UTC) VoVaN

[edit] Startup a multi region screen setup from a script or alias ?

I love the multi region screen thingy.. I use it to monitor my system with 3 regions each showing a particular status.

Q:I would like to create a script that startup screen, devides it into 3 regions (with a certain size if possible), and then give each of these regions it's own terminal and terminal name, then startup a certain command in each terminal.

I tried using a few screen -t commands in a script but to no avail..

Personal tools