bash 这个用于命名 iTerm 选项卡的脚本是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8823103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does this script for naming iTerm tabs work?
提问by Deonomo
I'm trying to name my iTerm tabs and found this link.Here is the pertinent part of the guy's post:
我正在尝试命名我的 iTerm 选项卡并找到了这个链接。这是该人帖子的相关部分:
I wrote a simple script, which I call “nametab”, which allows you to name the tab you are in from the command line. You just type something like:
$ nametab New tab nameIf you'd like to use this yourself, here is the code:
#!/bin/bash # A simple script which will name a tab in iTerm # usage: # $ nametab New tab name echo -ne "3]0;"$@"7"$ nametab New tab name
我写了一个简单的脚本,我称之为“nametab”,它允许你从命令行命名你所在的选项卡。您只需键入以下内容:
#!/bin/bash # A simple script which will name a tab in iTerm # usage: # $ nametab New tab name echo -ne "3]0;"$@"7"chmod u+x nametab.sh如果你想自己使用它,这里是代码:
echo TERM=$TERM
I have created a directory $HOME/dev/bash_scriptsand placed a file in that directory called nametab.sh. I then switched to that directory and ran the command
我创建了一个目录$HOME/dev/bash_scripts并在该目录中放置了一个名为nametab.sh. 然后我切换到该目录并运行命令
TERM=xterm-256color
But when I try to name my current tab in iTerm by typing nametab.sh New tab hellooooo, nothing happens. I also tried nametab.sh hellooooo, and nothing happens.
但是当我尝试通过键入在 iTerm 中命名我当前的选项卡时nametab.sh New tab hellooooo,没有任何反应。我也试过nametab.sh hellooooo,没有任何反应。
Can you help me understand what I don't understand?
你能帮我理解我不理解的东西吗?
Update:
更新:
echo $PATH
returns
回报
.:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:~/dev/bash_scripts
and
和
cat ~/dev/bash_scripts/nametab.sh
returns
回报
#!/bin/bash
# A simple script which will name a tab in iTerm
# usage:
# $ nametab NewTabName
echo "trying to rename the current tab to $@"
echo -ne "3]0;"$@"nametab.sh hellooooo
7"
echo "finished"
and
和
trying to rename the current tab to helloooo
finished
returns
回报
MindRoot (bash)
and
和
# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
return
fi
#PS1='\h:\W \u$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
# ALL OF THE BELOW ADDED BY DEONOMO ON 2011-04-25
# custom prompt
PROMPT_HOSTNAME='MindRoot'
PROMPT_COLOR='0;35m'
# If I am root, set the prompt to bright red
if [ ${UID} -eq 0 ]; then
PROMPT_COLOR='1;31m'
fi
PS1='\[\e]1;${PROMPT_HOSTNAME}\a\e]2;${PROMPT_HOSTNAME}:${PWD}\a\
\e[${PROMPT_COLOR}\]\
[\u@${PROMPT_HOSTNAME} \w]\n \#$ \
\[\e[m\]'
#PS1="\e[0;45m\w:$ "
# added by Deonomo on 2011/09/12 in order to have textmate work as default editor
export EDITOR='mate -w'
# added by Deonomo on 2012-01-11 in order to start a dev/bash_scripts directory
export PATH="$PATH:~/dev/bash_scripts"
returns
回报
function renametab () {
echo -ne "3]0;"$@"export PROMPT_COMMAND=''
7"
}
but the tab name always stays the same.
但选项卡名称始终保持不变。
Incidentally, the tab name reads
顺便说一句,选项卡名称读取
rename_tab () {
TEXT=
export PROMPT_COMMAND='echo -ne "3]0;${TEXT}##代码##7"'
}
I am running iTerm2. I try to do all my bash shell configuration in /etc/bashrc. This way I get the same bash terminal behavior regardless of which user account I am logged in on. The contents of /etc/bashrcis
我正在运行 iTerm2。我尝试在 .bashrc 中完成我所有的 bash shell 配置/etc/bashrc。这样,无论我登录哪个用户帐户,我都会获得相同的 bash 终端行为。的内容/etc/bashrc是
回答by Jeppe Fihl-Pearson
If you want to have an alias for changing the tab name, you can actually do it by defining a function in your .profile/.bashrc file like this:
如果您想使用别名来更改选项卡名称,实际上可以通过在 .profile/.bashrc 文件中定义一个函数来实现,如下所示:
##代码##回答by Chad Armstrong
I was having your same problem - but I saw the tab name briefly flash before going to back to what it was: the shell and the cwd. Turns out I had an environment variable changing the tab name on every shell command, so this fixed it for me:
我遇到了同样的问题 - 但我看到标签名称短暂闪烁,然后再回到原来的样子:shell 和 cwd。原来我有一个环境变量在每个 shell 命令上更改选项卡名称,所以这为我修复了它:
##代码##Now: echo -e "\033];MY_NEW_TITLE\007"
现在: echo -e "\033];MY_NEW_TITLE\007"
..works just fine and persists.
..工作得很好并且持续存在。
回答by csiu
This is a function. You can add it to your ~/.bashrc(or something similar, like a ~/.bash_profile). To rename the tabs, you can then do this:
这是一个函数。您可以将它添加到您的~/.bashrc(或类似的东西,如~/.bash_profile)。要重命名选项卡,您可以执行以下操作:
$ rename_tab 'NEW NAME HERE'
$ rename_tab 'NEW NAME HERE'

