bash:弄乱了长行的显示

Ban*_*ach 5 bash ubuntu bashrc

我在 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"它看起来像这样: 在此处输入图片说明 虽然我更喜欢包裹式显示器,而且我可以在不加倍线的情况下做到这一点,但这也还好。

问题是当我现在通过按退格键三次加上双引号来修复句子时,显示如下: 在此处输入图片说明

所述o"在端部是一个显示错误,通过按输入的证实: 在此处输入图片说明

您可以看到打印了所需的句子dog而不是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
Run Code Online (Sandbox Code Playgroud)

删除 ~/.bashrc 后,我得到以下信息:(我使用命令echo "This is a really long sentence, even long than a sentence containing all letters of the alphabet"来弥补较短的提示)

首先,一切都很好: 在此处输入图片说明

删除几个词后,我在行尾得到了一些剩余物: 在此处输入图片说明

此外,如果我一直按退格键,我什至可以删除部分提示: 在此处输入图片说明

按回车键(并将命令的结尾固定为[...], ev"),并在调整大小后,我得到了通常的混乱: 在此处输入图片说明

Oli*_*lac 6

您的提示包含颜色(可能还包含其他不可打印的内容)。

\[在 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"
Run Code Online (Sandbox Code Playgroud)

第二个周围的\[每个\]部分在更改颜色时不会在显示中添加任何可见字符,因此在计算提示上字符的位置时不应将其计算在内。

如果您修改了 PS1,请确保您没有放入\[可见\]字符。如果你这样做了,请将它们排除在外。

另一件事:您可能需要shopt -q -s checkwinsize确保终端大小在调整大小时更新。

而且您似乎使用的设置仅显示当前输入的命令行的末尾(类似于 ksh 所做的...您确定使用 bash 吗?):我将研究什么选项可以更改此设置,但我还不知道。

编辑

在聊天中我们还讨论了:

set -o emacs  #he was in vi mode
set -o posix  #he was in posix=no mode
Run Code Online (Sandbox Code Playgroud)

但最后似乎有效的是摆脱〜/ .inputrc(他这样做了,它可能包含一些错误的东西,但我不知道里面有什么,我也没有要求删除它^^只是重命名它) (或修复其内容)会有帮助......)


小智 0

您的终端设置(终端类型)是否正确?在我的 ubuntu Linux 主机上,我的终端类型是“ansi”。(您可以通过发出命令来获取此信息: echo $TERM )。终端类型必须与您正在使用的屏幕(控制台)匹配。有时,如果终端类型不正确,终端/控制台无法识别终端转义码(例如光标控制),因此屏幕输出会变得混乱。其他标准终端类型有“vt100”、“vt220”。