从 shellscript 设置屏幕标题

发布于 2024-08-10 11:19:19 字数 187 浏览 6 评论 0原文

是否可以使用 shell 脚本设置屏幕标题?

我考虑过发送按键命令 ctrl+A shift-A Name enter

我搜索了大约一个小时如何在 shell 脚本中模拟击键,但没有找到答案。

Is it possible to set the Screen Title using a shell script?

I thought about something like sending the key commands ctrl+A shift-A Name enter

I searched for about an hour on how to emulate keystrokes in an shell script, but didn't find the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

时光是把杀猪刀 2024-08-17 11:19:19

您可以使用以下行设置屏幕/ xterm 标题:

#!/bin/bash

mytitle="Some title"
echo -e '\033k'$mytitle'\033\\'

[更新] - 根据请求,我还包括下面 @Espo 提出的解决方案:

根据您的 xterm 版本或您的 Linux 发行版,上面的行可能会也可能不会工作,并且您可以尝试 xterm-defaults:

#!/bin/bash

mytitle="Some title"
echo -e '\033]2;'$mytitle'\007'

有关详细信息,请参阅: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 或参考下面 @Espo 的答案。

You can set the screen / xterm title using the following lines:

#!/bin/bash

mytitle="Some title"
echo -e '\033k'$mytitle'\033\\'

[UPDATE] - by request I'm also including the solution proposed by @Espo below:

Depending on your xterm version or your linux distribution the line above may or may not work and you can try the xterm-defaults:

#!/bin/bash

mytitle="Some title"
echo -e '\033]2;'$mytitle'\007'

For more on the details see: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 or refer to the answer by @Espo below.

一梦浮鱼 2024-08-17 11:19:19

来自 http://www.faqs.org/docs/Linux- mini/Xterm-Title.html#s3

xterm 转义序列

窗口和图标标题可能会更改
在运行的 xterm 中使用 XTerm
转义序列。下列
序列在这方面很有用:

  • ESC]0;stringBEL -- 将图标名称和窗口标题设置为字符串
  • ESC]1;stringBEL -- 将图标名称设置为字符串
  • ESC]2;stringBEL -- 将窗口标题设置为字符串

其中 ESC 是转义字符
(\033),BEL 是响铃字符
(\007).

在其中打印这些序列之一
xterm 将导致窗口或
图标标题要更改。

注意:这些序列适用于大多数
xterm 衍生物,例如 nxterm,
color-xterm 和 rxvt。其他终端
类型经常使用不同的转义符;看
附录中的示例。对于
xterm 转义序列的完整列表
查看文件ctlseq2.txt,该文件来自
与 xterm 分布,或
xterm.seq,随 rxvt 一起提供
分布。

打印转义序列

获取恒定的信息
在这个外壳的整个生命周期中,
例如主机和用户名,它将
足以简单地呼应逃逸
shell rc 文件中的字符串:

 echo -n "\033]0;${USER}@${HOST}\007"

应该产生一个类似的标题
用户名@主机名,假设是shell
设置变量 $USER 和 $HOST
正确。所需的选项为
echo 可能因外壳而异(参见示例
如下)。

有关期间可能发生变化的信息
shell 的生命周期,例如当前
工作目录,这些转义符
确实每次都需要应用
提示发生变化。这样一来
每个命令都会更新字符串
您发出并可以跟踪
当前工作等信息
目录、用户名、主机名等
一些 shell 提供特殊功能
为此,有些人不这样做,而我们
必须插入标题序列
直接进入提示字符串。这
下一节将对此进行说明。

From http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3

xterm escape sequences

Window and icon titles may be changed
in a running xterm by using XTerm
escape sequences. The following
sequences are useful in this respect:

  • ESC]0;stringBEL -- Set icon name and window title to string
  • ESC]1;stringBEL -- Set icon name to string
  • ESC]2;stringBEL -- Set window title to string

where ESC is the escape character
(\033), and BEL is the bell character
(\007).

Printing one of these sequences within
the xterm will cause the window or
icon title to be changed.

Note: these sequences apply to most
xterm derivatives, such as nxterm,
color-xterm and rxvt. Other terminal
types often use different escapes; see
the appendix for examples. For the
full list of xterm escape sequences
see the file ctlseq2.txt, which comes
with the xterm distribution, or
xterm.seq, which comes with the rxvt
distribution.

Printing the escape sequences

For information that is constant
throughout the lifetime of this shell,
such as host and username, it will
suffice to simply echo the escape
string in the shell rc file:

    echo -n "\033]0;${USER}@${HOST}\007"

should produce a title like
username@hostname, assuming the shell
variables $USER and $HOST are set
correctly. The required options for
echo may vary by shell (see examples
below).

For information that may change during
the shell's lifetime, such as current
working directory, these escapes
really need to be applied every time
the prompt changes. This way the
string is updated with every command
you issue and can keep track of
information such as current working
directory, username, hostname, etc.
Some shells provide special functions
for this purpose, some don't and we
have to insert the title sequences
directly into the prompt string. This
is illustrated in the next section.

自由如风 2024-08-17 11:19:19

