Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Better line numbers for Vim (myusuf3.github.io)
65 points by talmirza on July 13, 2013 | hide | past | favorite | 45 comments


I was at one point very interested in the same idea. I wanted relative line numbers and absolute line numbers integrated together. So, I created a patch, called it `dnu` for "dual line numbering", and sent the suggestion to Bram Moolenaar.

Here's what it looked like: http://i.imgur.com/NTwwB.jpg

Here was the patch. I wrote it back in 2011: https://code.google.com/r/justinvh-vim/source/detail?r=c2479...

Ultimately Bram Moolenaar sent me a reply:

"""

Justin,

I think the audience for this would be too small.

- Bram

"""

However, in 7.3.787 (Jan 2013) a patch was introduced which showed the current line number in :rnu instead of 0. That's definitely in the direction that this patch was going in.

So, I didn't get a patch in, but on the plus side, I did learn a bunch about vim's source code.


This looks quite useful to me.

Feature request: allow the user to select separate colors for the relative and absolute line numbers, and for the slash. So three separate colors in total. That would make it easier to read.

Another idea is to have relative line numbers on the left side of the screen, and absolute line numbers on the right (or vice-versa).


I would love to take feature requests, but it's honestly not maintained. Don't get me wrong, it wouldn't be hard to reintegrate it with the current release, but I believe the idea should be at least approximated with vimscripts rather than making someone build a custom vim release to support this feature alone. Plus, now :rnu sort-of mimics this functionality by showing the current line number on the 0th row, instead of '0'.

On a side note, to your second request of left-side/right-side splits for line numbers: my first experimentation was to do just that. Left side was rnu and right side was nu, but the way vim's buffers work out, it wasn't very practical. It also caused headaches for rtl text and so on if I remember correctly.


