Vim Tips Wiki
m (Added to Syntax Category + code reformated + help link fixed)
(27 intermediate revisions by 14 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=746
 
|id=746
  +
|previous=745
|title=change cursor color in different modes
 
  +
|next=747
|created=June 16, 2004 14:27
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=legba
 
|author=legba
|version=5.7
+
|version=6.0
 
|rating=26/18
 
|rating=26/18
  +
|category1=
|text=
 
  +
|category2=
Sometimes I think I am in insert mode and start writing, then I realize I was in some other mode when weird thing happen. So I've set a different color and blinking rate for insert mode and other modes to see at a glance in what mode I am (works only with GUI):
 
 
}}
  +
Vim allows the cursor shape, blink rate, and color to be customized, if supported by the underlying system. Depending upon your system, you can make the cursor more prominent using blinking and a distinctive color, or you can make the cursor less distracting by disabling blinking and using a bland color. In the GUI (gvim), the cursor can be fully customized. For example, the cursor color can be changed when entering insert mode to clarify whether you are in normal or insert mode.
  +
  +
==Changing the cursor color in insert mode==
  +
You may start typing, thinking you are in insert mode, but find that the characters are interpreted as commands because you are actually in normal mode. To help avoid that problem, you can specify that the cursor color and blink rate change when entering insert mode.
  +
  +
Using gvim with the defaults, the cursor shape is a block when in <code>n-v-c</code> modes (normal mode, or visual selection mode, or command mode while entering a colon command), and the shape changes to a vertical bar when in <code>i</code> (insert) mode. The color and blink rates do not change.
  +
  +
Here is an example for gvim showing how to customize the cursor properties (see {{help|'guicursor'}}):
 
<pre>
 
<pre>
highlight Cursor guifg=white guibg=black
+
highlight Cursor guifg=white guibg=black
highlight iCursor guifg=white guibg=steelblue
+
highlight iCursor guifg=white guibg=steelblue
set guicursor=n-v-c:block-Cursor
+
set guicursor=n-v-c:block-Cursor
set guicursor+=i:ver100-iCursor
+
set guicursor+=i:ver100-iCursor
set guicursor+=n-v-c:blinkon0-Cursor
+
set guicursor+=n-v-c:blinkon0
set guicursor+=i:blinkwait20-iCursor
+
set guicursor+=i:blinkwait10
 
</pre>
 
</pre>
   
  +
Line 1 defines the color highlighting used for <code>n-v-c</code> modes (set in line 3), and line 2 defines a different color for insert mode (set in line 4). Line 5 disables blinking (<code>blinkon</code> value <code>0</code>) for <code>n-v-c</code> modes, and line 6 increases the default blink rate for insert mode. Line 4 also sets the cursor shape to a 100% sized vertical bar for insert mode (the default is <code>ver25</code>, a 25% vertical bar. When using ver100 vim doesn't take the guifg parameter. It is better to use block instead).
see {{help|'guicursor'}} for more options.
 
}}
 
   
  +
It is possible to change the cursor color and style in the terminal if it understands the following escape sequences. Not all terminals support this, but <code>xterm</code>, <code>rxvt</code> and Terminator do. Recent versions of gnome-terminal support the sequence to change color, but not the one to restore the color to the default. Add the following to <code>~/.vimrc</code>:
== Comments ==
 
  +
<pre>
Could be nice if this was supported on Mac OS X.
 
  +
if &term =~ "xterm\\|rxvt"
  +
" use an orange cursor in insert mode
  +
let &t_SI = "\<Esc>]12;orange\x7"
  +
" use a red cursor otherwise
  +
let &t_EI = "\<Esc>]12;red\x7"
  +
silent !echo -ne "\033]12;red\007"
  +
" reset cursor when vim exits
  +
autocmd VimLeave * silent !echo -ne "\033]112\007"
  +
" use \003]12;gray\007 for gnome-terminal
  +
endif
  +
</pre>
   
  +
And changing the cursor shape.
amix--AT--amix.dk
 
  +
, June 16, 2004 16:26
 
  +
<pre>
----
 
  +
if &term =~ '^xterm'
Is there a simple way to also change the background (screen) color?
 
  +
" solid underscore
Something like
 
  +
let &t_SI .= "\<Esc>[4 q"
set guibackground=n-v-c:myBackgroundColor
 
  +
" solid block
set guibackground=i:myOtherBackgroundColor
 
  +
let &t_EI .= "\<Esc>[2 q"
  +
" 1 or 0 -> blinking block
  +
" 3 -> blinking underscore
  +
" Recent versions of xterm (282 or above) also support
  +
