When I use Ctrl+R to search, the text "(reverse-i-search)" appears in the command
I don't know how to explain this one. When I use the Ctrl+R command in bash to reverse-search through commands, the actual text of the reverse search appears in the command itself.
E.g., if I type Ctrl+R hello
to search for the keyword hello in historic commands, I see this:
DESKTOP-NHBC022@quant:~$ echo "hello world"
hello world
(reverse-i-search)`hello': echo "hello world"
So far so good, but suppose I now want to "edit" the command before executing it. If I press just Enter it's fine, and executes the command, but if I press the → (right arrow) or End key to move the cursor, the text (reverse-i-search)hello
bit becomes part of the text:
DESKTOP-NHBC022@quant:~$ (reverse-i-search)`hello ': echo "hello world"
EDIT: I have a custom PS1
environment variable in my .bashrc
. If I remove this, the problem goes away. So I guess my question is, what's wrong with this?
export EDITOR=vim
alias vi=vim
alias ls='ls --color=auto'
set editing-mode vi
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
export TERM='xterm-256color'
_GREEN=$(tput setaf 2)
_BLUE=$(tput setaf 4)
_RED=$(tput setaf 1)
_RESET=$(tput sgr0)
_YELLOW=$(tput setaf 3)
_BOLD=$(tput bold)
export PS1="${_GREEN}\h${_YELLOW}@${_RED}\u${_RESET}${_YELLOW}:${_RESET}\w${_RESET}\$ "
Your question has the same answer as this one: Bash terminal not wrapping text correctly. The answer is "wrap the tput
output in \[ \]
". The other question does not actually use tput
, so in case you had trouble with adjusting the solution to your exact setup, I'm posting this community wiki answer instead of voting to close your question as duplicate.
Modify your code in .bashrc
like this:
_GREEN="\[$(tput setaf 2)\]"
_BLUE="\[$(tput setaf 4)\]"
and so on, fix all the variables that use output from tput
. Your export PS1=…
line does not need to be modified then. Start a new interactive shell. The problem should now be fixed.