我在 Ubuntu 16.04 上使用 GNU bash 版本 4.4.0(1)。
当我在 bash 中输入长行时,它的显示会变得混乱。
例如,假设我在~/test/test/test/test/test/test/test
并且输入echo "The quick brown fox jumps over the lazy doggo"
它看起来像这样:
这还算可以,尽管我更喜欢包裹式显示器,而且我可以不用加倍线。
问题是,当我现在通过按退格键三次加双引号一次来修复句子时,显示如下:
您可以看到打印了所需的句子 with dog
instead of 。doggo
但无论出于何种原因,它都是以绿色打印的。
当我现在放大和缩小终端窗口几次时,所有这一切都变得更糟。然后,终端的内容被随机复制并连接到整个终端: (长黑框是我的提示内容的随机串联。)
我认为所有这一切都可能与 相关~/.bashrc
,尽管我刚刚这样做了cp /etc/skel/.bashrc ~/
。无论如何,这里是:
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
删除 ~/.bashrc 后,我得到以下信息:(我使用该命令echo "This is a really long sentence, even long than a sentence containing all letters of the alphabet"
来弥补较短的提示)
答案1
您的提示包含颜色(可能还包含其他不可打印的内容)。
\[
在 bash 中,您可以用和包围这些部分,\]
告诉 bash 忽略这些部分来计算提示的长度。
前任:
PS1="\e[33mthis is green\e[0m this is normal"
# should be done as:
PS1="\[\e[33m\]this is green\[\e[0m\] this is normal"
第二个周围的\[
每个\]
部分在更改颜色时不会在显示中添加任何可见字符,因此在计算提示上字符的位置时不应将其计算在内。
如果您修改了 PS1,请确保您没有放入\[
可见\]
字符。如果你这样做了,请将它们排除在外。
另一件事:您可能需要shopt -q -s checkwinsize
确保终端大小在调整大小时更新。
而且您似乎使用的设置仅显示当前输入的命令行的末尾(类似于 ksh 所做的...您确定使用 bash 吗?):我将研究什么选项可以更改此设置,但我还不知道。
编辑
在聊天中我们还讨论了:
set -o emacs #he was in vi mode
set -o posix #he was in posix=no mode
但最后似乎有效的是摆脱〜/ .inputrc(他这样做了,它可能包含一些错误的东西,但我不知道里面有什么,我也没有要求删除它^^只是重命名它) (或修复其内容)会有帮助......)
答案2
我在@OlivierDulac 的帮助下解决了这个问题,通过对~/.bashrc
和进行更改~/.inputrc
。
在~/.bashrc
我现在使用:
PS1='\e[01;32m\u:\e[01;34m\w\e[0m\$ '
请注意,我将 \033 替换为 \e 以创建颜色。单独对原始 PS1 执行此操作并没有修复它,因此它还有其他问题,但我太累了,无法找到它。我相信事实是我(与很多建议相反,也与我用来创建 PS1 的 ezprompt.net 相反)不使用\[
和\]
来包含非打印字符。事实上,如果我真的使用这些,一切都会再次变得混乱。
在 中~/.inputrc
,我之前有过
#These lines make the vi mode visible by switching from a block cursor to a line cursor.
set show-mode-in-prompt on
[...]
set vi-cmd-mode-string \1\e[1 q\2
set vi-ins-mode-string \1\e[5 q\2
我只是删除了\1
and \2
,即我现在有了
set editing-mode vi
[...]
set show-mode-in-prompt on
set vi-cmd-mode-string \e[1 q
set vi-ins-mode-string \e[5 q
同样,这与我在其他地方推荐的相反。也许是因为我使用的是相对较新的bash? (set show-mode-in-prompt on
仅适用于 bash4.4 afaik)
答案3
您的终端设置(终端类型)是否正确?在我的 ubuntu Linux 主机上,我的终端类型是“ansi”。 (您可以通过发出命令来获取此信息: echo $TERM )。终端类型必须与您正在使用的屏幕(控制台)匹配。有时,如果终端类型不正确,终端/控制台无法识别终端转义码(例如光标控制),因此屏幕输出会变得混乱。其他标准终端类型有“vt100”、“vt220”。
答案4
改变
TERM=xterm-256color
到
TERM=xterm