VimTrick: Relative line numbering
Edit faster by eliminating the need to do arithmetic in your head.
Many operations in Vim can be executed over a given number of lines. For example, d3j
will delete the current line, plus the 3 below it. Having line numbers displayed will make it easier to know how many lines you want to edit. You can enable and disable line numbering easily in Vim:
:set number
- Shows line numbers:set nonumber
- Hides line numbers
You may find yourself often reading the line numbers and then subtracting two numbers in your head to decide over how many lines to execute a command. But there is a better way!
Vim allows showing relative line numbers instead of absolute line numbers:
:set relativenumber
- Shows relative line numbers:set norelativenumber
- Hides relative line numbers
The numbers are now relative to the current cursor position. If you’re on Vim 7.4 or newer, and you have both absolute line numbering enabled and relative numbering, then you’ll get hybrid line numbering. That is, the number on your current line will be its absolute line number, instead of 0.
With relativenumber
enabled, one can simply glance at the number above or below the current line, rather than do mental math. If you find yourself editing chunks of lines, hybrid line numbering can be a real time (and brain) saver.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
I used to train myself to embrace relative line-numbers. But in the end I just stuck with visual selecting the portion I need for an operation. I know some vimmers see this as inefficient, but I like it for the following reasons:
1.) I can always visually predict what will happen, e.g. literally see the visually selected block before I yank it, or before I cut it. With `d3j` or `y6k`, I'll always have to make sure my operation was correct afterwards.
2.) There are several other motions we can make use of together with visual select, such as `vip` for selecting a paragraph, making sure vim correctly did so visually, then yank or indent or cut. On top of that, we can use `H`, `M`, and `L` to roughly get to where we want to be on the screen -- this prevents us from having to hold down j or k for 5 seconds.