GNU屏幕生存指南

GNU屏幕生存指南

GNU Screen Survival Guide

人们认为,关于优质的Screen实用程序,您不仅需要了解必要的知识,而且还需要从根本上教会初学者的知识?

我刚刚向屏幕介绍了一位朋友,他们很难适应它。 因此,记住绑定等的类比和方便的技巧会很棒。


我使用屏幕已有10多年了,可能只使用了不到一半的功能。因此,绝对没有必要立即学习其所有功能(并且我不建议尝试)。我的日常命令是:

1
2
3
4
5
6
7
^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

我想就是这样。我有时会使用分屏功能,但肯定不是每天使用。另一个提示是,如果屏幕似乎由于意外击中了一些随机的按键组合而被锁定,请同时执行^ Q和^ A ^ Q来尝试将其解锁。


直到我找到一种在屏幕底部设置"状态栏"的方法来显示您正在使用的"选项卡"或"虚拟屏幕"以及还有哪些其他方法之后,我才能习惯于进行屏幕显示。这是我的设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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吗? -显示帮助屏幕!


如果您的朋友习惯按bash到达行首,那么他/她会有些意外,因为^A是屏幕命令键绑定。通常我最终会死机,这可能是由于^A之后我按了一些随机键造成的:-)

在那种情况下,我尝试

^A s^A q阻止/取消阻止终端滚动

解决它。要转到屏幕内一行的开头,按键顺序为^A a


您可以将ctrl-A中的转义键重新映射为您选择的另一个键,因此,如果您将其用于其他用途,例如要在bash中转到该行的开头,只需在?/ .screenrc文件中添加一行。要使其变为^ b或^ B,请使用:

1
escape ^bB

在命令行中,使用名称会话来控制多个会话。我为每个任务使用一个会话,每个会话都有多个标签:

1
2
3
  screen -ls                lists your current screen sessions
  screen -S <name>          creates a new screen session called name
  screen -r <name>          connects to the named screen sessions

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

1
2
3
4
5
  ^A c          create a new shell
  ^A [0-9]      switch shell
  ^A k          kill the current shell
  ^A d          disconnect from screen
  ^A ?          show the help

此处提供了出色的快速参考。值得收藏。


针对那些熟悉屏幕但又不记得在手册页中阅读的内容的人的一些提示:

  • 更改屏幕窗口的名称非常容易:ctrl + A shift + A
  • 您是否错过了屏幕上的最后一条消息? ctrl + a ctrl + m会再次为您显示。
  • 如果您想运行某些内容(例如尾随文件)并在有更改时让屏幕告诉您,请在目标窗口上使用ctrl + A shift + m。警告:如果有任何变化,它将通知您。
  • 是否要直接选择窗口15?在您的.screenrc文件中尝试这些:
1
2
3
4
5
6
7
8
9
10
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

这将ctrl + a shift + 0至9分配给Windows 10至19。


ctrl + a是一个特殊密钥。

ctrl + a d-[d]执行,将程序(irssi?)留在后台,然后回家。

ctrl + a c 创建新窗口
ctrl + a 0-9按编号在窗口之间切换

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

这涵盖了90%的用例。不要试图一次显示所有功能。


我"必须"添加此内容:添加

1
bind s

.screenrc,如果您(像我一样)曾经使用拆分窗口,因为C-a S拆分了实际窗口,但C-a S冻结了它。因此,我只是禁用了冻结快捷方式。


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

我是几年前写的,但是它仍然是一个很好的介绍,得到了很多积极的反馈。


Ctrl + A是基本命令

Ctrl + A N =转到*** N ***下一屏

Ctrl + A P =转到*** P ***上一个屏幕

Ctrl + A C = *** C ***创建新屏幕

Ctrl + A D = *** D ***触摸屏幕


不仅与屏幕无关,而且在终端中启用256色不是很重要,GNU Screen和Vim极大地改善了我的屏幕体验(特别是因为我每天在Vim中编写代码的时间约为8小时-有一些很棒的对眼睛友好的色彩设计)。


我不记得是谁偷的(dotfile.org上的某人)。我已经对ssh进行了一些修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/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别名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vim() {
        scr vim $*
}

man() {
        scr man $*
}

info() {
        scr info $*
}

watch() {
        scr watch $*
}

ssh() {
        scr ssh $*
}

它使用ssh打开上述别名和iff的新屏幕,并使用ssh主机名重命名屏幕标题。

干杯z0mbix


我对.screenrc的第一个修改是更改转义命令。与你们中的许多人一样,我不喜欢默认的Ctrl-A序列,因为它几乎在所有其他情况下都干扰了基本功能。在我的.screenrc文件中,添加:

逃脱

那是反推。

这使我能够将反引号用作转义键(例如,创建一个新屏幕,我按backtick-c,detach是backtick-d,backtick-?是帮助,backtick-backtick是上一个屏幕,等等)。干扰的唯一方法(我不得不戒掉习惯)是在命令行上使用反引号捕获执行输出,或者粘贴任何包含反引号的内容。对于前者,我通过使用BASH $(command)约定来改变了习惯。对于后者,我通常只是弹出另一个xterm或与屏幕分离,然后粘贴包含反引号的内容。最后,如果要插入文字反引号,只需按backtick-e。


在下一版本的Ubuntu Server中,默认情况下会进行一些有趣的工作以获取良好的GNU屏幕设置,其中包括使用屏幕底部显示所有窗口以及其他有用的机器详细信息(例如#更新)可用以及计算机是否需要重新启动)。您可能可以抓住他们的.screenrc并根据需要对其进行自定义。

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

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

这样,我总是知道打开了哪些窗口,以及当前正在运行的窗口。


我想为屏幕设置一个带有描述性名称的屏幕会话。 ^ a A将为您提供当前窗口的名称,而^ a"将为您提供窗口列表。
完成后,用^ a d拆开屏幕,然后用屏幕-R重新连接


我喜欢使用screen -d -RR自动创建/附加到给定的屏幕。我创建了bash函数以使其更容易...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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创建别名。例如,我喜欢在屏幕会话中使用irssi,因此在我的.bashrc(在这些功能下)中,我具有:

1
mkscreen irc /usr/bin/irssi

然后,我可以在终端中键入irc进入irssi。如果屏幕" irc"尚不存在,则将创建该屏幕并从中运行/ usr / bin / irssi(当然会自动连接)。如果它已经在运行,那么我将重新连接到它,强行分离已经连接到它的任何其他实例。很好

另一个示例是在遇到Perldocs时为其创建临时屏幕别名:

1
2
3
4
5
6
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

附加:

当我在屏幕上进行很多工作时,我发现另一种bash-ism很方便:

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

这样,您可以用更少的击键列出屏幕。我不知道sls是否与任何现有实用程序冲突,但是当时我的系统上没有,所以我选择了它。


^ A对于UNIX用户来说是一个很棒的特殊字符,但是如果您使用屏幕与OpenVMS进行对话,则无法使用^ A将使您过早地秃头。 在VMS中,如果您在从历史记录缓冲区执行之前编辑DCL命令,则"插入"模式已关闭(出于某些原因,我不会在这里介绍)..将其打开,这样您就不会过分 键入命令而不是空格,您必须按^ A


^ A A切换回您刚来自的屏幕。


推荐阅读