以下是编写屏幕标题重命名脚本的其他方法:

将以下设置添加到 .ssh/config 可以在使用 SSH 登录系统时自动设置屏幕标题:

Host *
  PermitLocalCommand yes
  LocalCommand [ "$TERM" == 'screen' ] && echo -ne "\033k%h\033\\" 

而不是 %h,代表您要连接的机器的主机名,您可以使用%n,这是您用于连接到机器的实际名称/别名。

注意:您需要 OpenSSH >= v5.1 才能使用本地主机 %n 和 %h 参数。查看“man ssh_config”以获取有关 LocalCommand 的更多信息。

要自动将标题恢复为本地主机的主机名,在关闭 SSH 会话后,您可以在 .bashrc 中的提示符变量 PS1 中添加转义序列:

export PS1='you_favorite_PS1_here'
if [ "$TERM" == 'screen' ]; then
    export PS1=${PS1}'\[\033k\h\033\\\]'
fi

当使用 .screenrc 配置显示当前正在哪个屏幕“选项卡”中工作时,这些技巧特别有用。将如下内容添加到 .screenrc 中以使其正常工作:

caption always "%{= kY}%-w%{= Yk}%n %t%{-}%+w%{ kG} %-= @%H - %LD %d %LM - %c"

The following are other ways to script the renaming of screen titles:

Adding the following settings to .ssh/config sets the screen title automatically upon logging in to a system using SSH:

Host *
  PermitLocalCommand yes
  LocalCommand [ "$TERM" == 'screen' ] && echo -ne "\033k%h\033\\" 

Instead of %h, which represents the hostname of the machine you are connecting with, you may use %n, which is the actual name / alias you used to connect to the machine.

NOTE: You need OpenSSH >= v5.1 to be able to use the Localhost %n and %h parameters. Check out 'man ssh_config' for more info on LocalCommand.

To automatically revert the title, back to that of the hostname of the localhost, after closing the SSH session, you can add an escape sequence to you prompt variable PS1 in .bashrc :

export PS1='you_favorite_PS1_here'
if [ "$TERM" == 'screen' ]; then
    export PS1=${PS1}'\[\033k\h\033\\\]'
fi

These tricks are especially useful when using a .screenrc config that shows you in what screen 'tab' you are currently working. Add something like the following to .screenrc to get this working:

caption always "%{= kY}%-w%{= Yk}%n %t%{-}%+w%{ kG} %-= @%H - %LD %d %LM - %c"
岛徒 2024-08-17 11:19:19

尝试下面的命令,不需要编辑任何文件或配置,如~/.bashrc,可以在运行时使用。

将静态文本设置为标题:(我的标题)

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

将本地/全局变量设置为标题:($USER)

export PS1='\[\e]0;$USER\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

将命令输出设置为标题:(主机名)

export PS1='\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

设置为默认值(恢复):

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Try the below commands, no need to edit any file or configuration like ~/.bashrc, Can be used at runtime.

Set static text as title: (My Title)

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set local/global variable as title: ($USER)

export PS1='\[\e]0;$USER\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set command output as title: (hostname)

export PS1='\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set to default (Revert back):

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
猫卆 2024-08-17 11:19:19
set_screen_title ()
{
    echo -ne "\ek$1\e\\"
}
set_screen_title ()
{
    echo -ne "\ek$1\e\\"
}
笑饮青盏花 2024-08-17 11:19:19

您还可以调用 screen 并告诉它设置标题:

screen -X title "new title"

如果您位于屏幕窗口中,它将设置该窗口的名称。如果您不在屏幕中,它将设置最近打开的窗口的名称。

You can also call screen and tell it to set a title:

screen -X title "new title"

If you're in a screen window, it will set that window's name. If you're not in screen, it will set the most recently opened window's name.

我是有多爱你 2024-08-17 11:19:19

要添加到 Espo 的答案中,xterm 转义序列也可以应用于 Bash PS1 变量

ESC]0;stringBEL -- Set icon name and window title to string
ESC]1;stringBEL -- Set icon name to string
ESC]2;stringBEL -- Set window title to string

示例

PS1='\e]0;string\a'

To add to Espo's answer, the xterm escape sequences can also be applied to the Bash PS1 variable

ESC]0;stringBEL -- Set icon name and window title to string
ESC]1;stringBEL -- Set icon name to string
ESC]2;stringBEL -- Set window title to string

Example

PS1='\e]0;string\a'
梦在深巷 2024-08-17 11:19:19

要在使用 ssh 跳转时启用自动标题更新,请将其添加到 ~/.bashrc

ssh() {
  echo -n -e "\033k$1\033\\"
  /usr/bin/ssh "$@"
  echo -n -e "\033k`hostname -s`\033\\"
}
echo -n -e "\033k`hostname -s`\033\\"

请参阅 http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html

To enable automatic title updating when jumping around with ssh, add this to ~/.bashrc:

ssh() {
  echo -n -e "\033k$1\033\\"
  /usr/bin/ssh "$@"
  echo -n -e "\033k`hostname -s`\033\\"
}
echo -n -e "\033k`hostname -s`\033\\"

See http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html

