Vim Tips Wiki
Advertisement
Tip 231 Printable Monobook Previous Next

created 2002 · complexity basic · author Salman Halim · version 6.0


In a complex project you may be working with many files (buffers) in Vim, or you may be working with many instances of Vim. In addition, you may be working on files in different directories.

It may be useful to automatically change the color scheme to clearly identify where you are (which file, or which Vim, or which directory) to avoid accidentally changing the wrong file.

Original tip 231

I frequently like to edit multiple files in the same Vim session. However, if I come into Vim from another window I frequently hit 'i' and start typing in whatever buffer is currently being used – this is often the wrong one (requires <Esc>, undo, go the other buffer and . to redo).

One way to work around this for me is to use a different color scheme depending on what file I'm working on:

au BufEnter * if (exists("b:colors_name")) | let b:current_colors=colors_name
 \| execute "colorscheme " . b:colors_name | endif

au BufLeave * if (exists("b:current_colors")) | execute "colorscheme " . b:current_colors | endif

If you define b:colors_name with a particular color scheme name, then the above autocommands will switch to that colorscheme when you enter that window and will return to the original color upon departure.

Inside ftplugin/java.vim, for example, I might have b:colors_name set to 'morning', causing all java files to have a distinguishing color scheme.

From tip 1378 (now removed)

When editing files in two different directories, for example when copying parts of code from one into the other, you may change the color scheme to quickly identify the directory of the current file:

:autocmd BufEnter * if match(@%,'/otherdir/')>=0 | colorscheme oceanblack | else | colorscheme inkpot | end

This does not work very well for split windows (it might be better to use BufWinEnter instead of BufEnter?).

See :help registers for information on @%.

From tip proposed for 200907 (now removed)

I find this useful as I use focus-follows-mouse with lots of gvim windows.

Put the following in your vimrc:

:autocmd FocusLost * :colorscheme desert
:autocmd FocusGained * :colorscheme default

This means the focused gvim has a white background and the others are dark, which makes it much easier to be sure I'm typing in the right window. Obviously you can change the color schemes to fit your personal preferences.

 TO DO 

  • Presumably should change "lots of gvim windows" to "lots of instances of gvim running" because "window" has a meaning in Vim.

Comments

I have roughly merged 1378 and a proposed tip from 200907 to 231 (231 is this tip and it currently holds the entire text from each of the three tips). The original titles were:

  • 231 Localized color schemes
  • 1378 Change colors when switching to other directory
  • 200907 Change gvim colorscheme when focus changes

I propose to keep all the titles as redirects, and to rename 231 to something more general, for example:

  • Change the color scheme to show where you are

Or perhaps the current title of 231 is ok. Any thoughts? JohnBeckett 07:50, September 22, 2009 (UTC)

Advertisement