Bash PS1 设置 - 如何将当前文件夹恢复为终端标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2410802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:46:19  来源:igfitidea点击:

Bash PS1 settings - how to get the current folder back as the terminal title

gitbash

提问by Max Williams

I recently added these lines to my ~/.bashrc file to show the current branch if i'm in a git working folder, and it works nicely for that. However, what i've lost is that the current folder name used to be shown in the tab for the terminal i have open, and now it isn't: it always just says 'Terminal'. Can i get that back and still keep the git stuff? Here's the lines in question - it's the second one that's the issue, as commenting out just the second line fixes the problem.

我最近将这些行添加到我的 ~/.bashrc 文件中以显示当前分支,如果我在 git 工作文件夹中,它可以很好地工作。但是,我丢失的是当前文件夹名称曾经显示在我打开的终端的选项卡中,现在不是:它总是只显示“终端”。我可以拿回来并仍然保留 git 的东西吗?这是有问题的行 - 这是第二行的问题,因为注释掉第二行就可以解决问题。

source /etc/bash_completion.d/git
PS1='\h:\w$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ '

I've been looking at explanations of the options for PS1 but can't see anything about the terminal window's title in there. Can anyone advise? thanks, max

我一直在查看 PS1 选项的解释,但在那里看不到有关终端窗口标题的任何信息。任何人都可以建议吗?谢谢,最大

EDIT

编辑

I actually manipulate PS1 already in order to have a terminal with the format

我实际上已经在操纵 PS1 以便拥有一个具有该格式的终端

<rvm version and gemset> <computer name> <current folder> <git branch>

<rvm version and gemset> <computer name> <current folder> <git branch>

, with each part in a different color, but i've never actually seen the docs before, so thanks for the link to that. My current PS1 setting is

,每个部分都有不同的颜色,但我以前从未真正看过文档,所以感谢您提供链接。我目前的 PS1 设置是

