Vim Tips Wiki
Register
No edit summary
No edit summary
Tag: Visual edit
 
(38 intermediate revisions by 22 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
To change the shape of the cursor in different modes, you can add the following into your vimrc.
+
To change the shape of the cursor in different modes, you can add the following into your vimrc:
   
==For the Gnome-Terminal (version 2.26)==
+
== For Terminal on macOS ==
 
<pre>
 
<pre>
  +
"Mode Settings
if has("autocmd")
 
au InsertEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
 
au InsertLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"
 
au VimLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
 
endif
 
</pre>
 
   
  +
let &t_SI.="\e[5 q" "SI = INSERT mode
If you use more than one profile in gnome-terminal, you might have to adapt this to your profiles.
 
  +
let &t_SR.="\e[4 q" "SR = REPLACE mode
  +
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)
   
  +
"Cursor settings:
==For xfce-terminal==
 
  +
  +
" 1 -> blinking block
  +
" 2 -> solid block
  +
" 3 -> blinking underscore
  +
" 4 -> solid underscore
  +
" 5 -> blinking vertical bar
  +
" 6 -> solid vertical bar</pre>
  +
 
== For iTerm2 on OS X ==
 
<pre>
 
<pre>
  +
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
if has("autocmd")
 
  +
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
au InsertEnter * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_BLOCK/TERMINAL_CURSOR_SHAPE_UNDERLINE/' ~/.config/Terminal/terminalrc"
 
  +
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
au InsertLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/Terminal/terminalrc"
 
 
</pre>
au VimLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/Terminal/terminalrc"
 
  +
endif
 
  +
== For tmux running in iTerm2 on OS X ==
</pre>
 
  +
<pre>
  +
let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
  +
let &t_SR = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=2\x7\<Esc>\\"
  +
let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
  +
</pre>
  +
In theory, the escape sequence could be used for tmux in any terminal.
  +
  +
'''NOTE''': These do not work with the default terminal.
  +
 
==For Konsole in KDE4==
 
==For Konsole in KDE4==
 
<pre>
 
<pre>
 
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
 
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
  +
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
 
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
 
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
 
</pre>
 
</pre>
Line 40: Line 56:
 
This works with multiple tabs and windows.
 
This works with multiple tabs and windows.
   
  +
== For VTE compatible terminals (urxvt, st, xterm, gnome-terminal 3.x, Konsole KDE5 and others), wsltty and Windows Terminal ==
==For iTerm2 on Mac OS X ==
 
 
<pre>
 
<pre>
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
+
let &t_SI = "\<Esc>[6 q"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
+
let &t_SR = "\<Esc>[4 q"
  +
let &t_EI = "\<Esc>[2 q"
 
</pre>
 
</pre>
   
  +
Set IBeam shape in insert mode, underline shape in replace mode and block shape in normal mode.
This does not work with Terminal
 
  +
  +
==For the Gnome-Terminal (version 2.26)==
  +
This makes your cursor change in all open terminals. Upgrade your terminal and use the version above instead.<pre>
 
if has("autocmd")
 
au InsertEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
 
au InsertLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"
 
au VimLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
 
endif
  +
</pre>
  +
  +
==For the Gnome-Terminal (version 3.x)==
  +
First make a script gnome-terminal-cursor-shape.sh
  +
<pre>
  +
#!/bin/sh
  +
DEFAULTPROF=`dconf read /org/gnome/terminal/legacy/profiles:/default`
  +
DEFAULTPROF=`echo "$DEFAULTPROF" | sed -e "s/^'/:/" -e "s/'$//"`
  +
dconf write /org/gnome/terminal/legacy/profiles:/$DEFAULTPROF/cursor-shape "'$1'"
  +
</pre>
  +
Make it executable & put it in /usr/local/bin
  +
Next, add this to .vimrc
  +
<pre>
 
if has("autocmd")
  +
au InsertEnter *
  +
\ if v:insertmode == 'i' |
  +
\ silent execute "!gnome-terminal-cursor-shape.sh ibeam" |
  +
