How to change background color of current buffer or panel in vim?

Imagine what I am coding and I have different split panels. What parameters should be passed to vimrc to change the background color when switching from one buffer / panel to another?

I tried:

autocmd BufEnter * highlight Normal ctermbg=black autocmd BufLeave * highlight Normal ctermbg=white 

I would like to add that I am sure that I have 256 colors enabled

+4
source share
4 answers

Actually, there is a way to get this effect. See @ blueyed's answer to this related question: vim are dim inactive split panels . It provides a script below, and when it is placed in my .vimrc, it reduces the background of inactive windows. In fact, this makes their background the same color specified for the colorcolumn (a vertical line indicating the desired width of the text).

 " Dim inactive windows using 'colorcolumn' setting " This tends to slow down redrawing, but is very useful. " Based on https://groups.google.com/d/msg/vim_use/IJU-Vk-QLJE/xz4hjPjCRBUJ " XXX: this will only work with lines containing text (ie not '~') " from if exists('+colorcolumn') function! s:DimInactiveWindows() for i in range(1, tabpagewinnr(tabpagenr(), '$')) let l:range = "" if i != winnr() if &wrap " HACK: when wrapping lines is enabled, we use the maximum number " of columns getting highlighted. This might get calculated by " looking for the longest visible line and using a multiple of " winwidth(). let l:width=256 " max else let l:width=winwidth(i) endif let l:range = join(range(1, l:width), ',') endif call setwinvar(i, '&colorcolumn', l:range) endfor endfunction augroup DimInactiveWindows au! au WinEnter * call s:DimInactiveWindows() au WinEnter * set cursorline au WinLeave * set nocursorline augroup END endif 
+10
source

You can not. Groups :highlight are global; that is, when you have multiple windows :split s, all window backgrounds will be painted with the same Normal selection group.

The only difference between active and inactive windows is the (blinking) cursor and the status bar highlighted differently (i.e. StatusLine vs. StatusLineNC ). (You can add other differences, for example, only by turning on the 'cursorline' in the current buffer (see My CursorLineCurrentWindow plugin .))

One of Vim's design goals is to work equally well in a primitive, low-level console, as in the GVIM GUI. When you have only 16 colors, the difference in background color is likely to come across syntax highlighting. I think it is for this reason that Vim does not and will not have this functionality.

+4
source

Basically, what Kent said will work - surprisingly well. The only limitation is that you can only reduce the foreground color. Copy this into vimrc and call :call ToggleDimInactiveWin() .

 let opt_DimInactiveWin=0 hi Inactive ctermfg=235 fun! ToggleDimInactiveWin() if g:opt_DimInactiveWin autocmd! DimWindows windo syntax clear Inactive else windo syntax region Inactive start='^' end='$' syntax clear Inactive augroup DimWindows autocmd BufEnter * syntax clear Inactive autocmd BufLeave * syntax region Inactive start='^' end='$' augroup end en let g:opt_DimInactiveWin=!g:opt_DimInactiveWin endfun 

A few things I should have found by writing this:

(1) windo conveniently executes a command in all windows

(2) auproup defines auto-command groups that can be cleared using autocmd! group autocmd! group

+2
source

Personally, I use my status line to report this. I use autocmds WinEnter and WinLeave to go to the inactive status bar (grayed out) on exit and the active status line (bright colors) on input. The split panels you mention are windows in vim. This also works because :help statusline tells us that its parameter is global or local to the window, so you can use :setlocal statusline=... or let &l:statusline=... to apply only to the current window.

Your method will not work, because a) BufEnter and BufLeave are not necessarily the events you want, and b) the selection of groups is global, so changing the Normal definition changes it for each window.

+1
source

All Articles