36

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?

1

6 Answers 6

10

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
5
  • 8
    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. Commented Dec 7, 2017 at 22:01
  • 2
    :) here is the manual entry : gnu.org/software/bash/manual/html_node/… Commented Dec 9, 2017 at 1:27
  • 1
    This will only work on terminals and terminal emulators that understand DECSCUSR.
    – JdeBP
    Commented Jun 13, 2018 at 15:58
  • 3
    I had to add set show-mode-in-prompt on in ~/.inputrc also to make this work.
    – ronakg
    Commented Jul 29, 2019 at 18:08
  • 1
    Okay, so \1 and \2 are to begin and end the escape sequence. \e[5 and \e[2 to set the cursor mode (blinking bar and steady block in this case). But what does the q do?
    – winklerrr
    Commented Feb 24, 2020 at 10:26
5

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.

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

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

1
  • 1
    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
    Commented Nov 11, 2011 at 2:42
2

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

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

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.

1
  • 4
    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
    Commented Oct 13, 2011 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .