title: How to change cursor shape depending on VI mode in Bash?
body: <p>I have the following line in my .bashrc:</p>
  
  <pre><code>set -o vi
  </code></pre>
  
  <p>And I want my cursor to have a pipe shape when I am in insert mode and a block shape when I am in command mode, like what I would have in Vim if I placed the following in my .vimrc:</p>
  
  <pre><code>let &amp;t_SI = "\e[6 q"
  let &amp;t_SR = "\e[4 q"
  let &amp;t_EI = "\e[2 q"
  </code></pre>
  
  <p>Except in this case I want to have the equivalent behavior on the command line.
  <br />
  <br /></p>
  
  <p>I found a partial answer to my question here -
   <a href="https://unix.stackexchange.com/questions/22527/change-cursor-shape-or-color-to-indicate-vi-mode-in-bash">https://unix.stackexchange.com/questions/22527/change-cursor-shape-or-color-to-indicate-vi-mode-in-bash</a> - written by @gogolb.</p>
  
  <p>Here is the answer, copied:</p>
  
  <pre><code>#!/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="\u@\h \$(kmtest.sh)&gt; "
  </code></pre>
  
  <p>Unfortunately, though, as explained in the answer, the example script only changes the cursor shape after a carriage return, whereas, what I want is for the cursor shape to change when I hit &lt;Esc&gt; (i.e. when I change mode).
  <br />
  <br /></p>
  
  <p>I am on Linux running the native terminal app, with Bash 4.4.7 and my $TERM variable set to xterm-256color. Also, I do not know if tmux has any effect on what I am asking for, but I, ideally, would like the solution to work both within and exterior to tmux sessions.</p>
  
  <p><br />
  <strong>SOLUTION</strong></p>
  
  <p>I ended up discovering the answer to this question myself, which I describe here in another question I posted:</p>
  
  <p><a href="https://stackoverflow.com/questions/45136948/how-to-correctly-link-patched-gnu-readline-library-to-all-existing-programs">How to correctly link patched GNU readline library to all existing programs?</a></p>
  
  <p>Don't worry, the solution does not require any patching. ;)</p>