Shell Prompt Line Wrapping Issue

强颜欢笑 提交于 2019-12-03 00:53:34

问题


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?

|improve this question

回答1:


Line wrapping issues in Bash are nothing new. Consult the Mailinglist, maybe there's yet another bug regarding this.

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




回答2:


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




回答3:


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).




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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




回答9:


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



来源:https://stackoverflow.com/questions/1133031/shell-prompt-line-wrapping-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!