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"
" reset the cursor on start (for older versions of vim, usually not required)
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'