自定义Bash提示符是自身覆盖 [英] Custom Bash prompt is overwriting itself
问题描述
我使用的是定制的bash提示符显示的Git分支。
一切都在的/ etc / bash中/ .bashrc中
:
功能formattedGitBranch {
_branch =$(GIT分支2 - ;的/ dev / null的| sed的-e/ ^ \\ S / D-eS / ^ \\ * \\ S //)
#尝试这些:
回声-e\\ E [0;91米($ _branch)
回声-e\\ E [0;91米($ _branch)\\ E [M
回声-e $'\\ E [0;91米'($ _分公司)
回声($ _branch)
回声$(tput的setaf 2)($ _branch)$(tput的setaf 9)
printf的\\ E [0;91米($ _branch)
}#颜色之前函数调用设置
PS1 =\\ [\\ 033 [01;34米\\] \\ [\\ 033 [0;91米\\] $(formattedGitBranch)\\ $ \\ [\\ 033 [00米\\]'
#颜色设置功能里
PS1 =\\ [\\ 033 [01;34米\\] $(formattedGitBranch)\\ $ \\ [\\ 033 [00米\\]'
问题是,当我设置颜色 $ _分支
中的功能,当达到EOL我的提示将被改写:
试过所有可能的变量 tput的
,的printf
, $''
标记。
我只在 PS1
设置颜色的问题解决了
不过..
- 我想知道为什么它覆盖我的提示
- 如何使用功能时,解决这个问题
我使用的Gentoo Linux。 GNU的bash,verze 4.2.37(1) - 释放(i686的-PC-Linux的GNU)
1)我想知道为什么它覆盖我的提示
由于每个非打印字符必须由进行转义\\ [
和 \\]
,否则的readline 无法跟踪正确的光标位置。
您必须把
\\ [
和\\]
在你的周围迅速任何非打印转义序列。
如果没有\\ [\\]
庆典会认为这构成了颜色$ C $转义序列字节CS将实际占用的空间在屏幕上,这样的bash不会能知道光标实际上是。
\\ [
开始的非打印字符的序列。 (如颜色转义序列)。这个
允许庆典计算字正确包装。
\\]
结束的非打印字符的序列。
- BashFAQ
...注意,非打印字符转义,这些保证的readline可以正确地跟踪光标位置。 - ss64.com
块引用>2)如何使用功能时,解决这个问题
如果你真的需要/想设置内部函数,其输出
PS
用于你有2个选择的颜色:
PS1 ='\\ [$(formattedGitBranch)\\]
逃脱内
非打印字符回声
与\\ 001
\\ 002
(感谢的重力)!
回声-e
是不是bash的察觉\\ [
\\]
所以你必须以来替代这些\\ 001
&安培;\\ 002
ASCII控制codeS分隔从打印非打印字符:功能formattedGitBranch {回声-e\\ 001 \\ E [0;91米\\ 002($ _branch); }
PS1 ='$(formattedGitBranch)'I'm using custom bash prompt to show git branch.
Everything is in
/etc/bash/bashrc
:function formattedGitBranch { _branch="$(git branch 2>/dev/null | sed -e "/^\s/d" -e "s/^\*\s//")" # tried these: echo -e "\e[0;91m ($_branch)" echo -e "\e[0;91m ($_branch) \e[m" echo -e $'\e[0;91m'"($_branch)" echo "($_branch)" echo "$(tput setaf 2) ($_branch) $(tput setaf 9)" printf "\e[0;91m ($_branch)" } # color is set before function call PS1='\[\033[01;34m\] \[\033[0;91m\]$(formattedGitBranch) \$\[\033[00m\] ' # color is set inside function PS1='\[\033[01;34m\] $(formattedGitBranch) \$\[\033[00m\] '
Problem is that when I set color for
$_branch
in the function, my prompt will be overwritten when EOL is reached:Tried all possible variants
tput
,printf
,$''
notation.I solved the problem by setting the colour only in
PS1
:But..
- I would like to know why it is overwriting my prompt
- How to fix this issue when function is used
I'm using Gentoo Linux.
GNU bash, verze 4.2.37(1)-release (i686-pc-linux-gnu)
解决方案1) I would like to know why it is overwriting my prompt
Because every non-printable characters have to be escaped by
\[
and\]
otherwise readline cannot keep track of the cursor position correctly.You must put
\[
and\]
around any non-printing escape sequences in your prompt.
Without the\[ \]
bash will think the bytes which constitute the escape sequences for the color codes will actually take up space on the screen, so bash won't be able to know where the cursor actually is.
\[
Begin a sequence of non-printing characters. (like color escape sequences). This allows bash to calculate word wrapping correctly.
\]
End a sequence of non-printing characters. -- BashFAQ...note the escapes for the non printing characters, these ensure that readline can keep track of the cursor position correctly. -- ss64.com
2) How to fix this issue when function is used
If you really need/want to set colours inside function whose output is used in
PS
you have 2 options:
PS1='\[ $(formattedGitBranch) \] '
escape non-printable chars inside
echo
with\001
\002
(thanks gravity)!
echo -e
is not aware of bash's\[
\]
so you have to substitute these with\001
&\002
ASCII control codes to delimit non-printable chars from printable:function formattedGitBranch { echo -e "\001\e[0;91m\002 ($_branch)"; } PS1='$(formattedGitBranch) '
这篇关于自定义Bash提示符是自身覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!