27

There is good snippet for changing cursor color:

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

How should I alter this that instead of cursor, CursorLine would change color for example from dark blue to blue?

My complete config is https://bitbucket.org/JackLeo/home-configs/src/5b8faf340f87/.vimrc

General Grievance
  • 4,555
  • 31
  • 31
  • 45
JackLeo
  • 4,579
  • 9
  • 40
  • 66
  • 5
    I like the mapping of the arror keys with the comment "Use the damn hjkl keys". – Grammin Sep 30 '11 at 18:45
  • Do I litterally write \ here or is that a visualization of ^V'ESC'? When I copy paste the text above into my .vimrc it does nothing (and yes it claims to be an xterm ... it is a putty session on a Linux box) – Angel O'Sphere Nov 03 '15 at 14:24

5 Answers5

37

Have you look in into the 'highlight' command which is a easier way to control this.

For example, to change the CursorLine,

:hi CursorLine guifg=red guibg=blue

Reference: :help highlight

To make it switch between mode.

" Enable CursorLine
set cursorline

" Default Colors for CursorLine
highlight  CursorLine ctermbg=Yellow ctermfg=None

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine ctermbg=Green ctermfg=Red

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine ctermbg=Yellow ctermfg=None

I may be possible to mix termcap color with autocmd, but IMO, highlight is more easy to maintain in long term (and in case if use gVim occassionally)

Zarick Lau
  • 1,505
  • 12
  • 9
  • I do know that, I'm using that in the config file. The point is that I want that it would change depending on if i'm in insert mode. – JackLeo Oct 01 '11 at 09:01
  • 1
    updated the reply to address the 'switch color depends on mode' issue. – Zarick Lau Oct 01 '11 at 09:49
  • This was such a handy tip!! Thank you!! – Mike Caron Jul 03 '12 at 14:03
  • 1
    To clarify, for gVim and Neovim with `set termguicolors`, you will need to use `guifg` and `guibg`. For Vim in terminal and Neovim without true colors, you should use `ctermbg` and `ctermfg`. – joelostblom Feb 27 '17 at 16:22
  • I do not like background colors so I changed it to "autocmd InsertEnter * set cursorline" / "autocmd InsertLeave * set nocursorline". Now I get the underline only as indicator for insert or normal mode. – t3o Nov 27 '20 at 08:59
9

This is pretty straightforward, put the following in your .vimrc or custom colorscheme file.

set cursorline
autocmd InsertEnter * highlight CursorLine guifg=white guibg=blue ctermfg=white ctermbg=blue
autocmd InsertLeave * highlight CursorLine guifg=white guibg=darkblue ctermfg=white ctermbg=darkblue

For more information see:

N.B: You can use the same method to change the colour of the cursor without all of those if-statements and escape-sequences (and this will also work in GVim).

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    other useful examples `cterm=bold` and you can 'unset' anything with `NONE`, e.g. I have no highlighting when not in Insert mode, with `ctermfg=NONE ctermbg=NONE cterm=NONE` – artfulrobot Feb 11 '13 at 10:59
4

When using MacVim with 'Lokaltog/vim-powerline' you can setup your normal/visual/insert colors to match the powerline mode color. I find this extremely helpful to know what mode I'm in without reading the powerline, especially on a large screen.

Here is the code I am using, based on @Zarick-Lau's answer.

In my colors/molokai.vim file:

" Visual Mode Orange Background, Black Text
hi Visual          guifg=#000000 guibg=#FD971F

" Default Colors for CursorLine
highlight CursorLine guibg=#3E3D32
highlight Cursor guibg=#A6E22E;

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine guibg=#323D3E
autocmd InsertEnter * highlight  Cursor guibg=#00AAFF;

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine guibg=#3E3D32
autocmd InsertLeave * highlight  Cursor guibg=#A6E22E;

Here is an example using the molokai original color scheme.

Normal

normal mode (green

Visual

visual mode (orange)

Insert

insert mode (blue)

I also find it's helpful to set the OS up to visually select using the same color too. For example, I've changed my highlight color to Orange in OSX, and when I select text, it is now orange instead of blue, same as in VIM.

select orange highlight

Example

Here the orange highlight being used in the text-box as I'm writing this Stack Overflow entry. Now all text I select in my OS matches the VIM setup.

selecting text in the OS

f1lt3r
  • 2,176
  • 22
  • 26
2

I chose to switch CursorLine and Normal in insert mode. First get the values with :hi Normal and :hi CursorLine. Then adjust the following lines:

set cursorline
autocmd InsertEnter * highlight Normal ctermbg=7
autocmd InsertEnter * highlight CursorLine ctermbg=15
autocmd InsertLeave * highlight Normal ctermbg=15
autocmd InsertLeave * highlight CursorLine ctermbg=7

For solarized light, this looks like this. I like the "focus" effect.

normal modeinsert mode

iGEL
  • 16,540
  • 11
  • 60
  • 74
  • Thanks ! I guess it is the simple good looking everyone wants when landing on this page because it does not ruin syntax highlighting. Too bad it only works for light mode and the terminal. Could you expand your answer to include gvim and dark backrounds ? – cassepipe Jul 07 '22 at 12:41
1

NO COLOR in current line even if you enter or leave INSERT MODE


"set cursorline
set noshowmode

"Enable CursorLine
set nocursorline

"Default Colors for CursorLine
hi CursorLine cterm=NONE ctermbg=NONE ctermfg=NONE

"Change Color when entering Insert Mode
autocmd InsertEnter * set nocursorline

"Revert Color to default when leaving Insert Mode
autocmd InsertLeave * set nocursorline

fuadnafiz98
  • 450
  • 5
  • 15