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

vim

Solution

`: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`.

Problem

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.

Original source