Guest User

Untitled

a guest
Apr 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. GNUScreen
  2. ---------
  3.  
  4. Screen is to the console what your window manager is to X. It will allow you to have several
  5. buffers or windows running at the same time. You can easily switch from one buffer to another,
  6. copy and paste between them, create new windows and close others, etc. Each new window will
  7. launch a new process defined by the configuration, typically an interactive shell. But that is
  8. not all. Probably the better feature is that it runs as a separated process from the X server,
  9. though, you can get connected to it from a Terminal window running on top of X. We will return
  10. to this topic later.
  11.  
  12. Now we'll going to do a tour though most useful screen features, from basic ones to some cute
  13. configurations that can make your life easier understanding screen.
  14.  
  15. Launching Screen
  16. ----------------
  17.  
  18. This one is easy, from a console type "screen" (without quotes) and press enter. This is a way
  19. to launch screen, we will see later we can do some cool stuff using the screen command, but for
  20. now this is the basic way.
  21.  
  22. Basic usage
  23. -----------
  24.  
  25. First some conventions, as many Unix manuals, <c-X> stands for a keyboard shortcut using the
  26. CONTROL key and the "X" key.
  27. The way to communicate with screen is typing a prefix -<c-a> by default- and then pressing a
  28. key. The <c-a> combination will tell screen that the next key is a screen command, in this way, we will send orders to screen pressing <c-a> and a key. The default keys are nice, they typically follow a mnemotechnical rule, or they have a privileged position in the keyboard for easy access.
  29.  
  30. <c-a> c -> *C*reates a new *C*onsole window
  31. <c-a> n -> *N*ext tab or window
  32. <c-a> <space> -> next tab
  33. <c-a> <Return> -> next tab
  34. <c-a> p -> *P*revious tab
  35. <c-a> S -> *S*plit screen
  36. <c-a> <Tab> -> focus next frame
  37. <c-a> Q -> make current frame the only one
  38. <c-a> K -> *K*ill current application
  39. <c-a> ? -> Shows all commands (really helpful)
  40. <c-a> w -> Shows a list of current running tabs
  41.  
  42. Those are the basics keys used by navigate through the different "tabs" or "windows". They are
  43. pretty easy to use. Suppose you are downloading something with wget. It is boring to watch the
  44. download progress, so you want to do something else. Press <c-a> c to create a new terminal and
  45. launch whatever you want from there. If you want to check the download, just press <c-a> p to go
  46. to the previous tab, and <c-a> n to back to your funny stuff. You can use this features in TUI
  47. mode, or in GUI mode from a terminal window as well.
  48. There are some ways a bit more smart to move to windows:
  49.  
  50. <c-a> # (# being a number), goes to # tab
  51. <c-a> ' will ask for a number, type the number and press enter
  52. <c-a> " will show a menu with a list of all windows, you can browse with j/k ,
  53. <c-n>/<c-p> , and arrows
  54.  
  55. Probably, your listing when <c-a> " will be a bit unintuitive, like
  56.  
  57. 0 bash
  58. 1 bash
  59. 2 bash
  60.  
  61. you can change the name of a given window with <c-a> A.
  62.  
  63. Detaching and reattaching
  64. --------------------------
  65.  
  66. One of greatest screen's feature is the ability to detach itself from the current terminal
  67. application that's running in (xterm, putty or whatever). We said already Screen runs as a
  68. separated process from X, this is why that is so great. If you want to restart the X server, or
  69. some X application makes it to crash, all the programs and processes running on Screen will keep
  70. going. If you are working over a ssh connection and you suddenly suffer network problems, the
  71. processes you started from Screen over ssh will keep going too. If you have no problem at all
  72. but for some reason you want to detach your terminal and reattach it from another place, Screen
  73. will let you do it.
  74. We can see an example, the usual work flow when using ssh should be:
  75.  
  76. Connect to the remote machine (using ssh, putty,...). Then start a screen session. Open as many
  77. consoles as you want. If your connection falls, normally you would loose all unsaved changes and
  78. current processes would be killed in a rude way. Not to talk if you were compiling/executing
  79. something that would last several hours...So much work lost!
  80.  
  81. That's what you could think at first, but screen has a very powerful feature
  82. called 'detach'. When something goes wrong with your connection, or you click
  83. that 'X' at the corner window, screen just detaches itself from the containing
  84. window, but none of your work is lost. It keeps running at the background. you
  85. can try playing some music with mplayer, and closing the terminal containing it
  86. and keep listening music.
  87.  
  88. When you're on a system without X, and you have no 'x' to click (or nested
  89. screens), you can detach screen with the next command:
  90.  
  91. <c-a> d -> *d*etach screen
  92.  
  93. Now if you want to recover (reattach) your screen session you just open a
  94. terminal and enter the command:
  95.  
  96. screen -r
  97.  
  98. I personally use screen -DR that forces a reattach and it even detaches it if
  99. it was active somewhere else.
  100.  
  101. If you have more than one screen detached, you have to provide info about
  102. which screen session you want to recover.
  103.  
  104. kidd@raymobil [ ~ ] %screen -ls
  105. There are screens on:
  106. 2945.pts-0.raymobil (Attached)
  107. 1558.pts-8.raymobil (Detached)
  108. 2 Sockets in /tmp/uscreens/S-kidd.
  109.  
  110. Then you can decide which one you want.
  111.  
  112. screen -r 29
  113.  
  114. will do the job. (unique substring)
  115.  
  116. Monitoring
  117. ----------
  118.  
  119. Screen can monitor your consoles for both activity and silence.
  120.  
  121. <c-a> M will note you when there's any activity on the current console. Great
  122. for IM.
  123.  
  124. <c-a> _ will monitor for 30 secs of silence in the current console. Great for
  125. long compilations.
  126.  
  127. Copy mode
  128. ---------
  129.  
  130. With screen you can copy and paste text between buffers in the same session.
  131. It's done only with the keyboard (pretty useful if you have to paste text from
  132. a terminal to an IRC channel to get help on how to recover your X).
  133.  
  134. <c-a> <esc> -> enters 'copy mode'.
  135.  
  136. In copy mode, you can move around your window with hjkl (vi movement keys), arrow keys, or
  137. page-up and page-down. Once in copy mode, press <space> to start the selection. Use the movement
  138. keys to select the desired text and press <space> again to exit the selection mode. Now you
  139. have the text copied and ready to be pasted where you want.
  140.  
  141. <c-a> ] pastes the copied text onto current buffer.
  142.  
  143. screen provides a way so search text in the scroll buffer, guess what? it's the
  144. vi style ;)
  145.  
  146. c-a <esc> ?text<Return>
  147.  
  148. Options, binds and beautifying
  149. ------------------------------
  150.  
  151. screen has lots of features that can be activated interactively or through your
  152. $HOME/.screenrc
  153.  
  154. To set options interactively, the command is
  155. <c-a> :option
  156.  
  157. for example:
  158.  
  159. <c-a> :select 0 #goes to first console
  160.  
  161. my ~/.screenrc looks more or less like that:
  162.  
  163. shell /bin/zsh #I want zsh when openning new tabs
  164.  
  165. # caption and hardstatus are 'status bars' for screen. I took them somewhere on
  166. # the web, look at the screen man page for info on meta-characters
  167.  
  168. caption always "%{wb}%n%f %t %{wk} %?%-Lw%?%{wb}[%n*%f %t]%?(%u)%?%{wk}%?%+Lw%? %{wk}"
  169. hardstatus alwayslastline "%{+b kr}host: %{kw}%H%{kg} %c - %D %d.%m.%Y %{kr}(load: %l) %-26=%1`"
  170.  
  171. # you can bind keys to actions, even executing external programs.
  172. bind o only
  173. bind s split
  174.  
  175. Making screen collaborate with other apps
  176. -----------------------------------------
  177.  
  178. If you use zsh as your default shell, you can make zsh and screen communicate to
  179. each other and make screen set titles tied to zsh title (bash can't change the window/teminal
  180. title dynamically AFAIK).
  181.  
  182. You should paste the next code in your ~/.zshrc . Keep in mind you may have to tweak the code to match your terminal emulator.
  183.  
  184. #screen integration to set caption bar dynamically
  185. function title {
  186. if [[ $TERM == "screen" || $TERM == "screen.linux" ]]; then
  187. # Use these two for GNU Screen:
  188. print -nR $'\033k'$1$'\033'\\\
  189.  
  190. print -nR $'\033]0;'$2$'\a'
  191. elif [[ $TERM == "xterm" || $TERM == "urxvt" ]]; then
  192. # Use this one instead for XTerms:
  193. print -nR $'\033]0;'$*$'\a'
  194. fi
  195. }
  196.  
  197. function precmd {
  198. title zsh "urxvt $PWD"
  199. echo -ne '\033[?17;0;127c'
  200. }
  201.  
  202. function preexec {
  203. emulate -L zsh
  204. local -a cmd; cmd=(${(z)1})
  205. if [[ $cmd[1]:t == "ssh" ]]; then
  206. title "@"$cmd[2] "urxvt $cmd"
  207. elif [[ $cmd[1]:t == "sudo" ]]; then
  208. title "#"$cmd[2]:t "urxvt $cmd[3,-1]"
  209. elif [[ $cmd[1]:t == "for" ]]; then
  210. title "()"$cmd[7] "urxvt $cmd"
  211. elif [[ $cmd[1]:t == "svn" ]]; then
  212. title "$cmd[1,2]" "urxvt $cmd"
  213. else
  214. title $cmd[1]:t "urxvt $cmd[2,-1]"
  215. fi
  216. }
  217.  
  218. Acknowledgements
  219. ----------------
  220.  
  221. Thanks to #screen channel at freenode, people publishing their zshrc, and zsh maillist. If you find your snippets here and you want credit, don't hesitate to add yourself here, or notify it in #vectorlinux @ freenode.
Add Comment
Please, Sign In to add comment