Skip to content

Instantly share code, notes, and snippets.

@dlo
Created June 28, 2012 15:53
Show Gist options
  • Save dlo/3012145 to your computer and use it in GitHub Desktop.
Save dlo/3012145 to your computer and use it in GitHub Desktop.
Relative Line Numbers in Vim
set rnu
au BufEnter * :set rnu
au BufLeave * :set nu
au WinEnter * :set rnu
au WinLeave * :set nu
au InsertEnter * :set nu
au InsertLeave * :set rnu
au FocusLost * :set nu
au FocusGained * :set rnu
@rdlugosz
Copy link

I found that this wasn't setting relative numbers when I would load a file, so I added a couple of events based on another commenter's post:

set rnu
au BufEnter * :set rnu
au BufLeave * :set nu
au WinEnter * :set rnu
au WinLeave * :set nu
au InsertEnter * :set nu
au InsertLeave * :set rnu
au FocusLost * :set nu
au FocusGained * :set rnu

@dlo
Copy link
Author

dlo commented Jun 29, 2012

Updated!

@Hezion
Copy link

Hezion commented Feb 27, 2013

after applying this, you can't just set 'nonumber' in vim anymore (i.e to copy text into another application). you'll have to do the following:

set nu
set nonumber

@isuldor
Copy link

isuldor commented Feb 6, 2014

Vim 7.4 introduced a hybrid mode that shows both relative number and the line number you're currently on. Now that either mode is non-exclusive I think you'll need to disable the current mode to toggle.

@jviotti
Copy link

jviotti commented Mar 30, 2015

@brianbaligad is right. My current setup is:

set number
set relativenumber

augroup linenumbers
  autocmd!
  autocmd BufEnter *    :set relativenumber
  autocmd BufLeave *    :set number norelativenumber
  autocmd WinEnter *    :set relativenumber
  autocmd WinLeave *    :set number norelativenumber
  autocmd InsertEnter * :set number norelativenumber
  autocmd InsertLeave * :set relativenumber
  autocmd FocusLost *   :set number norelativenumber
  autocmd FocusGained * :set relativenumber
augroup END

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment