Vim CursorLine color change in insert mode

2019-03-08 10:58发布

问题:

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

回答1:

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)



回答2:

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:

  • :help 'cursorline'
  • :help :autocmd
  • :help InsertEnter
  • :help :highlight

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).



回答3:

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

Visual

Insert

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.

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.