" 5 -> blinking vertical bar
  +
" 6 -> solid vertical bar
  +
endif
  +
</pre>
  +
  +
If Vim is running in a Gnome-terminal, the cursor shape can be changed as follows:
  +
  +
<pre>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 block"
  +
au VimEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"</pre>
  +
  +
'''Don't do this! This is using a global setting for a local problem -- it changes the cursor shape in all open gnome-terminal windows, not just the one running vim.'''
  +
  +
The first two lines change the cursor to ibeam/block as it enters/leaves insert mode, the last 2 lines ensure the terminal cursor is independent of vim cursor setting.
  +
  +
==Disabling cursor blinking==
  +
The <code>'guicursor'</code> option can be used to set the cursor properties when working in a GUI (gvim). The following commands illustrate how to disable blinking for all modes (the "<code>a</code>" specifies all modes, and the <code>0</code> value for <code>blinkon</code> disables blinking):
  +
<pre>
  +
" Disable all blinking:
  +
:set guicursor+=a:blinkon0
  +
" Remove previous setting:
  +
:set guicursor-=a:blinkon0
  +
" Restore default setting:
  +
:set guicursor&
  +
</pre>
  +
  +
You can also enter a command to directly set <code>guicursor</code> to disable blinking for the <code>n-v-c</code> modes. Type the following:
  +
<pre>
  +
:set guicursor=
  +
</pre>
  +
  +
then press the Tab key (see {{help|'wildchar'}}) to have the current value of <code>'guicursor'</code> displayed in the command line. You should see something like this (the <code>...</code> indicates text omitted from this example):
  +
<pre>
  +
:set guicursor=n-v-c:block-Cursor/lCursor,...
  +
</pre>
  +
  +
After the first colon, add "<code>blinkon0-</code>" so it reads:
  +
<pre>
  +
:set guicursor=n-v-c:blinkon0-block-Cursor/lCursor,...
  +
</pre>
  +
  +
Alternatively, you can use the <code>:let</code> command with the <code>&guicursor</code> variable to set the <code>'guicursor'</code> option. The following command inserts the "<code>blinkon0-</code>" using a substitute (in the replacement, <code>&</code> represents the original text, that is, <code>'n-v-c:'</code>):
  +
<pre>
  +
let &guicursor = substitute(&guicursor, 'n-v-c:', '&blinkon0-', '')
  +
</pre>
  +
  +
==See also==
 
*[[Change cursor shape in different modes]]
  +
 
==Comments==
  +
In KDE 4.7 Konsole terminal emulator this works for changing cursor color background:
  +
<pre>
  +
" orange in insert mode, red in command mode
  +
" if you want to use rgb color formatting:
  +
" konsoleprofile CustomCursorColor=#255255255
  +
autocmd VimEnter * silent !konsoleprofile UseCustomCursorColor=1
  +
let &t_SI = "\<Esc>]50;CustomCursorColor=orange;BlinkingCursorEnabled=1\x7"
  +
let &t_EI = "\<Esc>]50;CustomCursorColor=red;BlinkingCursorEnabled=0\x7"
  +
silent !konsoleprofile CustomCursorColor=red
  +
autocmd VimLeave * silent !konsoleprofile CustomCursorColor=gray;BlinkingCursorEnabled=0
  +
</pre>
   
  +
Additionally it's blinking in insert mode but not in command mode.
'''Anonymous'''
 
  +
It changes cursor color "back" to gray after leaving Vim.
, June 18, 2004 9:20
 
----
 
<!-- parsed by vimtips.py in 0.490063 seconds-->
 
   
  +
If it's still not working, check if you set the TERM variable to "xterm-256color" instead of "xterm" in .bashrc for example. --May 5, 2013
[[Category:Syntax]]
 

Revision as of 14:09, 31 October 2013

Tip 746 Printable Monobook Previous Next

created 2004 · complexity basic · author legba · version 6.0


Vim allows the cursor shape, blink rate, and color to be customized, if supported by the underlying system. Depending upon your system, you can make the cursor more prominent using blinking and a distinctive color, or you can make the cursor less distracting by disabling blinking and using a bland color. In the GUI (gvim), the cursor can be fully customized. For example, the cursor color can be changed when entering insert mode to clarify whether you are in normal or insert mode.

Changing the cursor color in insert mode

You may start typing, thinking you are in insert mode, but find that the characters are interpreted as commands because you are actually in normal mode. To help avoid that problem, you can specify that the cursor color and blink rate change when entering insert mode.