I just wrote to the mailing list about that patch... it slows vim down so much, it makes the feature entirely unusable :( I wish the old way was back.


In my vim doesn't show the 0 instead it shows the line number you're currently on. Seems somewhat what you suggested.


Indeed it does! However, that didn't come around until vim 7.3.787 was introduced. That was on 30-Jan-2013. I wrote this patch back in 2011.


In the video, the author moves a block of code by: 1. going to the start of the code block 2. changing mode to visual 3. moving to the end of the block 4. deleting the block 5. moving to the insertion point 6. inserting the delete buffer

I'd like to suggest that using absolute line numbers shortens this considerably. For example

:43,48m63

removes lines 43 to 48 and inserts them after line 63 regardless of the current cursor position (no need to move cursor).

Not saying relative line numbers are bad, just, there's a quicker way, at least for the demo use case.


Actually, he was in normal mode and issued

    5dd
at the start of the block.


You are technically correct, but coming up with those magic numbers(43, 48, 63) requires the user to manually try and find them, is more error-prone, and requires a whole lot more keypresses for larger blocks that don't fit on one screen.

I would argue that the author's way is the more idiomatic usage of vim.


idiomatic shmidiomatic, give me faster always!


I'll ask it again:

What is the rationale behind "relative in normal mode and absolute in insert mode"?

relativenumber is useful for quick navigation with 6j or 10k or defining a target for operators with c6j or d10k. Relative numbers are a tool for better text editing.

number is slightly less useful for navigation when in a large buffer: 6j or v6j are obviously a little better than 4321G or v4321. But I'd argue that the purpose of absolute numbers is more to provide information than to be used as a tool.

I've observed that using one or the other is mostly a matter of taste, though, and needs.

But the "relative in normal mode and absolute in insert mode" part sounds strange.

How are line numbers any useful in insert mode when there's traditionally no editing or cursor movement beyond the same line or maybe a couple of lines above/below?

I recently came across a slightly better take on the rnu vs nu "problem",

https://github.com/vim-scripts/RelOps

that switches to relativenumber when you hit an operator like c or y and puts you back to number when you are done. This sounds a lot more sensible and less arbitrary to me: you get rnu when it's useful (for defining a target for your operator) and nu elsewhere. Still you get to do 4321G, though.

Too bad the author doesn't get as much coverage as this one.


In case you're wondering about the cool status bars for Vim/tmux, it's Powerline: https://github.com/Lokaltog/powerline


Nice job with your first vim script!

That said, VIM patch 7.3.787 provides a 'better' solution by replacing the current line's number with the absolute number (because seeing 0 on the current line provides nothing meaningful) IMHO:

  with 'relativenumber' can't see the absolute line number
ftp://ftp.vim.org/pub/vim/patches/7.3/7.3.787


I'm living happily with this:

"relative number switches - show numbers in insert mode, relative in normal

autocmd InsertEnter * :set number

autocmd InsertLeave * :set relativenumber


I have two more events since I don't need relative numbers in a buffer that I'm not currently working in:

  set relativenumber
  autocmd InsertEnter,WinLeave * :set norelativenumber
  autocmd InsertLeave,WinEnter * :set relativenumber
(Also, :set number does not reset to non-relative numbers anymore for me)


If it's useful to other people--and obviously it is--it was worth writing, so congratulations on shipping.

That said, put me in the category of crotchety old Vim users (even though I'm only 20) who really don't have a use for this. Potential use cases:

Scrolling: If it's a few lines, I tap j a few times--or guesstimate a number prefix. It's just scrolling--no precision needed. If it's more than a few lines, I use C-d/C-u. If I need to teleport to a line I know about, :<absolute linenum> is fine.

Yanking/Deleting/Changing/Indenting: I turn on visual line mode (V), and proceed as above. Or, depending on the language I'm in, I'll do, for example, yi{ to yank everything in the current block. Or use () judiciously.

So...what's the actual use case for this? Not trying to be critical--I just want to understand how I'd integrate it into my workflow.


Are you asking the use case for relative line numbers or for the plugin? For relative line numbering, the benefit I see is the ability to move with precision to specific lines without (a) switching to line-wise selection mode during visual selection, or (b) losing your horizontal cursor position, both of which are required for using the :<line number> jump. As for the plugin, it just makes switching between relative and absolute numbers conveniently automatic.


Absolute line numbers are really annoying to use in huge files with thousands of lines (because it takes a noticeable amount of time just to type the number).


My workaround for this was practicing typing symbols and numbers using a typing program (back when I was a young teen). Now I can type a number like 42385 very nearly as fast as a word like hello.


I agree with your points, and would like to add that with EasyMotion it has even less use.


This is kinda life-changing. You have now made the use of numeric count prefixes to line-wise operations actually useful. 20dd for example. Delete groups of lines quickly and with precision, without guessing how many lines are involved.


I didn't even realize relative numbering was a built in feature of vim - learned by reading the plugin source. Vim is the gift that keeps on giving.


I went to relative numbering a while ago and I can't go back. It's awesome.

The only problem is that it's new in vim 7.3, so there are times where I'm forced to use an older version of vim and can't use the feature. I'll look up 10 lines, see "583" and automatically hit 583k without thinking. Not a big deal, and of course I can just do 583G to fix my mistake, but it's definitely some combination of funny and irritating.


This feature is exposing that my neural pathways for typing numerals above 4 are weak. Jumping 13 lines is so much easier than jumping 7... I have to actually look at the keyboard to type 7k! This will require some training.


I got to know the numbers plugin from http://vim.spf13.com/ This distribution gets you a really good head start on vim and lets you install additional plugins easily with its VBundle Integration and its customization options.

And in case if you ever wondered, why the ESC key is used to change to normal mode, you should read up on the history of the escape key. Therefore I use CTRL-<P> to switch to normal mode, so my hands can stay on the keyboard.


No. SPF13 and the other so called "distributions" are the worst things a new vimmer could install. Starting with vanilla Vim is the only sane option if you are serious about it: learn the basics, get used to them and, if you don't like them, create your own mappings and/or look for plugins.

Be a Vim user, not an SPF13 user.

Also, you don't need to remap <Esc> to <C-p> as there's already <C-[> or (the slightly different) <C-c>.

And it's Vundle, not VBundle.


Plugin functionality aside, the use case in the video demo is IMO better addressed by not worrying about line numbers at all.

Position the cursor at the start of the function, then d} (delete 1 paragraph forward). Alternatively if you don't have a clean paragraph boundary, use V to start a visual highlight, then move the cursor (up,down,},10down,whatever) to the end, then d to delete.

Then } (move 1 paragraph forward)

Then p to paste


If the author of the video is reading this: The problem you have with the background color in vim in the beginning of the video can probably be cleared up by making sure your TERM is set correctly.

TERM=screen-256color-s cleared it up for me using tmux/urxvt (with 256 colors).

(Also, great plugin!)


Tim Pope's unimpaired plugin [1], apart from other very useful key mappings [2], also has a mapping to toggle between relative (cor) and absolute numbering (con).

[1]: https://github.com/tpope/vim-unimpaired

[2]: http://vim-doc.heroku.com/view?https://raw.github.com/tpope/...


Same thing for Sublime Text: https://github.com/tmanderson/VintageLines


I used to use Relative Line Numbers, until I switched over to EasyMotion. (Original fork: https://github.com/Lokaltog/vim-easymotion. I use a different fork, but don't have the URL for it ATM).

If you haven't tried it, EasyMotion is far, far better and contains much more functionality (covers far more use cases).


I was going to complain this didn't work then I realised my server (Latest Debian stable) only has Vim 7.2 and this requires 7.3. I'm not surprised to find my Mac, which I bought last year, has 7.3 but my server, which I installed last month, has older software.

Does anyone know why Debian seems to move so slowly? Should I change to a different distro?


Debian does move slowly (that's why it is good for servers; you don't have to update often) but you probably need to update your server. On Debian Wheezy, I have got Vim 7.3.547.


>Debian Wheezy

I believe due to a mistake it ended up on Debian 6 (Squeeze). Still, shouldn't 7.3 be available via apt-get?


Nope, this is what Packages.bz2 shows for Debian 6:

  Package: vim
  ...
  Version: 2:7.2.445+hg~cb94c42c0e1a-1
  ...
  Filename: pool/main/v/vim/vim_7.2.445+hg~cb94c42c0e1a-1_i386.deb
And the file is still available here: http://debian.ipacct.com/debian/pool/main/v/vim/vim_7.2.445+...

Edit: and no, it wouldn't be available to Debian Squeeze users, since it is served as an update to Wheezy users and not as a separate package (even if it was, it would have been made available only to Wheezy).


My problem with relative numbering is that it doesn't seem to work correctly when a line is wrapped.

Say I want to move 6 lines down. I use 6j, but one of the lines I want to jump over is wrapped and occupies 2 lines in my editor. That will mean my cursor will end up at the line that was previous numbered 5. This behavior drives me nuts.


That shouldn't happen unless you've mapped "j" to "gj" (which is fairly common for people who use line wrapping): http://vimdoc.sf.net/htmldoc/motion.html#gj


If you?ve remapped `j` and `k` to move visually, you can still move linewise using `<CR>` (Enter) and `-`. They are equivalent to `j^` and `k^`.


That's right, I have remapped j and k. I had it for so long that I forgot it is not the default behavior.


Honestly, if you already use powerline or familiar extension, you can look at the status bar for the absolute line. I find it's no need for changing between modes.

And if you use Mac OSX and latest vim version installed by homebrew, in relative line number mode of vim, instead of 0 it is the row line number.


You know you don't need Powerline to display information like line number in your statusline, right?


A nice simple solution. Thanks!

Here's something a little more complicated, which may or may not be more useful:

http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vi...


This is actually awesome...a simple solution to a problem I only just realized I had.

Hats off to you, sir!


This is standard in Mathias Bynen's dotfiles that can be found here: http://dotfiles.github.io/


Great job on this. I've always had an issue with doing yy or dd from an arbitrary position in VIM. this tool is great-thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: