I would like to know how to change, if possible, the cursor in Vim (in color, shape, etc.) depending on what mode you are in.
I am constantly forgetting that I am not
Not sure if anyone else is facing a delay after hitting the Esc key to go back to normal mode to show the block cursor but if so, this is the way I fix it too.
Actually I'm using iTerm2 and using Vim inside my terminal on macOS. And when entering to insert mode, the cursor still being a block and is kind of confusing when you are at insert mode or normal mode.
I wanted to show a thin line as cursor when in insert mode and back to block when in normal mode as MacVim does. And to do so it's pretty simple, just added this to my .vimrc
file as described here:
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
But as you can see there was a delay when hitting ESC
to exit insert mode back to normal mode and show the block as cursor again. So to fix it I found this:
set ttimeout
set ttimeoutlen=1
set listchars=tab:>-,trail:~,extends:>,precedes:<,space:.
set ttyfast
And now it works pretty fine as you can see:
I hope it could help any one else!
The following works in xterm, urxvt, and other terminal emulators on Linux; iTerm2 on macOS; Git Bash with ConEmu on Windows; and more (see comments):
let &t_SI = "\e[6 q"
let &t_EI = "\e[2 q"
" Optionally reset the cursor on start:
augroup myCmds
au!
autocmd VimEnter * silent !echo -ne "\e[2 q"
augroup END
Other options (replace the number after \e[
):
Ps = 0 -> blinking block.
Ps = 1 -> blinking block (default).
Ps = 2 -> steady block.
Ps = 3 -> blinking underline.
Ps = 4 -> steady underline.
Ps = 5 -> blinking bar (xterm).
Ps = 6 -> steady bar (xterm).
When you use tmux, it is important to use it like that (without the \<Esc>Ptmux;
escape). tmux will keep track of the correct cursor shape when you switch windows/panes.
If it does not work for you, try either to set TERM=xterm-256color
before starting tmux, or add this to your .tmux.conf
(thanks @Steven Lu):
set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'
I don't think this adds much to the answers that other people have already provided but I wanted to somehow summarise things in one place and also have links to the relevant references.
This is what the relevant snippet from my .vimrc
looks like:
" Cursor appearance
"
" See also: [1]'ANSI Control Functions Summary', [2]DECSCUSR, [3]xterm+tmux
" entry in terminfo.src.
" [1] https://www.vt100.net/docs/vt510-rm/chapter4.html
" [2] https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
" [3] https://raw.githubusercontent.com/mirror/ncurses/master/misc/terminfo.src
"
" On:
" - entered insert mode
let &t_SI = "^[[5 q^[]12;Magenta\007" " blinking bar (Ss) in magenta (Cs)
" - entered replace mode
let &t_SR = "^[[0 q^[]12;Red\007" " blinking block (Ss) in red (Cs)
" - leaving insert/replace mode
let &t_EI = "^[[2 q^[]112\007" " terminal power-on style (Se) and colour (Cr)
Note: The '^['
characters are actually one ESC
(escape sequence) control character.
This works properly on xfce4-terminal:
add following script to your .vimrc
if has("autocmd")
au InsertEnter * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_BLOCK/TERMINAL_CURSOR_SHAPE_IBEAM/' ~/.config/xfce4/terminal/terminalrc"
au InsertLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_IBEAM/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"
au VimLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_IBEAM/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"
endif
Brief:
As You know xfce4-terminal keeps preferences in .config/xfce4/terminal/terminalrc
file. The script changes TERMINAL_CURSOR_SHAPE_BLOCK
to TERMINAL_CURSOR_SHAPE_IBEAM
when you're in insert mode, and back to block when you leave insert mode or vim. Feel free to change IBEAM
to anything you want (BLOCK
, IBEAM
and UNDERLINE
available).
A popular approach to indicate switching to and from Insert mode is
toggling the cursorline
option, which is responsible for whether
the current screen line is highlighted (see :help cursorline
):
:autocmd InsertEnter,InsertLeave * set cul!
or, alternatively:
:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul
Modify the CursorLine
highlighting group to change the styling
of the cursor line to your liking (see :help :highlight
and
:help highlight-groups
).
If you are using a modern version of nvim
and you wanted to achieve this, you can avoid some of these fancy workarounds listed above.
The below settings will switch from block cursor in normal mode, to underline cursor in replace to line cursor in insert.
# ~/.tmux.conf
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",*256col*:Tc"
set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q',w
" ~/.vimrc
" Sets cursor styles
" Block in normal, line in insert, underline in replace
set guicursor=n-v-c-sm:block,i-ci-ve:ver25-Cursor,r-cr-o:hor20
I managed to get this working with the following settings pulled from these two sources.
tui-cursor-shape
guicursor