Skip to content

Instantly share code, notes, and snippets.

@dlo
Created June 28, 2012 15:53
Show Gist options
  • Select an option

  • Save dlo/3012145 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

dlo commented Jun 29, 2012

Copy link
Copy Markdown
Author

Updated!

@Hezion

Hezion commented Feb 27, 2013

Copy link
Copy Markdown

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

isuldor commented Feb 6, 2014

Copy link
Copy Markdown

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

jviotti commented Mar 30, 2015

Copy link
Copy Markdown

@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