\[3[0;31m\]$(__my_rvm_ruby_version)\[3[0;33m\]\h\[3[0;37m\]:\[3[1;33m\]\W\[3[1;32m\]$(__git_branch)\[3[1;32m\]$(__git_dirty) \[3[0;37m\]$

Presumably i can do something like

大概我可以做类似的事情

export "<something> $PS1"

to set my terminal tab name without losing my existing settings. I've been poking around with this though and not managed to do it.

在不丢失现有设置的情况下设置我的终端选项卡名称。我一直在探索这个,但没有设法做到。

EDIT - figured this out with the help of some of the answers below - thanks all! I wrapped it up in a shell script

编辑 - 在下面的一些答案的帮助下解决了这个问题 - 谢谢大家!我把它包装在一个 shell 脚本中

#!/usr/bin/env bash
#renames the current terminal tab via the PS1 env var
source ~/.bashrc
export PS1="$PS1""\[\e]0; \a\]"

it's called "renametab" so i can now call it with eg

它被称为“renametab”,所以我现在可以用例如

source renametab mytabname

"source" is needed to export the changes into the current shell: if i just do renametab mytabnamethe export just goes into a subshell which is killed when the script finishes.

需要“源”将更改导出到当前外壳程序中:如果我只是执行renametab mytabname导出,则只会进入一个子外壳程序,该子外壳程序在脚本完成时被终止。

Thanks again all, for the help!

再次感谢大家的帮助!

采纳答案by Romuald Brunet

That's what I have as default on my Ubuntu concerning the terminal's title:

这就是我在 Ubuntu 上关于终端标题的默认设置:

PS1='\[\e]0;\u@\h: \w\a\]'

Prepend your PS1 with this one and it should be fine

把你的 PS1 放在这个前面,应该没问题

回答by Paused until further notice.

You can try:

你可以试试:

PS1="$PS1"'\h:\w$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ '

But it would help to know what PS1 is being set to earlier in ~/.bashrcor in /etc/bash.bashrc.

但了解 PS1~/.bashrc/etc/bash.bashrc.

回答by David W.

If you're lucky, your terminal is Xterm compatible, and you can use the ANSI sequences to set your terminal title.

如果幸运的话,您的终端与 Xterm 兼容,您可以使用 ANSI 序列来设置终端标题。

Instead of giving you the sequence, I'll point you to the documentation. Now, you can colorize your prompt. The sequence you need is described as xterm title hackwhich is what it is.

我不会为您提供序列,而是将您指向文档。现在,您可以为提示着色。您需要的序列被描述为xterm title hack这就是它。

You can also use the control sequences in the prompt to help set PS1. These can be found in the BASH manpage under PROMPTING.

您还可以使用提示中的控制序列来帮助设置PS1. 这些可以在PROMPTING下的 BASH 联机帮助页中找到。

And here's what I have in my .bashrc:

这是我在我的.bashrc

PS1="\e]0;BASH: \u@\h\aBASH \v: \u@\h:\w\n$ "
  • \e]0;BASH: \u@\h\a- Set Window title
    • \e]- Send ESC character followed by closed left bracket. This is the start of an ANSI command sequence.
    • 0;- Zero followed by a semicolon. The following string sets the title
    • BASH: \u@\h\- My Window Title
      • \u- User Name
      • @- The @ sign
      • \h- Host Name
    • \a- The BEL (Cnrl-G) which ends my title
  • BASH \v: \u@\h:\w\n\$- Sets my prompt
    • BASH \v:- The string BASHfollowed by the version. My default shell is Ksh, so when I am using the BASH shell, I want to see the word BASHin my prompt
    • \u- The user name
    • \h- The Host name
    • \w- The current working directory (HOME based)
    • \n- New line - My prompt is two lines. This way, long directories don't take up command line space.
    • \$- Dollar sign or #if you have root privileges.
  • \e]0;BASH: \u@\h\a- 设置窗口标题
    • \e]- 发送 ESC 字符,后跟封闭的左括号。这是 ANSI 命令序列的开始。
    • 0;- 零后跟一个分号。以下字符串设置标题
    • BASH: \u@\h\- 我的窗口标题
      • \u- 用户名
      • @- 标志
      • \h- 主机名
    • \a- 结束我的头衔的 BEL (Cnrl-G)
  • BASH \v: \u@\h:\w\n\$- 设置我的提示
    • BASH \v:- 字符串BASH后跟版本。我的默认 shell 是 Ksh,所以当我使用 BASH shell 时,我想在我的提示中看到BASH这个词
    • \u- 用户名
    • \h- 主机名
    • \w- 当前工作目录(基于 HOME)
    • \n- 新行 - 我的提示是两行。这样,长目录就不会占用命令行空间。
    • \$- 美元符号或#如果您有 root 权限。

Play around and have fun. Try the different escape sequences. Add color. Embed the GIT stuff. Explore.

四处玩耍,玩得开心。尝试不同的转义序列。添加颜色。嵌入 GIT 的东西。探索。

Remember, it's just a computer. The worst you can do is cause the whole system to come crashing down and delete all of your valuable work, lose your job, and become a hopeless pariah whose only means of income will be asking customers if they want fries with that. Unless, you work at a bank. Then you also can bring down the entire global economy.

请记住,它只是一台计算机。你能做的最糟糕的事情是导致整个系统崩溃并删除你所有有价值的工作,失去你的工作,并成为一个绝望的贱民,唯一的收入手段就是询问顾客是否想要炸薯条。除非你在银行工作。然后你也可以拖垮整个全球经济。

回答by gjcamann

For the truelly helpless (like myself). Here is my Bash RC that includes git branch name in the prompt and sets the window title. Notice this uses the PROMPT_COMMAND for __git_ps1, which sets PS1 internally. When using __git_ps1 this way it takes 2 arguments. The first argument is the string before the branch name, the second argument is the string after the branch name.

对于真正无助的人(比如我自己)。这是我的 Bash RC,它在提示中包含 git 分支名称并设置窗口标题。注意这使用了 __git_ps1 的 PROMPT_COMMAND,它在内部设置 PS1。当以这种方式使用 __git_ps1 时,它需要 2 个参数。第一个参数是分支名称之前的字符串,第二个参数是分支名称之后的字符串。

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

source ~/git/contrib/completion/git-prompt.sh

alias ls='ls --color=auto'
alias cd..='cd ..'

c_reset='\[\e[0m\]'
c_prompt='\[\e[1;31m\]'
c_path='\[\e[0;33m\]'

if [ -f ~/git/contrib/completion/git-prompt.sh ]; then
. ~/git/contrib/completion/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_UNTRACKEDFILES=true
# Below, The section "\[\e]0;\w\a\]" sets the window title
PROMPT_COMMAND="__git_ps1 '${c_path}\w${c_reset}' '\n\u@\t${c_prompt}-->${c_reset}\[\e]0;\w\a\]' "
fi

#PS1 is set by the __git_ps1 script above, so comment out below
#PS1='\w\n[\u@\t]$>'

回答by Joe Wahoo

Another way to do this is to use PROMPT_COMMAND and let PS1 just be the prompt. For example:

另一种方法是使用 PROMPT_COMMAND 并让 PS1 成为提示。例如:

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

回答by Trampas Kirk

I use this:

我用这个:

PROMPT_COMMAND='echo -ne "3]0;${PWD/$HOME/~} - ${USER}@${HOSTNAME}
/home/tkirk - tkirk@hostname
7"'

which results in a window title like this:

这会导致窗口标题如下:

##代码##