正确使用向上和向下键来检查向上和向下箭头

时间:2014-09-01 12:52:00

标签: bash .bash-profile

我在使用gnome-terminal上下来时遇到了一个问题。 让我们说我以前的命令是

vi pos_of_rightmost_setbit.cpp 
gcc -o harsha pos_of_rightmost_setbit.cpp

此时按下向上箭头,我看到上一个上一个命令gcc -o harsha pos_of_rightmost_setbit.cpp,这是完美的。

但是当我再次点击箭头时,我必须看到最后一个但前一个命令vi pos_of_rightmost_setbit.cpp,而不是我看到gcc -o harvightmost_setbit.cpp

当我在这个时间点击输入时,命令vi pos_of_rightmost_setbit.cpp工作正常,所以我认为bashrc设置存在一些问题。

我附上截图以便更好地理解。 For previous command

For last but one previous command

1 个答案:

答案 0 :(得分:1)

那个提示字符串就是问题所在。

您需要用\[\]包围该提示字符串的每个非打印位,以告诉readline不要在提示的长度内计算它们。

所以你应该有类似PS1="\[\e[1;39m\][\u@\h \W]\$ \[\e[m\]"的东西。

参考:https://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt/301355#301355