Vim Tips Wiki
(Move categories to tip template)
No edit summary
 
(19 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1287
 
|id=1287
 
|previous=1286
 
|previous=1286
 
|next=1288
 
|next=1288
|created=July 18, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Yakov Lerner
 
|author=Yakov Lerner
|version=n/a
+
|version=7.0
 
|rating=39/21
 
|rating=39/21
 
|category1=
 
|category1=
Line 15: Line 14:
   
 
The color codes below are Green in normal mode, and Magenta in insert mode. If you like other colors, just change them.
 
The color codes below are Green in normal mode, and Magenta in insert mode. If you like other colors, just change them.
 
 
<pre>
 
<pre>
  +
" first, enable status line always
 
set laststatus=2
 
set laststatus=2
  +
if version &gt;= 700
 
  +
" now set it up to change the status line based on mode
 
if version >= 700
 
au InsertEnter * hi StatusLine term=reverse ctermbg=5 gui=undercurl guisp=Magenta
 
au InsertEnter * hi StatusLine term=reverse ctermbg=5 gui=undercurl guisp=Magenta
 
au InsertLeave * hi StatusLine term=reverse ctermfg=0 ctermbg=2 gui=bold,reverse
 
au InsertLeave * hi StatusLine term=reverse ctermfg=0 ctermbg=2 gui=bold,reverse
Line 24: Line 25:
 
</pre>
 
</pre>
   
  +
If you want a different color based on the ''kind'' of insert mode you are in (i.e. insert, replace, or virtual insert), then you can use the <code>v:insertmode</code> variable during the InsertEnter event execution to do something different in each mode. For example:
==Comments==
 
  +
<pre>
I can't see an advantage over {{help|'showmode'}}.
 
  +
function! InsertStatuslineColor(mode)
  +
if a:mode == 'i'
  +
hi statusline guibg=magenta
  +
elseif a:mode == 'r'
  +
hi statusline guibg=blue
  +
else
  +
hi statusline guibg=red
  +
endif
  +
endfunction
   
  +
au InsertEnter * call InsertStatuslineColor(v:insertmode)
----
 
  +
au InsertChange * call InsertStatuslineColor(v:insertmode)
See [[VimTip1279]].
 
  +
au InsertLeave * hi statusline guibg=green
   
  +
" default the statusline to green when entering Vim
----
 
  +
hi statusline guibg=green
I use both ':showmode' *and* this tip, the statusline color change, together.
 
  +
</pre>
   
  +
==References==
----
 
  +
*{{help|InsertEnter}}
I too can not see much benefit in chaning the colour of the statusline. Also this hint takes up another line for status.
 
  +
*{{help|InsertChange}}
  +
*{{help|InsertLeave}}
  +
*{{help|v:insertmode}}
  +
*{{help|:hi}}
  +
*{{help|hl-StatusLine}}
   
  +
==See also==
----
 
  +
*{{help|'showmode'}} for another way to indicate mode
Liked the idea, but not the extra line. What about changing the ModeMsg? This is better for me:
 
  +
*{{help|hl-ModeMsg}} if you want to change the color of the above
   
  +
==Related plugins==
  +
* There is the [https://github.com/molok/vim-smartusline SmartusLine] plugin that kind-of does what is explained in this page.
  +
* The {{script|id=3383|text=StatusLineHighlight}} plugin does not indicate the mode, but the buffer state (modified, readonly, unmodifiable, special non-file "scratch") / window (is preview window) by changing the highlighting of the window's status line.
  +
* [https://github.com/Lokaltog/vim-powerline Powerline] provides a display of the current mode along with much more information in the statusline area.
  +
 
==Comments==
  +
This works when I enter insert mode for the first time and changes the colours properly when i leave insert mode, however after that the colour of the status line stays the same regardless of what mode I am currently in.
  +
Could it be something to do with other Vim settings?
  +
:Which script are you using? I haven't tried either, but at a quick glance it looks like the second should work, but I'm not as certain about the first. The second sets the same property differently depending on mode, but the first does not have much overlap between the settings changed when switching modes. --[[User:Fritzophrenic|Fritzophrenic]] 15:18, January 28, 2010 (UTC)
  +
  +
You can also, rather than changing the statusline , highlight the current line (see [[Highlight current line]]
  +
), which might be more visible.--[[User:Bmx007]]
 
<pre>
 
<pre>
  +
hi CursorLine ctermbg=green cterm=none
au InsertLeave * hi ModeMsg ctermfg=black ctermbg=yellow gui=undercurl guisp=Magenta
 
au InsertEnter * hi ModeMsg ctermfg=white ctermbg=none
+
au InsertEnter * set cursorline
  +
au InsertLeave * set nocursorline
  +
</pre>
  +
  +
The method of changing the statusline color works well, except it does not change the statusline to green when you exit insert mode using ^C, (as insertleave is not generated). I was thinking that you could change things so ^C changed the statusline color to green then called the ^C functionality, but I don't see how to do that with imap.
  +
  +
  +
You can do the Ctrl-C remapping via inoremap, like so:
  +
<pre>
  +
inoremap <c-c> <c-o>:hi statusline guibg=green<cr><c-c>
 
</pre>
 
</pre>
   
 
----
 
----
  +
Thanks, incorporating all the above to provide a cut and paste solution:
  +
<pre>
  +
" Mode Indication -Prominent!
  +
function! InsertStatuslineColor(mode)
  +
if a:mode == 'i'
  +
hi statusline guibg=red
  +
set cursorcolumn
  +
elseif a:mode == 'r'
  +
hi statusline guibg=blue
  +
else
  +
hi statusline guibg= magenta
  +
endif
  +
endfunction
  +
  +
function! InsertLeaveActions()
  +
hi statusline guibg=green
  +
set nocursorcolumn
  +
endfunction
  +
  +
au InsertEnter * call InsertStatuslineColor(v:insertmode)
  +
au InsertLeave * call InsertLeaveActions()
  +
  +
" to handle exiting insert mode via a control-C
  +
inoremap <c-c> <c-o>:call InsertLeaveActions()<cr><c-c>
  +
  +
" default the statusline to green when entering Vim
  +
hi statusline guibg=green
  +
  +
" have a permanent statusline to color
  +
set laststatus=2
  +
</pre>
  +
----------

Latest revision as of 07:31, 16 January 2013

Tip 1287 Printable Monobook Previous Next

created 2006 · complexity basic · author Yakov Lerner · version 7.0


The following small piece changes the color of the statusline when you enter insert mode, and when you leave insert mode. There are no mapping keys or new commands to remember, it works totally automatically.

The color codes below are Green in normal mode, and Magenta in insert mode. If you like other colors, just change them.

" first, enable status line always
set laststatus=2

" now set it up to change the status line based on mode
if version >= 700
  au InsertEnter * hi StatusLine term=reverse ctermbg=5 gui=undercurl guisp=Magenta
  au InsertLeave * hi StatusLine term=reverse ctermfg=0 ctermbg=2 gui=bold,reverse
endif

If you want a different color based on the kind of insert mode you are in (i.e. insert, replace, or virtual insert), then you can use the v:insertmode variable during the InsertEnter event execution to do something different in each mode. For example:

function! InsertStatuslineColor(mode)
  if a:mode == 'i'
    hi statusline guibg=magenta
  elseif a:mode == 'r'
    hi statusline guibg=blue
  else
    hi statusline guibg=red
  endif
endfunction

au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertChange * call InsertStatuslineColor(v:insertmode)
au InsertLeave * hi statusline guibg=green

" default the statusline to green when entering Vim
hi statusline guibg=green

References[]

See also[]

Related plugins[]

  • There is the SmartusLine plugin that kind-of does what is explained in this page.
  • The StatusLineHighlight plugin does not indicate the mode, but the buffer state (modified, readonly, unmodifiable, special non-file "scratch") / window (is preview window) by changing the highlighting of the window's status line.
  • Powerline provides a display of the current mode along with much more information in the statusline area.

Comments[]

This works when I enter insert mode for the first time and changes the colours properly when i leave insert mode, however after that the colour of the status line stays the same regardless of what mode I am currently in. Could it be something to do with other Vim settings?

Which script are you using? I haven't tried either, but at a quick glance it looks like the second should work, but I'm not as certain about the first. The second sets the same property differently depending on mode, but the first does not have much overlap between the settings changed when switching modes. --Fritzophrenic 15:18, January 28, 2010 (UTC)

You can also, rather than changing the statusline , highlight the current line (see Highlight current line ), which might be more visible.--User:Bmx007

hi CursorLine ctermbg=green cterm=none
au InsertEnter * set cursorline
au InsertLeave * set nocursorline

The method of changing the statusline color works well, except it does not change the statusline to green when you exit insert mode using ^C, (as insertleave is not generated). I was thinking that you could change things so ^C changed the statusline color to green then called the ^C functionality, but I don't see how to do that with imap.


You can do the Ctrl-C remapping via inoremap, like so:

inoremap <c-c> <c-o>:hi statusline guibg=green<cr><c-c>

Thanks, incorporating all the above to provide a cut and paste solution:

" Mode Indication -Prominent!
function! InsertStatuslineColor(mode)
  if a:mode == 'i'
    hi statusline guibg=red
    set cursorcolumn
  elseif a:mode == 'r'
    hi statusline guibg=blue
  else
    hi statusline guibg= magenta
  endif
endfunction

function! InsertLeaveActions()
  hi statusline guibg=green
  set nocursorcolumn
endfunction

au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertLeave * call InsertLeaveActions()

" to handle exiting insert mode via a control-C
inoremap <c-c> <c-o>:call InsertLeaveActions()<cr><c-c>

" default the statusline to green when entering Vim
hi statusline guibg=green

" have a permanent statusline to color
set laststatus=2