Making the active window in vim more obvious

22

6

My workspace is normally one very big xterm with vim, split into six or more buffers. It would be really helpful if it were more obvious which one was the active buffer. Right now I'm using the presence of line numbers for this:

augroup BgHighlight
    autocmd!
    autocmd WinEnter * set number
    autocmd WinLeave * set nonumber
augroup END

but this means that when I change buffers my code jumps left or right, which is annoying. Plus, I'd like to be able to see which is line 94 even in an inactive buffer. So is there any way of changing the colours of the line numbers in the active buffer?

DrHyde

Posted 2012-02-03T12:12:30.373

Reputation: 320

Answers

26

Following your template, you could vary different properties, such as colorcolumn:

augroup BgHighlight
    autocmd!
    autocmd WinEnter * set colorcolumn=80
    autocmd WinLeave * set colorcolumn=0
augroup END

This will color column 80 on your current window, while disabling it on the others. It's a bit less jarring than setting/unsetting line numbers.

An even less intrusive option, if you are used to highlighting the current line (set cul), is to do:

augroup BgHighlight
    autocmd!
    autocmd WinEnter * set cul
    autocmd WinLeave * set nocul
augroup END

It all comes down to your usage and what you're willing to put up with.

fgb

Posted 2012-02-03T12:12:30.373

Reputation: 383

11

The color of the statusline is the most usual way to know which window is active. It is defined in your colorscheme with these two lines:

hi StatusLine   ctermfg=15  guifg=#ffffff ctermbg=239 guibg=#4e4e4e cterm=bold gui=bold
hi StatusLineNC ctermfg=249 guifg=#b2b2b2 ctermbg=237 guibg=#3a3a3a cterm=none gui=none

The first is for the current window, the second is for the "non-current" window.

This is an excerpt from xoria256, you should adapt the colors to your colorscheme.

But you could try something like that (beware, the colors in the second line are completely random):

augroup NrHighlight
  autocmd!
  autocmd WinEnter * hi LineNr ctermfg=247 guifg=#9e9e9e ctermbg=233 guibg=#121212
  autocmd WinLeave * hi LineNr ctermfg=274 guifg=#e9e9e9 ctermbg=133 guibg=#212121
augroup END

romainl

Posted 2012-02-03T12:12:30.373

Reputation: 19 227

Using hi StatusLine... and hi StatusLineNC... works like a charm! – Luc M – 2019-09-05T14:36:15.750

I'd already tried changing the LineNr highlighting on WinEnter/WinLeave, but it appears that LineNr colouring is global. – DrHyde – 2012-02-03T14:58:02.943

Yes, and I had to wait your comment to remember that. So, no, if LineNr is global there is obviously no way to change it in a specific window. Don't you think the statusline is enough? – romainl – 2012-02-03T15:48:46.963

I wondered if there might be some other per-window version of LineNr. And no, I don't think the status line is enough. It helps, but I'd like more. One problem with using the status line is that having the active window indicator at the bottom of the window is the exact opposite of what we have just about everywhere else - a title bar at the top of a window that changes colour - so there's a moment's pause to think "oh yeah, vim does this the other way round". Whereas line numbers are always, in every application, in the same place. – DrHyde – 2012-02-03T22:22:07.477

I'd really like it if you could change window background color, but it sounds like, from the above, this isn't possible. – Jonathan Hartley – 2013-11-01T14:23:33.247

7

See also my plugin which dims inactive windows: https://github.com/blueyed/vim-diminactive

blueyed

Posted 2012-02-03T12:12:30.373

Reputation: 1 071

3

This is what I do:

augroup BgHighlight
    autocmd!
    autocmd WinEnter * set relativenumber
    autocmd WinLeave * set norelativenumber
augroup END

Relative numbers are just an aid for calculating the repeat amount of commands, there is no use for them in a window you are not editing.

saga

Posted 2012-02-03T12:12:30.373

Reputation: 143

2

I like to hide the cursorline for inactive windows

augroup CursorLineOnlyInActiveWindow
  autocmd!
  autocmd VimEnter,WinEnter,BufWinEnter * setlocal cursorline
  autocmd WinLeave * setlocal nocursorline
augroup END  

(thanks to https://codeyarns.com/2013/02/07/how-to-show-cursorline-only-in-active-window-of-vim/)

mickey megabyte

Posted 2012-02-03T12:12:30.373

Reputation: 21

1

You can add this to your ~/.vimrc, which will make the inactive windows' status bars have a dark gray background to differentiate from the active window's white status bar.

highlight StatusLineNC cterm=bold ctermfg=white ctermbg=darkgray

See this screenshot example. The middle window is active (white status bar). The left and right windows are inactive (dark gray status bar).

enter image description here

wisbucky

Posted 2012-02-03T12:12:30.373

Reputation: 1 522