30

To clarify: I am not asking how to change the cursor when using vim within a terminal. I want the cursor to change when switching between input and command mode within Bash's vi-mode:

set -o vi <CR> type some text <ESC> (the cursor changes shape/color)
i (cursor change back) etc... 

I found a script that makes changing color of the cursor on mode changes possible in zsh, but I don't want to change shells just for this one feature.

Is there a way to get the cursor to change shape, like in gvim, (or even just color) when switching between command and insert modes on the Bash command line?

5

add these two lines to ~/.inputrc :

set vi-ins-mode-string \1\e[5 q\2
set vi-cmd-mode-string \1\e[2 q\2
  • 4
    This answer would be much more useful if you explain what they do and provide some context, e.g, do the settings require the user to have a particular version of Readline. – Anthony Geoghegan Dec 7 '17 at 22:01
  • :) here is the manual entry : gnu.org/software/bash/manual/html_node/… – holytrousers Dec 9 '17 at 1:27
  • 1
    This will only work on terminals and terminal emulators that understand DECSCUSR. – JdeBP Jun 13 '18 at 15:58
  • I had to add set show-mode-in-prompt on in ~/.inputrc also to make this work. – ronakg Jul 29 at 18:08
4

Copied from here - Command-line editing vim style

Should be available in bash 4.3 - see the changelog -

j.  New user-settable variable, show-mode-in-prompt, adds a characters to the
    beginning of the prompt indicating the current editing mode.

bash 4.3 is currently at the rc stage, you should be able to get packages for most distros without compiling from source, though not in the main repositories. Eg. Ubuntu (amd64) here and Debian (experimental repo) here.

  • Note that: the prompt will not be updated if you use a custom PS1 which contains a newline character. found here – pgericson Jun 26 '17 at 7:42
  • @pgericson It seems to work with a custom PS1 containing a newline for me on bash 4.4.12. – DuBistKomisch Jan 6 '18 at 22:08
2

I've not find a real solution, but perhaps this could help you (or someone else) to find a better one.

You have to create a script (e.g. kmtest.sh)

#!/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

and after add it to your PS1, something like:

export PS1="\u@\h \$(kmtest.sh)> "

but as I said, it is not what you want, cause it changes cursor color only after a cr. Good luck

gb

  • Did this actually work for you? It did not work for me; it just made the cursor red. I don't think kmtest.sh is run every time I switch between insert and command modes. It seems like it would only be run on the creation of the prompt. – joecan Nov 11 '11 at 2:42
1

It looks like there may be a way to do it through hacking readline. Perhaps we could push for it to get mainstreamed?

Here is my answer on SO https://stackoverflow.com/a/12201092/255961 which I will update if I get more info.

1

While it's not exactly what you want (dynamically changing the cursor), bash 4.4 / readline 7.0 will add support for dynamically changing the prompt . You will be able to specify custom mode indicators for insert/command modes (This is different then the current show-mode-in-prompt option which is hardcoded to use + and :).

Unfortunately these versions are currently in beta and don't seem to have all the bugs ironed out yet. They're also missing the ability to specify where in the prompt you'd like the mode indicator to occur.

In the interim I've published patched versions of bash 4.3 / readline 6.3 on github with this functionality.

0

I asked the same question a while back on AskUbuntu when I was transitioning from Gvim to Console Vim.

I received one answer but it involved toggling the gnome terminal settings, and these settings applied across terminals. Thus, it was possible for the gnome terminal cursor to be stuck in the wrong state at times.

I'm still interested in whether you get a satisfactory solution, but over time I've adjusted to the lack of mode state information in the cursor. Perhaps I just press escape more often (e.g., esc to ensure I'm in command-mode; esc i to ensure I'm in insert mode); perhaps I look at the state displayed at the bottom of the screen; perhaps I've just internalised the mode.

  • 3
    Just to clarify, you're question is not the same as mine. You asked how to change the cursor within Vim on the console. This is well documented on the Vim wiki. I want to know how to do this in Bash's vi-mode. – joecan Oct 13 '11 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.