2017-06-14 46 views
3

我在我的.bashrc以下行:如何根据Bash中的VI模式更改光标形状?

set -o vi 

而且我想我的光标有一个管状当我在插入模式和块形状,当我在命令模式下,像我会在Vim的,如果我把下面在我的.vimrc:

let &t_SI = "\e[6 q" 
let &t_SR = "\e[4 q" 
let &t_EI = "\e[2 q" 

除了在这种情况下,我想有在命令行上等价的行为。

我找到了部分答案在这里我的问题 - https://unix.stackexchange.com/questions/22527/change-cursor-shape-or-color-to-indicate-vi-mode-in-bash - 由@gogolb写的。

下面是答案,复制:

#!/bin/bash 
# Script "kmtest.sh" 

TEST=`bind -v | awk '/keymap/ {print $NF}'` 
if [ "$TEST" = 'vi-insert' ]; then 
    echo -ne "\033]12;Green\007" 
else 
    echo -ne "\033]12;Red\007" 
fi 

export PS1="\[email protected]\h \$(kmtest.sh)> " 

但不幸的是,在回答解释,示例脚本只回车后改变光标形状,反之,我要的是游标形状改变当我点击<Esc>(即当我改变模式)。

我在Linux上运行的本地端APP,用猛砸4.4.7和我的$ TERM变量设置为xterm方式256color。此外,我不知道tmux是否对我所要求的功能有任何影响,但理想情况下,我希望解决方案能够在tmux会话内外使用。


SOLUTION

我最终发现了这个问题的答案我自己,我描述了另一个问题,我在这里公布:

How to correctly link patched GNU readline library to all existing programs?

别担心,该解决方案不需要任何修补。 ;)

+0

您应该让您的解决方案成为答案 –

+0

我该怎么做? – jinscoe123

+0

在下面给你自己的问题添加一个答案,然后在任何等待时间(我认为这是一天或什么)后,你可以接受它作为你的问题的答案。你的解决方案对我有帮助,所以我很乐意通过updoot向你扔一些虚假的互联网点。 :-) –

回答

1

SOLUTION:

我张贴我的答案在这里我自己的问题的建议。


该解决方案适用于击4.4+,因为,从该版本击,GNU readline库的版本7.0的被使用,其包括vi-cmd-mode-stringvi-ins-mode-string变量的必要添加。

这些变量可以在您的中设置如下。为了实现该功能,上述我inputrc中文件:

set show-mode-in-prompt on 
set vi-cmd-mode-string "\1\e[2 q\2" 
set vi-ins-mode-string "\1\e[6 q\2" 



交代:

对于那些谁在上面的解决方案是如何工作真正感兴趣。


这两个变量,vi-cmd-mode-stringvi-ins-mode-string,被印刷到终端,以便在命令提示一起提供一种视觉指示器,以您目前在(即命令模式与插入模式的模式)。

对于这两个变量的默认值是“(CMD)”和“(INS)”为命令和分别插入模式。所以,如果你是刚刚离开他们的默认值,有,比方说,PS1='>>>'命令提示符,然后你提示看起来像下面这样:

  • 命令模式:

    (cmd) >>> 
    
  • 插入模式:

    (ins) >>> 
    


Accordin g到readline的手册页(见下文),您还可以通过在\ 1和\ 2转义字符之间嵌入序列来指定不可打印的字符,例如终端控制序列。

vi-cmd-mode-string ((cmd)) 
     This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in command mode. The value is expanded like a key binding, so the 
     standard set of meta- and control prefixes and backslash escape sequences is available. Use the \1 and \2 escapes to begin and end sequences of non-printing characters, which can be 
     used to embed a terminal control sequence into the mode string. 
vi-ins-mode-string ((ins)) 
     This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in insertion mode. The value is expanded like a key binding, so the 
     standard set of meta- and control prefixes and backslash escape sequences is available. Use the \1 and \2 escapes to begin and end sequences of non-printing characters, which can be 
     used to embed a terminal control sequence into the mode string. 


因此,在我的上述方案中,我嵌入终端控制序列\e[2 q(使光标竖线)和\e[6 q(使光标的配管),这些\ 1和\ 2之间转义字符,导致我的光标在命令模式下具有垂直条形状,而在插入模式下时具有管状形状。