\ elseif v:insertmode == 'r' |
  +
\ silent execute "!gnome-terminal-cursor-shape.sh underline" |
  +
\ endif
  +
au InsertLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
  +
au VimLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
 
endif
  +
</pre>
  +
 
If you use more than one profile in gnome-terminal, you might have to adapt this to your profiles.
  +
  +
== For the Gnome-Terminal (version ≥3.16) ==
  +
This method has the advantage compared to the method above that it influences the given tab only, and not all the tabs that use the given profile. Add the following into .vimrc
  +
if has("autocmd")
  +
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[2 q"' | redraw!
  +
au InsertEnter,InsertChange *
  +
\ if v:insertmode == 'i' |
  +
\ silent execute '!echo -ne "\e[6 q"' | redraw! |
  +
\ elseif v:insertmode == 'r' |
  +
\ silent execute '!echo -ne "\e[4 q"' | redraw! |
  +
\ endif
  +
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
  +
endif
  +
Above snippet disables cursor blinking. If you want cursor blink, just decrement all the 3 numbers above 2,4,6 by one each.
  +
if has("autocmd")
  +
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
  +
au InsertEnter,InsertChange *
  +
\ if v:insertmode == 'i' |
  +
\ silent execute '!echo -ne "\e[5 q"' | redraw! |
  +
\ elseif v:insertmode == 'r' |
  +
\ silent execute '!echo -ne "\e[3 q"' | redraw! |
  +
\ endif
  +
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
  +
endif
  +
 
==For xfce-terminal==
  +
see VTE above, the same does work on xfce4-terminal (as it is VTE-based)<pre>
  +
if has("autocmd")
 
au InsertEnter * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_BLOCK/TERMINAL_CURSOR_SHAPE_UNDERLINE/' ~/.config/xfce4/terminal/terminalrc"
 
au InsertLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"
 
au VimLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"
  +
endif
 
</pre>
   
 
==See also==
 
==See also==
Line 52: Line 136:
   
 
==Comments==
 
==Comments==
  +
VTE compatible terminals includes mate-terminal 1.18.1.
  +
 
In a vim_use thread on 31 March 2009, Matt Wozniski made the [http://groups.google.com/group/vim_use/browse_thread/thread/91619df7d7f3fdc5 following comments]:
 
In a vim_use thread on 31 March 2009, Matt Wozniski made the [http://groups.google.com/group/vim_use/browse_thread/thread/91619df7d7f3fdc5 following comments]:
   
Line 60: Line 146:
 
Both will probably have problems with multiple tabs.
 
Both will probably have problems with multiple tabs.
 
----
 
----
  +
In konsole 18.12.3 the above "KDE4" code works AFAICT. In multiple konsole tabs, Vim in one tab has no influence on Vim in another tab, which is correct. In multiple Vim tabs, cursor shape changes for all tabs, which agrees with the --INSERT--, --REPLACE-- or empty label in the command-line area. (FWIW I don't use any of those terminal multiplexers.) [[User:Tonymec|Tonymec]] ([[User talk:Tonymec|talk]]) 22:40, May 28, 2019 (UTC)

Latest revision as of 00:59, 10 June 2022

Tip 1622 Printable Monobook Previous Next

created March 28, 2009 · complexity basic · author Weltensegler · version 7.0


To change the shape of the cursor in different modes, you can add the following into your vimrc:

For Terminal on macOS[]

"Mode Settings

let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)

"Cursor settings:

"  1 -> blinking block
"  2 -> solid block 
"  3 -> blinking underscore
"  4 -> solid underscore
"  5 -> blinking vertical bar
"  6 -> solid vertical bar

For iTerm2 on OS X[]

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

For tmux running in iTerm2 on OS X[]

let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
let &t_SR = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=2\x7\<Esc>\\"
let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"

In theory, the escape sequence could be used for tmux in any terminal.

NOTE: These do not work with the default terminal.

For Konsole in KDE4[]

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

This works with multiple tabs and windows.

For VTE compatible terminals (urxvt, st, xterm, gnome-terminal 3.x, Konsole KDE5 and others), wsltty and Windows Terminal[]

let &t_SI = "\<Esc>[6 q"
let &t_SR = "\<Esc>[4 q"
let &t_EI = "\<Esc>[2 q"

Set IBeam shape in insert mode, underline shape in replace mode and block shape in normal mode.

For the Gnome-Terminal (version 2.26)[]

This makes your cursor change in all open terminals. Upgrade your terminal and use the version above instead.

if has("autocmd")
  au InsertEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
  au InsertLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"
  au VimLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
endif

For the Gnome-Terminal (version 3.x)[]

First make a script gnome-terminal-cursor-shape.sh

#!/bin/sh
DEFAULTPROF=`dconf read /org/gnome/terminal/legacy/profiles:/default`
DEFAULTPROF=`echo "$DEFAULTPROF" | sed -e "s/^'/:/" -e "s/'$//"`
dconf write /org/gnome/terminal/legacy/profiles:/$DEFAULTPROF/cursor-shape "'$1'"

Make it executable & put it in /usr/local/bin Next, add this to .vimrc

if has("autocmd")
    au InsertEnter *
        \ if v:insertmode == 'i' |
        \   silent execute "!gnome-terminal-cursor-shape.sh ibeam" |
        \ elseif v:insertmode == 'r' |
        \   silent execute "!gnome-terminal-cursor-shape.sh underline" |
        \ endif
    au InsertLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
    au VimLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
endif

If you use more than one profile in gnome-terminal, you might have to adapt this to your profiles.

For the Gnome-Terminal (version ≥3.16)[]

This method has the advantage compared to the method above that it influences the given tab only, and not all the tabs that use the given profile. Add the following into .vimrc

if has("autocmd")
  au VimEnter,InsertLeave * silent execute '!echo -ne "\e[2 q"' | redraw!
  au InsertEnter,InsertChange *
    \ if v:insertmode == 'i' | 
    \   silent execute '!echo -ne "\e[6 q"' | redraw! |
    \ elseif v:insertmode == 'r' |
    \   silent execute '!echo -ne "\e[4 q"' | redraw! |
    \ endif
  au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

Above snippet disables cursor blinking. If you want cursor blink, just decrement all the 3 numbers above 2,4,6 by one each.

if has("autocmd")
  au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
  au InsertEnter,InsertChange *
    \ if v:insertmode == 'i' | 
    \   silent execute '!echo -ne "\e[5 q"' | redraw! |
    \ elseif v:insertmode == 'r' |
    \   silent execute '!echo -ne "\e[3 q"' | redraw! |
    \ endif
  au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

For xfce-terminal[]

see VTE above, the same does work on xfce4-terminal (as it is VTE-based)

if has("autocmd")
  au InsertEnter * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_BLOCK/TERMINAL_CURSOR_SHAPE_UNDERLINE/' ~/.config/xfce4/terminal/terminalrc"                                                                                          
  au InsertLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"                                                                                          
  au VimLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_UNDERLINE/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"  
endif

See also[]

Comments[]

VTE compatible terminals includes mate-terminal 1.18.1.

In a vim_use thread on 31 March 2009, Matt Wozniski made the following comments:

Re the Gnome code: Unless I'm misunderstanding something, this would not only work improperly when combined with tools like screen and dvtm, but also, when you have multiple terminals open, it would change the cursor shape in all of them!

Re the KDE4 code: That won't play nicely with gnu screen, or with dvtm, or any of the other terminal multiplexers out there.

Both will probably have problems with multiple tabs.


In konsole 18.12.3 the above "KDE4" code works AFAICT. In multiple konsole tabs, Vim in one tab has no influence on Vim in another tab, which is correct. In multiple Vim tabs, cursor shape changes for all tabs, which agrees with the --INSERT--, --REPLACE-- or empty label in the command-line area. (FWIW I don't use any of those terminal multiplexers.) Tonymec (talk) 22:40, May 28, 2019 (UTC)