2017-06-14 319 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之間轉義字符,導致我的光標在命令模式下具有垂直條形狀,而在插入模式下時具有管狀形狀。