Using gvim with the defaults, the cursor shape is a block when in n-v-c modes (normal mode, or visual selection mode, or command mode while entering a colon command), and the shape changes to a vertical bar when in i (insert) mode. The color and blink rates do not change.

Here is an example for gvim showing how to customize the cursor properties (see :help 'guicursor'):

highlight Cursor guifg=white guibg=black
highlight iCursor guifg=white guibg=steelblue
set guicursor=n-v-c:block-Cursor
set guicursor+=i:ver100-iCursor
set guicursor+=n-v-c:blinkon0
set guicursor+=i:blinkwait10

Line 1 defines the color highlighting used for n-v-c modes (set in line 3), and line 2 defines a different color for insert mode (set in line 4). Line 5 disables blinking (blinkon value 0) for n-v-c modes, and line 6 increases the default blink rate for insert mode. Line 4 also sets the cursor shape to a 100% sized vertical bar for insert mode (the default is ver25, a 25% vertical bar. When using ver100 vim doesn't take the guifg parameter. It is better to use block instead).

It is possible to change the cursor color and style in the terminal if it understands the following escape sequences. Not all terminals support this, but xterm, rxvt and Terminator do. Recent versions of gnome-terminal support the sequence to change color, but not the one to restore the color to the default. Add the following to ~/.vimrc:

if &term =~ "xterm\\|rxvt"
  " use an orange cursor in insert mode
  let &t_SI = "\<Esc>]12;orange\x7"
  " use a red cursor otherwise
  let &t_EI = "\<Esc>]12;red\x7"
  silent !echo -ne "\033]12;red\007"
  " reset cursor when vim exits
  autocmd VimLeave * silent !echo -ne "\033]112\007"
  " use \003]12;gray\007 for gnome-terminal
endif

And changing the cursor shape.

if &term =~ '^xterm'
  " solid underscore
  let &t_SI .= "\<Esc>[4 q"
  " solid block
  let &t_EI .= "\<Esc>[2 q"
  " 1 or 0 -> blinking block
  " 3 -> blinking underscore
  " Recent versions of xterm (282 or above) also support
  " 5 -> blinking vertical bar
  " 6 -> solid vertical bar
endif

If Vim is running in a Gnome-terminal, the cursor shape can be changed as follows:

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 block"
au VimEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"

Don't do this! This is using a global setting for a local problem -- it changes the cursor shape in all open gnome-terminal windows, not just the one running vim.

The first two lines change the cursor to ibeam/block as it enters/leaves insert mode, the last 2 lines ensure the terminal cursor is independent of vim cursor setting.

Disabling cursor blinking

The 'guicursor' option can be used to set the cursor properties when working in a GUI (gvim). The following commands illustrate how to disable blinking for all modes (the "a" specifies all modes, and the 0 value for blinkon disables blinking):

" Disable all blinking:
:set guicursor+=a:blinkon0
" Remove previous setting:
:set guicursor-=a:blinkon0
" Restore default setting:
:set guicursor&

You can also enter a command to directly set guicursor to disable blinking for the n-v-c modes. Type the following:

:set guicursor=

then press the Tab key (see :help 'wildchar') to have the current value of 'guicursor' displayed in the command line. You should see something like this (the ... indicates text omitted from this example):

:set guicursor=n-v-c:block-Cursor/lCursor,...

After the first colon, add "blinkon0-" so it reads:

:set guicursor=n-v-c:blinkon0-block-Cursor/lCursor,...

Alternatively, you can use the :let command with the &guicursor variable to set the 'guicursor' option. The following command inserts the "blinkon0-" using a substitute (in the replacement, & represents the original text, that is, 'n-v-c:'):

let &guicursor = substitute(&guicursor, 'n-v-c:', '&blinkon0-', '')

See also

Comments

In KDE 4.7 Konsole terminal emulator this works for changing cursor color background:

" orange in insert mode, red in command mode
" if you want to use rgb color formatting:
"   konsoleprofile CustomCursorColor=#255255255
autocmd VimEnter * silent !konsoleprofile UseCustomCursorColor=1
let &t_SI = "\<Esc>]50;CustomCursorColor=orange;BlinkingCursorEnabled=1\x7"
let &t_EI = "\<Esc>]50;CustomCursorColor=red;BlinkingCursorEnabled=0\x7"
silent !konsoleprofile CustomCursorColor=red
autocmd VimLeave * silent !konsoleprofile CustomCursorColor=gray;BlinkingCursorEnabled=0

Additionally it's blinking in insert mode but not in command mode. It changes cursor color "back" to gray after leaving Vim.

If it's still not working, check if you set the TERM variable to "xterm-256color" instead of "xterm" in .bashrc for example. --May 5, 2013