GNU屏幕生存指南

什么人认为不仅是你需要知道的关于优秀的屏幕工具的基本的东西,而且你认为值得从头开始教一个初学者的东西呢?

我刚刚向Screen介绍了一位朋友,他们很难适应。 所以比喻和方便的提示记住绑定等将是真棒。

我一直在使用屏幕超过10年,可能使用不到一半的function。 所以绝对没有必要马上学习所有的function(我不推荐尝试)。 我的日常命令是:

^A ^W - window list, where am I ^A ^C - create new window ^A space - next window ^A p - previous window ^A ^A - switch to previous screen (toggle) ^A [0-9] - go to window [0-9] ^A esc - copy mode, which I use for scrollback 

我想就是这样。 我有时使用分屏function,但肯定不是每天。 另一个提示是,如果屏幕似乎已经locking,因为你偶然碰到一些随机的组合键,同时做^ Q和^ A ^ Q试图解锁它。

我不能习惯屏幕,直到我find一种方法来设置屏幕底部的“状态栏”,显示您正在使用的“标签”或“虚拟屏幕”以及其他的屏幕。 这是我的设置:

 [roel@roel ~]$ cat .screenrc # Here comes the pain... caption always "%{=b dw}:%{-b dw}:%{=b dk}[ %{-b dw}%{-b dg}$USER%{-b dw}@%{-b dg}%H %{=b dk}] [ %= %?%{-b dg}%-Lw%?%{+b dk}(%{+b dw}%n:%t%{+b dk})%?(%u)%?%{-b dw}%?%{-b dg}%+Lw%? %{=b dk}]%{-b dw}:%{+b dw}:" backtick 2 5 5 $HOME/scripts/meminfo hardstatus alwayslastline "%{+b dw}:%{-b dw}:%{+b dk}[%{-b dg} %0C:%s%a %{=b dk}]-[ %{-b dw}Load%{+b dk}:%{-b dg}%l %{+b dk}] [%{-b dg}%2`%{+b dk}] %=[ %{-b dg}%1`%{=b dk} ]%{-b dw}:%{+b dw}:%<" sorendition "-b dw" [roel@roel ~]$ cat ~/scripts/meminfo #!/bin/sh RAM=`cat /proc/meminfo | grep "MemFree" | awk -F" " '{print $2}'` SWAP=`cat /proc/meminfo | grep "SwapFree" | awk -F" " '{print $2}'` echo -n "${RAM}kb/ram ${SWAP}kb/swap" [roel@roel ~]$ 

Ctrl + A ? – 显示帮助屏幕!

如果你的朋友习惯按^A到bash的开头,他/她会有一些惊喜,因为^A是屏幕命令键绑定。 通常我最终会冻结屏幕,可能是因为我在^A之后按了一些随机键:-)

在这些情况下,我尝试

^A s^A q块/ unblockterminal滚动

解决这个问题。 要进入屏幕内的一行的开始,按键顺序是^A a

你可以将ctrl-A中的转义关键字重新映射成你所select的另一个关键字,所以如果你使用它作为别的东西,例如在bash中的行首,你只需要添加一行到你的〜 /.screenrc文件。 为了使^ b或^ B使用:

 escape ^bB 

从命令行使用名称会话来控制多个会话。 每个任务使用一个会话,每个会话都有多个选项卡:

   screen -ls列出您当前的屏幕会话
  屏幕-S <名称>创build一个名为名称的新屏幕会话
  屏幕-r <名称>连接到指定的屏幕会话

使用屏幕时,只需要几个命令:

   ^ c创build一个新的shell
   ^ A [0-9]开关shell
   ^ K杀死当前的shell
   ^ d从屏幕断开连接
   ^ A? 显示帮助

一个很好的快速参考可以在这里find。 值得加书签。

对于那些熟悉屏幕的人来说,一些技巧可能会让他们忘记他们在手册页中读到的东西:

  • 要更改屏幕窗口的名称非常简单: ctrl + A shift + A。
  • 你有没有错过屏幕上的最后一条消息? ctrl + a ctrl + m会再次显示给你。
  • 如果你想运行一些东西(比如拖拽一个文件),并且有一个屏幕告诉你什么时候有变化,在目标窗口上使用ctrl + A shift + m 。 警告:它会让你知道是否有任何改变。
  • 要直接select窗口15吗? 试试这些在你的.screenrc文件中:
 bind ! select 11 bind @ select 12 bind \# select 13 bind $ select 14 bind % select 15 bind \^ select 16 bind & select 17 bind * select 18 bind ( select 19 bind ) select 10 

为窗口10到19分配Ctrl + Shift + 0到9

http://www.debian-administration.org/articles/34

我在几年前写了这样的一篇文章,但是这篇文章还是一个很好的介绍,得到了很多正面的反馈。

我“必须”添加这个:添加

 bind s 

到你的.screenrc ,如果你 – 像我一样 – 用来使用分割窗口,就像Ca S分割实际的窗口一样,但Ca s冻结它。 所以我只是禁用冻结快捷方式。

ctrl + a是一个特殊的键。

ctrl + a d – [d] etach,让程序(irssi?)在后台,回家。

ctrl + a c [c]重新创build一个新窗口ctrl + a 0-9在窗口之间按数字切换

屏幕-r – 回到分离的会话

这涵盖了90%的用例。 不要试图在单一时间显示所有的function。

Ctrl + A是基本命令

Ctrl + A N =进入*** ***分机屏幕

Ctrl + A P =进入下一个屏幕

Ctrl + A C = *** C *** reate新的屏幕

Ctrl + A D = *** D *** etach你的屏幕

