Geeks like line numbers. I think Vim geeks really like line numbers, because we use them for more than just finding a specific place in the code.
Jumping to a line number
Out-of-the-box you’ve got three choices. Just pick the one you like best and don’t worry about remembering the others. I’ve used the 1st one for years and never had any need for the others.
Minimal typing
Type the number of the line you want to go to followed by capital G. E.g. to
jump to line 14 you’d type 14G
If a cat walks over your keyboard while
attempting to enter a number, just hit escape and start over.
The G
is for Goto
.
Maximal visibility
If you prefer to see the number as you type it then hit colon, the number, and
then enter. E.g. :14
Other
I don’t know why this one exists, but like the first one, you can type the
number, but instead of following it with G
you can type gg
So, 14gg
The help text explanation of gg
is exactly the same as G
.
Displaying Line Numbers
Turn them on
set number
Turn them off
set nonumber
Easy. Most geeks add set number
to their vimrc because who
wouldn’t want that on by default?
Relative line numbers
In Cut, Copy, Paste we show the value of counting relative
numbers. E.g. “I want to copy this line and 2 below it.” -> y2<RETURN>
but
it gets annoying when you have to either do math (subtract the current
line number from the destination line number) or count like a kindergarten
student: “one, two, three, four, five…”
Wouldn’t be easier if you could just find the line you wanted and have something tell you how far away it was? Well, that’s relative line numbering. The commands to enable and disable this are simple.
Looking at the screenshot, it’s easy to tell, without math or counting, that
I’m on line 37 and that if I want to delete everything down to, and including
the “then” then I’ll need to delete 4 additional lines. d4<RETURN>
(see
Cut, Copy, & Paste).
Technically this is the “Hybrid line numbers”. Pure relative line numbers would have a zero at the current line. Hybrid shows you the current absolute line number instead.
set relativenumber " turns on relative line numbers
set number " adds in the absolute line number on cursor line
then
set norelativenumber
If you’re displaying hybrid line numbers then turning off relative numbers will leave you displaying just absolute numbers. If you were only displaying relative numbers then it’ll leave you with no line numbers at all.
Typing all that just to toggle line numbers is, sadly, more work than just
counting. So, let’s fix that. Adding the following function to your
vimrc
will give you a new key combo. ^l
(Control + l). l
is for line numbers.
Hitting ^l
will toggle your line numbering from normal, to relative, and
back.
"------
" use Ctrl+L to toggle the line number counting method
" either normal, or relative to the current line
" that way you can easily see the number to type after y or d
" without having to actually count
function! g:ToggleNuMode()
if(&rnu == 1)
set norelativenumber
set number
else
set relativenumber
endif
endfunc
nnoremap <C-L> :call g:ToggleNuMode()<cr>
Relative Cursor position
Not strictly about line numbers, but this seemed as good a place as any to describe this.
By default vim likes to give you a little context around the current line. Try moving your cursor to the top or bottom of the screen. You can’t do it until you reach the top or bottom of the file.
Well, the number of buffer lines it gives you is controlled by the so
variable. Set it to 1 set so=1
and you’ll be able to get within 1 line of the
top or bottom. Set it to 0 (set so=0
) and you’ll get right up to it. Set it to
any number bigger than 1⁄2 your total lines on your screen (set so=999
) and
Vim can’t actually keep your cursor that far away from the top and bottom of
the window (Damn you math!). So, vim gives up and just keeps your cursor in the
center. Some people find this to be really nice when they’ve got their screen
showing relative line numbers.
Notable plugins
numbertoggle allows you to quickly toggle between relative and absolute line numbers and cleverly switches when when focus is lost or gained or when switching between normal and insert mode.
The idea is that when you’re in Normal mode you’re navigating and doing other high-level operations. When you’re in Insert mode you’re doing more fine-grained things where relative numbers are more useful.
Personally, I don’t like the idea of switching automatically, and it chooses
<C-n>
(control n) for toggling line numbers which sucks if you use a unix
machine where control n is frequently what’s used for invoking a “new file”.
I’ll stick with the simple function above.
RltvNmbr
The RltvNmbr plugin abuses the
“signs” feature to display
relative numbers in the gutter beside the absolute
line numbers that Vim is calculating. The constant recalculating can slow things
down on large files and I suspect you will have problems if you try using this
at the same time as another plugin that’s trying to use the “signs” gutter.