How can I use Vim for absolute line numbers when I type the command line and vice versa?

I need a simple augroup that will switch all buffers to absolute line numbers (for Ex commands) when I enter the command window

my current code is:

augroup cmdWin autocmd! autocmd CmdwinEnter * setglobal nornu autocmd CmdwinLeave * setglobal rnu augroup END 

but it does not work.

+6
source share
2 answers

:setglobal will not work because it just sets future defaults, but does not update the values ​​in existing windows. You need to apply this to all current windows, usually with :windo , but changing windows is a bad idea when a special command line window is involved. Therefore, we switch the option "at a distance" through setwinvar() and the loop:

 augroup cmdWin autocmd! autocmd CmdwinEnter * for i in range(1,winnr('$')) | call setwinvar(i, '&number', 1) | call setwinvar(i, '&relativenumber', 0) | endfor autocmd CmdwinLeave * for i in range(1,winnr('$')) | call setwinvar(i, '&number', 0) | call setwinvar(i, '&relativenumber', 1) | endfor augroup END 

This switches between nu + nornu and nonu + rnu ; adapt the logic if you want to enable nu and just switch rnu .

+4
source

All Articles