Shell 提示换行问题

发布于 2024-07-26 21:29:26 字数 801 浏览 14 评论 0原文

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

这是我配置的 PS1:

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

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

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

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

我仍然看到类似的内容:

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/

我也尝试过 \033 而不是 \e。 我只是将 PS2 包含在内以供参考,我没有更改安装默认值。 如果我完全删除颜色代码,那么一切正常,有什么想法吗?

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

This is the PS1 that I had configured:

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 \033 instead 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

夜雨飘雪 2024-08-02 21:29:27

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

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

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

I am now using this PS1 with good effect:

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

因为看清所以看轻 2024-08-02 21:29:27

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

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

内心激荡 2024-08-02 21:29:27

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

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

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

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

吹梦到西洲 2024-08-02 21:29:27

看来您已经正确转义和封闭了序列。

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

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

您可能知道的优秀指南:

Bash 提示如何

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\[\033[0;30m\]$\[\033[0m\]

An excellent howto you probably know :

Bash prompt howto

夕色琉璃 2024-08-02 21:29:27

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

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

您可以通过执行以下操作来重现此内容:

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

并多次按向上键,您将看到提示困惑了。

您可以看到一个实际的示例:

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

出现这种情况,只需按即可恢复正常。

请注意,ZShell 不存在此问题。

I noticed that there are some issues with the prompt cursor positioning even if there are no special character in the PS1 or PROMPT environment variable.

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

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

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

Note that ZShell does not have this issue.

红焚 2024-08-02 21:29:27

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

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

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

[ec2-user@Project:~]$

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

For future reference, this is what I use:

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

This would display my shell prompt as:

[ec2-user@Project:~]$

Helps me distinguish between live and dev sites.

情泪▽动烟 2024-08-02 21:29:27

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

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\]\

另外,添加“

GIT_PS1_SHOWDIRTYSTATE=true

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

export HISTCONTROL=ignoredups

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

bind "set completion-ignore-case on" 

也有帮助。

最后,

shopt -s checkwinsize

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

另外,添加“

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

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

也有帮助。

最后,

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

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.

bind "set completion-ignore-case on" 

Helps too.

Lastly,

shopt -s checkwinsize

May be helpful on OSX if issues persist.

Also, add

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

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

Helps too.

Lastly,

May be helpful on OSX if issues persist.

明月夜 2024-08-02 21:29:27

'shopt -s checkwinsize' 也适用于 Cygwin 换行问题

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

贪了杯 2024-08-02 21:29:27

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文