bash Shell 提示换行问题

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

Shell Prompt Line Wrapping Issue

bashcolorspromptansi-escape

提问by Rob

I've done something to break my Bash Shell Prompt in OS X (10.5.7) Terminal.

我已经做了一些事情来破坏 OS X (10.5.7) 终端中的 Bash Shell Prompt。

This is the PS1 that I had configured:

这是我配置的PS1:

PS1='\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]$ '

As far as I can tell I have the color commands escaping correctly. However when I scroll up and down in my command history I often get line wrapping issues if the historic commands wrap onto multiple lines.

据我所知,我的颜色命令可以正确转义。但是,当我在命令历史记录中上下滚动时,如果历史命令换行到多行,我经常会遇到换行问题。

I simplified my prompts to the following:

我将提示简化为以下内容:

PS1='\[\e[1m\]\h:\w$ \[\e[0m\]'
PS2='> '

And I still see something like:

我仍然看到类似的东西:

localhost:~/Library/Application Support/Firefox/Profiles/knpmxpup.Defau
lt/extensions/{1A2D0EC4-75F5-4c91-89C4-3656F6E44B68}$ expocd \{1A2D0EC4-7
5F5-4c91-89C4-3656F6E                                           export PS1="\[
\e[1;32m\]\h\[\e[0m\]:                                          cd Library/Appl
ication\ Support/

I've also tried \033instead of \e. I just included PS2 up there for information, I haven't changed that from the install default. If I completely remove the color codes then everything works fine, any ideas?

我也试过\033代替\e. 我只是在那里包含了 PS2 以获取信息,我没有更改安装默认设置。如果我完全删除颜色代码,那么一切正常,有什么想法吗?

采纳答案by TheBonsai

Line wrapping issues in Bash are nothing new. Your PS1should work as is but there is a bug in Bash 3.2.49. Consult the mailing list, there's yet another bug regarding thiswhich was confirmed to be fixed in 4.0.

Bash 中的换行问题并不是什么新鲜事。您PS1应该按原样工作,但 Bash 3.2.49 中存在一个错误。查阅邮件列表,还有一个关于此的错误确认已在 4.0 中修复

You can't do much more than tagging unprintable characters with \[and \], the rest must be done by the prompting code.

除了用\[和标记不可打印的字符外,您不能做更多的事情\],其余的必须由提示代码完成。

回答by Rob

I am now using this PS1 with good effect:

我现在用的这个PS1效果很好:

green=$(tput setaf 2)
blue=$(tput setaf 4)
bold=$(tput bold)
reset=$(tput sgr0)
PS1="\[$green$bold\]\h\[$reset\]:\[$blue$bold\]\w\[$reset\]$ "

Scrolling through my command history appears to handle line wraps now. However in the meantime since this question was asked I have also updated my OS X to 10.6.3

滚动我的命令历史记录似乎现在可以处理换行。然而与此同时,自从提出这个问题以来,我也将我的 OS X 更新到了 10.6.3

回答by willdye

This stackoverflow threadseems relevant. As someone noted in that thread, the Bash FAQ at mywiki.wooledge.orgdiscusses how to properly quote color codes in Bash prompts (FAQ 53), and the proper invocation of terminal colors (FAQ 37).

这个 stackoverflow 线程似乎相关。正如有人在该线程中指出的那样,mywiki.wooledge.org 上Bash 常见问题解答讨论了如何在 Bash 提示中正确引用颜色代码(常见问题 53),以及正确调用终端颜色(常见问题 37)。

回答by neuro

It seems that you have correctly escaped and enclosed sequences.

您似乎已正确转义和封闭序列。

A workaround I use anyway it it to add a '\n' at the end. I find it clearer and lessen any problem with wrapping issues. The exact end of my PS1 is :

无论如何,我使用的一种解决方法是在末尾添加一个 '\n'。我发现它更清晰并减少了包装问题的任何问题。我的 PS1 的确切结尾是:

'\n\[3[0;30m\]$\[3[0m\]

An excellent howto you probably know :

您可能知道的一个很好的方法:

Bash prompt howto

Bash 提示操作方法

回答by Martinos

I noticed that there are some issues with the prompt cursor positioning even if there are no special character in the PS1or PROMPTenvironment variable.

我注意到即使PS1orPROMPT环境变量中没有特殊字符,提示光标定位也存在一些问题。

If we output a file that does not have a end-of-line char at the end. It will confuse the prompt.

如果我们输出的文件末尾没有行尾字符。它会混淆提示。

You can reproduce this by doing:

您可以通过执行以下操作来重现这一点:

curl https://gist.githubusercontent.com/martinos/d4aa0a7d4d752b0d0d9f/raw/3198c39f84a080c44227a084a19fb3a0bb661ee5/wrapping_issue.txt

curl https://gist.githubusercontent.com/martinos/d4aa0a7d4d752b0d0d9f/raw/3198c39f84a080c44227a084a19fb3a0bb661ee5/wrapping_issue.txt

and pressing the up key multiple times and you will see that the prompt get confused.

并多次按下向上键,您将看到提示变得混乱。

You can see an example of this in action:

你可以看到一个这样的例子:

https://asciinema.org/a/9mtjhi9dib6md4ocsbw210cca

https://asciinema.org/a/9mtjhi9dib6md4ocsbw210cca

When this occurs, just press <CTRL-C>and the prompt will come back to normal.

出现这种情况时,只需按一下<CTRL-C>,提示就会恢复正常。

Note that ZShell does not have this issue.

请注意,ZShell 没有这个问题。

回答by rcd

Here's mine: it's the best one I've found, but the site where I originally found it was missing an escape character, leading to the line wrapping issue. I tinkered with it and finally got it working. It shows your user, path, and branch info with good contrast, color-wise.

这是我的:这是我找到的最好的,但我最初发现它的网站缺少转义字符,导致换行问题。我对它进行了修改,终于让它工作了。它以良好的对比度和颜色显示您的用户、路径和分支信息。

export PS1='\[\e[1;37m\]\[\e[1;32m\]\u\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]|\[\e[0;39m\]$'

Also, add

另外,添加

GIT_PS1_SHOWDIRTYSTATE=true

To show a marker when a branch is "dirty" (changes to be committed exist)

在分支“脏”时显示标记(存在要提交的更改)

export HISTCONTROL=ignoredups

Is also useful to ignore duplicates when scrolling up through bash history.

在向上滚动 bash 历史记录时忽略重复项也很有用。

bind "set completion-ignore-case on" 

Helps too.

也有帮助。

Lastly,

最后,

shopt -s checkwinsize

May be helpful on OSX if issues persist.

如果问题仍然存在,可能对 OSX 有所帮助。

回答by user3375990

'shopt -s checkwinsize' also works for Cygwin wrap problems also

'shopt -s checkwinsize' 也适用于 Cygwin 包装问题

回答by Charlie

If you're using the title bar trick "\e]2;titlebar\a", make sure to escape that too: "\[\e]2;titlebar\a\]"

如果您正在使用标题栏技巧"\e]2;titlebar\a",请确保也将其转义:"\[\e]2;titlebar\a\]"

回答by Anil

For future reference, this is what I use:

为了将来参考,这就是我使用的:

export PS1="\[\033[0;31m\][\u@Project:\w]$\[\033[0m\] "

export PS1="\[\033[0;31m\][\u@Project:\w]$\[\033[0m\] "

This would display my shell prompt as:

这会将我的 shell 提示显示为:

[ec2-user@Project:~]$

[ec2-user@Project:~]$

Helps me distinguish between live and dev sites.

帮助我区分实时站点和开发站点。