并非真正必要的不是单纯的与屏幕相关,而是在我的terminal中使用256色,GNU屏幕和Vim改善了我的屏幕体验(尤其是因为我每天大约8小时在Vim中编程 – 这里有一些非常好看的颜色scheme)。

在下一个版本的Ubuntu服务器中,默认情况下会发生一些有趣的工作 ,包括使用屏幕的底部来显示所有的窗口以及其他有用的机器细节(比如#更新可用以及机器是否需要重新启动)。 你可以抓住他们的.screenrc并根据你的需要进行定制。

我的.screenrc中最有用的命令如下

 shelltitle "$ |bash" # make screen assign window titles automatically hardstatus alwayslastline "%w" # show all window titles at bottom line of term 

这样我总是知道窗户是开着的,现在还在里面跑着什么。

我对.screenrc的第一个修改是改变escape命令。 与你们许多人不一样,我不喜欢默认的Ctrl-A序列,因为它几乎在所有其他环境中都干扰了这个基本的function。 在我的.screenrc文件中,我添加:

逃避

这是反面的。

这使我可以使用反引号作为转义键(例如创build一个新的屏幕,我按反向-c,分离是反向-d,反向 – 是帮助,反向反向是前一个屏幕等)。 它干扰的唯一方式(我不得不打破自己的习惯)是在命令行上使用反向(backtick)来捕获执行输出,或者粘贴任何包含反引号的东西。 对于前者,我通过使用BASH $(command)约定修改了我的习惯。 对于后者,我通常只是popup打开另一个xterm或从屏幕上分离,然后粘贴包含反引号的内容。 最后,如果我想插入一个文字反引号,我只需要按反向键。

我不记得是谁偷了这个(dotfile.org上的人)。 我为ssh稍微修改了一下:

 #!/bin/sh # scr - Runs a command in a fresh screen # # Get the current directory and the name of command wd=`pwd` cmd=$1 shift # We can tell if we are running inside screen by looking # for the STY environment variable. If it is not set we # only need to run the command, but if it is set then # we need to use screen. if [ -z "$STY" ]; then $cmd $* else # Screen needs to change directory so that # relative file names are resolved correctly. screen -X chdir $wd # Ask screen to run the command if [ $cmd == "ssh" ]; then screen -X screen -t ""${1##*@}"" $cmd $* else screen -X screen -t "$cmd $*" $cmd $* fi fi 

然后我设置下面的bash别名:

 vim() { scr vim $* } man() { scr man $* } info() { scr info $* } watch() { scr watch $* } ssh() { scr ssh $* } 

它为上面的别名打开一个新的屏幕,如果使用ssh,它将使用ssh主机名重命名屏幕标题。

干杯z0mbix

我喜欢用窗口的描述性名称来设置屏幕会话。 ^ a A会让你给当前窗口一个名字,^ a会给你一个你的窗口列表。完成后,用^ ad分离屏幕并用屏幕重新连接-R

有人在服务器故障上发布了类似的问题。

我喜欢使用screen -d -RR来自动创build/附加给定的屏幕。 我创build了bash函数,使其更容易…

 function mkscreen { local add=n if [ "$1" == '-a' ]; then add=y shift; fi local name=$1; shift; local command="$*"; if [ -z "$name" -o -z "$command" ]; then echo 'Usage: mkscreen [ -a ] name command -a Add to .bashrc.' 1>&2; return 1; fi if [ $add == y ]; then echo "mkscreen $name $command" >> $HOME/.bashrc; fi alias $name="/usr/bin/screen -d -RR -S $name $command"; return 0; } function rmscreen { local delete=n if [ "$1" == '-d' ]; then delete=y shift; fi local name=$1; if [ -z "$name" ]; then echo 'Usage: rmscreen [ -d ] name -d Delete from .bashrc.' 1>&2; return 1; fi if [ $delete == y ]; then sed -i -r "/^mkscreen $name .*/d" $HOME/.bashrc; fi unalias $name; return 0; } 

他们为/usr/bin/screen -d -RR -S $name $command创build一个别名。 例如,我喜欢在屏幕会话中使用irssi,所以在我的.bashrc(在这些函数之下),我有:

 mkscreen irc /usr/bin/irssi 

然后我可以在terminalinputirc来进入irssi。 如果屏幕“irc”不存在,则会创build并从/ usr / bin / irssi运行(当然会自动连接)。 如果它已经运行,那么我只是重新附加到它,强行分离已经附加到它的任何其他实例。 这很好。

另一个例子是为perldocs创build临时屏幕别名,当我遇到他们:

 mkscreen perlipc perldoc perlipc perlipc # Start reading the perldoc, ^A d to detach. ... # Later, when I'm done reading it, or at least finished # with the alias, I remove it. rmscreen perlipc 

-a选项(必须是第一个参数)将屏幕别名附加到.bashrc(所以它是持久的),-d将其删除(这些可能具有破坏性,因此请自担风险)。 的xD

附加:

在屏幕上工作很多时,我觉得很方便:

 alias sls='/usr/bin/screen -ls' 

这样你就可以用很less的按键来列出你的屏幕。 我不知道sls是否与任何现有的工具碰撞,但是在我的系统上没有,所以我去了。

^ AA切换回刚刚来自的屏幕。

^ A对于UNIX用户来说是一个非常特殊的字符,但是如果你使用屏幕与OpenVMS交谈,那么不能^ A过早地让你变成秃头。 在VMS中,如果在执行前从历史logging缓冲区编辑DCL命令,插入模式closures(必须有几个原因,我不会进入这里)。要打开它,键入你的命令而不是空间的东西,你必须击中^ A