Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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.

like image 865
lsiebert Avatar asked Jan 28 '14 20:01

lsiebert


2 Answers

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

like image 128
Ingo Karkat Avatar answered Oct 17 '22 20:10

Ingo Karkat


I'd recommend to look at two vim plugins:

  • http://www.vim.org/scripts/script.php?script_id=4212
  • https://github.com/jeffkreeftmeijer/vim-numbertoggle
like image 36
Zsolt Botykai Avatar answered Oct 17 '22 20:10

Zsolt Botykai