七种武器之screen

终端操作的利器, 当你远程ssh到一台主机的时候, 就深刻体验到screen的好处了.

screen常用的键位(前缀键已经改为C-z):

功能 键位
卷屏 C-z [进入copy mode,按q退出
移动光标 卷屏后,使用C-bC-f/、C-nC-p移动光标,类似vi
复制粘贴 卷屏后,按空格键开始复制,移动光标选择复制内容后,再次按空格键完成复制,然后使用C-z ]粘贴
搜索 卷屏后,使用 /? 向下和向上搜索,类似 vi
更改tab标题 C-z A
分屏 C-z S
切换分屏 C-z C-I
退出分屏 C-z Q
锁屏 C-z x
后台运行 C-z d 后detach,在终端里可以 screen -r 再重新恢复 session
命令帮助 C-z ?

我的 .screenrc 文件:

# skip the startup message
startup_message off
# encoding
defutf8 on
# go to home dir
# chdir
# Automatically detach on hangup
autodetach on
# Change default scrollback value for new windows
defscrollback 5000
# start with visual bell as default
vbell on
vbell_msg "bell on %t (%n)"
# The vt100 description does not mention "dl". *sigh*
termcapinfo vt100 dl=5\E[M
termcapinfo xterm*|xterms|xs|rxvt ti@:te@
altscreen on
# Enable 256 color terminal
attrcolor b ".I"
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
defbce "on"
# Monitor windows, funcy feature
defmonitor on
activity ""
defhstatus "^Et | $USER@^EH"
# look and feel
caption always
#caption string "%{+b rk}%H%{gk} |%c %{yk}%d.%m.%Y | %72=Load: %l %{wk}"
caption string " %-Lw%{= d.}%50>%n%f %t%{-}%+Lw%< %=|%{+b rk} %H %{gk}%0c:%s "
#hardstatus alwayslastline
#hardstatus string "%?%{yk}%-Lw%?%{wb}%n*%f %t%?(%u)%?%?%{yk}%+Lw%?"
hardstatus string "%?[%H] %n%f %t%?"
shelltitle '$ |sh'
# Toggle 'fullscreen' or not.
bind f eval "caption splitonly" "hardstatus ignore"
bind F eval "caption always" "hardstatus ignore"
escape ^Zz
view raw .screenrc hosted with ❤ by GitHub

如果设置 hardstatus alwayslastline,则无法在xterm上显示动态标题,有人认为这是一个bug,所以我在配置文件里取消了这个设置,而将caption用来显示打开的终端列表。另外,设置了快捷键f/F用来隐藏和显示这些额外信息。

启动后台进程

一般可以使用 nohup 或者在命令最后加 &,把进程放置到后台。但借助screen可以有更美好的方式:

screen -dmS <session-name> /path/to/program

其中,参数-dm表示直接detach方式创建新session,参数-S指定新session的名字。 同样,后续可以直接用screen -r <session-name>恢复session。

      加载更多