lsiebert
lsiebert

Reputation: 667

How can I have Vim use absolute line numbers when I'm typing in the command line and relative otherwise?

I'd like a simple augroup that switches all buffers to absolute line numbers (for Ex commands) when I go into the command window

my current code is:

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

but this doesn't seem to work.

Upvotes: 6

Views: 340

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172570

:setglobal won't work, because it just sets the future default, but doesn't update the values in existing windows. You need to apply this for all current windows, normally with :windo, but changing windows is a bad idea when the special command-line window is involved. Therefore, we toggle the option(s) "at a distance" via setwinvar() and a 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 toggles between nu + nornu and nonu + rnu; adapt the logic if you want to keep nu on and just toggle rnu.

Upvotes: 4

Related Questions