Search code examples
vimviewsplitbufferautocmd

Vim - calling buffer events when moving to other window with same buffer


if I have a file open in vim and use the command ":split", it splits the same buffer into two windows.

In my vimrc I have this commands:

autocmd BufEnter * set cursorline
autocmd BufEnter * set cursorcolumn
autocmd BufLeave * set nocursorline
autocmd BufLeave * set nocursorcolumn

Unfortunattely they are not executed when I move from one window to another window if the window holds the same buffer.

How can I execute those commands, when I move to another window, even if the buffer is the same?


Solution

  • Use WinEnter/ WinLeave instead. As the former doesn't fire on startup, you may need VimEnter as well.

    Note that there are also plugin solutions for showing 'cursorline' just in the current window, for example my CursorLineCurrentWindow plugin (the next version 2.00 will support 'cursorcolumn' and 'wincolor' as well) and cursorline_current.vim plugin.