从 shellscript 设置屏幕标题
是否可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用以下行设置屏幕/ xterm 标题:
[更新] - 根据请求,我还包括下面 @Espo 提出的解决方案:
根据您的 xterm 版本或您的 Linux 发行版,上面的行可能会也可能不会工作,并且您可以尝试 xterm-defaults:
有关详细信息,请参阅: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 或参考下面 @Espo 的答案。
You can set the screen / xterm title using the following lines:
[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:
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.
来自 http://www.faqs.org/docs/Linux- mini/Xterm-Title.html#s3
From http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3
以下是编写屏幕标题重命名脚本的其他方法:
将以下设置添加到
.ssh/config
可以在使用 SSH 登录系统时自动设置屏幕标题:而不是
%h
,代表您要连接的机器的主机名,您可以使用%n
,这是您用于连接到机器的实际名称/别名。注意:您需要 OpenSSH >= v5.1 才能使用本地主机 %n 和 %h 参数。查看“man ssh_config”以获取有关 LocalCommand 的更多信息。
要自动将标题恢复为本地主机的主机名,在关闭 SSH 会话后,您可以在
.bashrc
中的提示符变量PS1
中添加转义序列:当使用
.screenrc
配置显示当前正在哪个屏幕“选项卡”中工作时,这些技巧特别有用。将如下内容添加到.screenrc
中以使其正常工作: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: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
: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:尝试下面的命令,不需要编辑任何文件或配置,如~/.bashrc,可以在运行时使用。
将静态文本设置为标题:(我的标题)
将本地/全局变量设置为标题:($USER)
将命令输出设置为标题:(主机名)
设置为默认值(恢复):
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)
Set local/global variable as title: ($USER)
Set command output as title: (hostname)
Set to default (Revert back):
您还可以调用 screen 并告诉它设置标题:
如果您位于屏幕窗口中,它将设置该窗口的名称。如果您不在屏幕中,它将设置最近打开的窗口的名称。
You can also call screen and tell it to set a 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.
要添加到 Espo 的答案中,xterm 转义序列也可以应用于 Bash
PS1
变量示例
To add to Espo's answer, the xterm escape sequences can also be applied to the Bash
PS1
variableExample
要在使用 ssh 跳转时启用自动标题更新,请将其添加到
~/.bashrc
:请参阅 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
:See http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html
或者更好地从 此处
or even better copy the whole concept for customizing your bash configs between a lot of hosts from here
我对这个问题的解决方案是创建一个 bash 脚本并将其添加到我的 ~/.bashrc 文件中:
现在,当我在任何 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:
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.
我通过与其他人进行实验得到了这个解决方案,比如 @flurin-arner 我启动了 @weston-ganger 设置标题()。我还使用了 @imgx64 PROMPT_DIRTRIM 建议。我还在使用 @itseranga git 分支提示,虽然这与问题无关,但它确实显示了您可以使用提示做什么。
首先如weston和上面所示
可以用来手动设置终端标题,“$*”是命令行输入,但不是我们想要的。
第二,如上所述,我还将 git 分支添加到我的提示符中,这又不是问题的一部分。
第三,通过实验,我复制了上面的 TITLE 代码,将 $* 设置为固定字符串并尝试了以下操作:
这达到了预期的效果!最终,我希望将基本路径作为我的标题。
PS1 Params 显示 \W 是基础路径所以我的解决方案是这样的:
没有 git 分支:
导致带有 git-branch 的提示:
导致没有 parse_git_branch 的提示:
其中 pwd 给出
和终端标题
注意:从上面的 @seff 中,我基本上将“我的标题”替换为“\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
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.
3rd, by experiment I copied the TITLE code above, set the $* to a fixed string and tried this:
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:
without the git branch:
resulting in a prompt with git-branch:
resulting in a prompt without parse_git_branch:
where pwd gives
and Terminal Title
NOTE: From @seff above I am essentially replacing the "My Title" with "\W"
我在 Ubuntu 18.10 上尝试过,它只能与 ~/.bashrc 中的 PROMPT_COMMAND 一起使用。
如果您覆盖PROMPT_COMMAND,标题的行为会略有变化。我决定仅在必要时进行更改:
在此处输入图像描述
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:
enter image description here