绻影浮沉 2024-08-17 11:19:19
 # add the following in your ~/.bashrc or ~/.bash_profile
 PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'

或者更好地从 此处

 # add the following in your ~/.bashrc or ~/.bash_profile
 PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'

or even better copy the whole concept for customizing your bash configs between a lot of hosts from here

温馨耳语 2024-08-17 11:19:19

我对这个问题的解决方案是创建一个 bash 脚本并将其添加到我的 ~/.bashrc 文件中:

set-title() {
  ORIG==$PS1
  TITLE="\e];$@\a"
  PS1=${ORIG}${TITLE}
}

现在,当我在任何 bash shell 会话中时,我键入“set-titledesired_title”,它会更改为“desired title”。
这适用于 Ubuntu 的多个版本,目前在 Kinetic 16.04 上,

我从 此处 获得了此解决方案。我再次寻找它,找不到它,并认为我会将其发布在这里供任何感兴趣的人使用。

My solution to this problem was to create a bash script and add it to my ~/.bashrc file:

set-title() {
  ORIG==$PS1
  TITLE="\e];$@\a"
  PS1=${ORIG}${TITLE}
}

Now when I'm in any bash shell session, I type "set-title desired_title" and it changes to "desired title".
This works for multiple versions of Ubuntu, currently on Kinetic 16.04

I got this solution from here. I was looking for it again, couldn't find it and thought I'd post it here for anyone interested.

一绘本一梦想 2024-08-17 11:19:19

我通过与其他人进行实验得到了这个解决方案,比如 @flurin-arner 我启动了 @weston-ganger 设置标题()。我还使用了 @imgx64 PROMPT_DIRTRIM 建议。我还在使用 @itseranga git 分支提示,虽然这与问题无关,但它确实显示了您可以使用提示做什么。

首先如weston和上面所示

 TITLE="\[\e]2;$*\a\]"

可以用来手动设置终端标题,“$*”是命令行输入,但不是我们想要的。

第二,如上所述,我还将 git 分支添加到我的提示符中,这又不是问题的一部分。

export PROMPT_DIRTRIM=3
parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
     }

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

第三,通过实验,我复制了上面的 TITLE 代码,将 $* 设置为固定字符串并尝试了以下操作:

see: \[\e]2;'SomeTitleString'\a\]

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;'SomeTitleString'\a\] $ "

这达到了预期的效果!最终,我希望将基本路径作为我的标题。
PS1 Params 显示 \W 是基础路径所以我的解决方案是这样的:

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;\W\a\] $ "

没有 git 分支:

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\[\033[00m\]\[\e]2;\W\a\] $ "

导致带有 git-branch 的提示:

user@host ~/.../StudyJava (master) $  

导致没有 parse_git_branch 的提示:

   user@host ~/.../StudyJava $  

其中 pwd 给出

/home/user/somedir1/otherdir2/StudyJava

和终端标题

StudyJava

注意:从上面的 @seff 中,我基本上将“我的标题”替换为“\W”

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

I got this solution from experimenting with others, like @flurin-arner I started the @weston-ganger set-title(). I also used @imgx64 PROMPT_DIRTRIM suggestion. I'm also using @itseranga git branch prompt, though this has nothing to do with the question it does show what you can do with the prompt.

First as shown by weston and above

 TITLE="\[\e]2;$*\a\]"

can be used to manually set the Terminal Title, "$*" is commandline input, but not what we want.

2nd as stated I'm also adding git branch to my prompt, again not part of the question.

export PROMPT_DIRTRIM=3
parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
     }

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

3rd, by experiment I copied the TITLE code above, set the $* to a fixed string and tried this:

see: \[\e]2;'SomeTitleString'\a\]

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;'SomeTitleString'\a\] $ "

This had the desired effect! Ultimately, I wanted the base path as my title.
PS1 Params shows that \W is the base path so my solution is this:

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;\W\a\] $ "

without the git branch:

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\[\033[00m\]\[\e]2;\W\a\] $ "

resulting in a prompt with git-branch:

user@host ~/.../StudyJava (master) $  

resulting in a prompt without parse_git_branch:

   user@host ~/.../StudyJava $  

where pwd gives

/home/user/somedir1/otherdir2/StudyJava

and Terminal Title

StudyJava

NOTE: From @seff above I am essentially replacing the "My Title" with "\W"

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
记忆で 2024-08-17 11:19:19

我在 Ubuntu 18.10 上尝试过,它只能与 ~/.bashrc 中的 PROMPT_COMMAND 一起使用。
如果您覆盖PROMPT_COMMAND,标题的行为会略有变化。我决定仅在必要时进行更改:

t() {
  TITLE="$@"
  PROMPT_COMMAND='echo -ne "\033]0;${TITLE}\007"'
}

在此处输入图像描述

I tried this on Ubuntu 18.10 and it only worked with PROMPT_COMMAND in ~/.bashrc.
And if you override PROMPT_COMMAND, the behavior of the title changes slightly. I decided to change only if necessary:

t() {
  TITLE="$@"
  PROMPT_COMMAND='echo -ne "\033]0;${TITLE}\007"'
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文