5

At home I use iTerm2 on MacOS X, and at work, I have a laptop with CygWin. I have another PC where I use gnome-terminal or terminator, and sometimes I use tmux.

I'm looking to configure the cursor shape in Vim in a way that will work in all above terminals emulators. I want the cursor to be a upright bar in insert mode and solid block in normal mode.

Anyone can help here? It's really uncomfortable when the cursor's shape is inconsistent when I switch between all my devices.

Below is the my current vimrc for cursur shape change, I got it from an online example, but it doesn't work in gnome-teriminal, terminator, tmux:

if &term == "^xterm" || &term == "^screen"
    let &t_SI = "\e[5 q"
    let &t_EI = "\e[2 q"
endif

if exists("$TMUX")
    let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
    let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
endif
  • 1
    I'm pretty sure you want to at least change if &term == "^xterm" to if &term =~ "^xterm" and &term =~ "^screen" Changing this makes it work in xterm and xterm+tmux for me. I commented out the second part as this changed my terminal's font(!). Didn't test all the other terminals. – Martin Tournoij Jun 29 '15 at 8:24
  • It looks like the tmux part is sending Escape code 50 (<Esc>]50), which is a OSX specific thing. See: Escape code 50 in xterm. – Martin Tournoij Jun 29 '15 at 8:27
  • vi.stackexchange.com/a/3456/1145 Very similar. – Alex Kroll Jun 29 '15 at 8:58
  • 1
    You can try the togglecursor plugin. Not sure if it works with cygwin, though. I'd recommend skimming over the help file for more info on what terminals are supported. – tokoyami Jun 29 '15 at 9:49
7

As of November 2017, all the terminals you are using support the same DECSCUSR escape sequences for changing the cursor shape1. So you don't need to test for the different terminals.

As such, the only thing that requires different treatment is tmux, which will only forward escape sequences on to the terminal when surrounded by a DCS sequence. You already had this, but in that branch you were passing escape sequences that would only work in iTerm2.

Thus:

if exists('$TMUX')
  let &t_SI = "\ePtmux;\e\e[5 q\e\\"
  let &t_EI = "\ePtmux;\e\e[2 q\e\\"
else
  let &t_SI = "\e[5 q"
  let &t_EI = "\e[2 q"
endif

1: N.B. By "Terminator" I presume you are referring to GNOME Terminator and not the Java-based terminal. Cygwin isn't a terminal: its default terminal, mintty emulates xterm and so should support this. Earlier versions of iTerm didn't support DECSCUSR, but later versions do. GNOME terminal also added support more recently.
2: And Konsole (and possibly others), but that's not one of the terminals you've said you use.

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.