Hacker News new | past | comments | ask | show | jobs | submit login
Numbers.vim - better line numbers for vim (myusuf3.github.com)
93 points by mccarthyjm on June 28, 2012 | hide | past | favorite | 45 comments



Do you really need a plugin for this?

If you want relative numbers to appear when in normal mode, and absolute line numbers to appear otherwise (e.g., when the window has lost focus), just throw this in your .vimrc:

    set rnu
    au InsertEnter * :set nu
    au InsertLeave * :set rnu
    au FocusLost * :set nu
    au FocusGained * :set rnu
Here's a gist for posterity. Feel free to fork it, leave comments on what should be changed, etc. The above has worked for me on Mac OS X for years, but there's a chance I didn't set it up the "right way". Leave comments on this gist and I'll update as necessary:

https://gist.github.com/3012145


I just have this in my .vimrc which lets you toggle it whatever way you want in edit mode and it stays however you leave it when you toggle modes

    " use Ctrl+L to toggle the line number counting method
    function! g:ToggleNuMode()
    	    if(&rnu == 1)
        		set nu
	    else
		    set rnu
	    endif
    endfunc
    nnoremap <C-L> :call g:ToggleNuMode()<cr>


Quick note: This requires vim 7.3


Thank you for sharing!


One benefit of the plugin approach is that it will get forked and improved and other cool and useful things added to it. It's a central repository for all of that goodness, and keeping it up to date just requires pulling the repo (assuming you're using Pathogen or Vundle), instead of remembering to dig through vim.org/scripts periodically.

TLDR - the plugin approach gains the benefits of software evolution.


You don't need one. It's a cost/benefit thing - is it worth having one? Do you really need to use Vim or Emacs or Eclipse, etc? You don't need to.

I can imagine some reasons why it might be useful to have it as a plugin. Making a plugin means giving it a name and a URL (and if it's on vim.org, a place in a well-known directory), and that makes discovery and communication of its existence easier. The fact that it's useful to have a gist for it bears this out, I think. I can't think of why it's that bad a thing to have a plugin for it.


Also useful to hook BufLeave/BufEnter and WinLeave/WinEnter imo.


yeah, one because you forgot to surround it with an auto group. Plus its lines you don't have to write yourself.


As someone with a low-bandwidth connection and that I often work in cafés I hate it when the only description available is through video when there could easily have been text and screenshots.


Even on high bandwidth connections it's annoying. My read speed is much higher bandwidth than videos for things that can be explained in a paragraph or three.

Videos should probably be used when:

1. Your audience is non-technical.

2. A (moving) picture is worth a thousand words (eg, words can't do it justice, but a quick video can).

3. You're not bandwidth constrained (or firewalled).

4. You're not just video'ing text entry, or if you must, you ensure the user can zoom in or enlarge the video to see it (the embedded video can't be enlarged, I had to open it in Youtube to actually see it).


Or when you're behind a corporate firewall that prohibits youtube.


I know some people like line numbers, but personally, I just look at the status bar at the bottom of the buffer.

I find line numbers on the side clutter up my view too much. I always use 80 character terminal widths, to make it easier to stick to my PEP8 validation script we run.


That's fine for determining what line you're on, but visible line numbers make it much easier to jump to a particular line. I've only recently set number in my .vimrc and wish I'd have done it sooner.


Do you use (){}[] and others to move about? I rarely need to know the line number to move to on the screen.


That only really works well if the line you want is visible.

I've found searching to be much quicker, since I know class/method/variable names more than I know line numbers - we follow reasonably standard names for things in the projects we work on.


I do that by jumping to the particular line. :<linenum> or <linenum>G does that.


I think the point is that it's harder to see what line number you want to jump to if you don't have line numbering on. . . .


Right, the alternative is to just sort of guess how much you need to ctrl-u/d or j and k to get to a specific line. Also, I guess it depends on screen size. I usually have plenty so the line numbers' usefulness outweighs the extra space required, at least for me.


Yes, that's exactly my point


Yes, that's how we all jump to a particular line :) It's just easier to target a specific line when numbering is turned on, as hesitz said.


They also make it much easier to find the line number named in an exception backtrace. :)


I just tried turning off line numbers, and you're right, it's great. They really don't provide enough value to justify taking 4 columns on every vertical split.

I use a pretty large font size for my terminal, and the extra 8 columns finally allows for two 80 column vertical splits on my laptop. Thanks for the suggestion!


There's also https://github.com/jeffkreeftmeijer/vim-numbertoggle which seems to do the same thing. I've been using that for a few months but i'm still not sure it's actually an improvement. It gets in the way almost as much as being actually useful.


FWIW, the example you give would be more easily handled with the built in "dap" (delete a paragraph)


Yea, that's what I had in mind while watching the video too. I remember that example as delete around paragraph, to match delete inside paragraph (dip), which differ in how they handle whitespace at the end of the paragraph. Delete inside quotes, di', and delete around quotes, da', work in the same way too.


I'm still pondering the "how is this useful?" part... why are absolute numbers useful for you in insert mode?


My exact thought. I absolutely love 'relative line numbering' as it lets me use different VIM commands without having to do math. And as I can go to any line number by typing ':<line number>' [generally reported by debugger or other means], I don't really care about actual line numbers anymore.


In addition, if you have 'set ruler' set, the absolute line number of the current line is displayed at the bottom of the screen when in insert mode.


I still feel like I have to do math (albeit simple math) with relative line numbering. If I want to yank a block of code that's X lines long, I have to look at line X, and add one to the number next to it. E.g., when I want to grab 8 lines, the last line I want to grab has a 7 next to it.

I guess I think it'd be more helpful if the numbers were inclusive, and 1-indexed. I'm not sure how to configure that though.


Yes, I wish the numbering was 1-indexed. But by now, I have trained my brain to automatically add one. I would definitely prefer a fix where I don't have to do +1.


I've found it occasionally useful when pairing, i.e. "no no, I mean the call on line 23, not 28".


well when you moving around code you want to be in normal mode. so lets say you cursor is at the top of the function which would be 0 (with relative numbering) you could simply look down and see how many lines you need to delete. so for example lets say 5, then you could simply do 5dd or 5yy, without having to count all to see how many lines which can get annoying.

while in insert mode you would simply want to see what line you are one and for collaboration with others. In cases where you might want jump to a line like :15.

There is also a NumberToggle function if you ever need to switch on the go.


If you're operating on more than just a couple lines, then it's probably a bad sign to be doing math. It's usually better to navigate to the end of the selection and set a mark or select the text in visual mode.


Actually I'm more interested in seeing this guy's tmux config file.




Its these kind of things that I wish to see vanilla vim shipped with, without bothering the user much.

Yes you can toss some commands and code snippets at me and ask me to use them to get these features. But not having to type commands/snippets and have all goodies out of the box is what modern text editors like sublime text are all about.


If you don't like typing, vim is not the right editor for you!


There's a difference in productive typing and typing to set up your editor. I would compare this to the popularity in Ubuntu vs Arch, too.


Well, it's not like it's Eclipse. I've been carrying my .vimrc and other dotfiles around from machine to machine for a decade. Occasionally I make changes, but for the most part there's not a lot of setup with vim for me.


Anyone else just use visual mode for that kinna stuff?


What's with the funky status bar?


Thats his tmux Theme(outer) inner is powerline for Vim(https://github.com/Lokaltog/vim-powerline/), with a patched font.


That's what I was looking for. I love HN comments


Was wondering that too one for tmux and a matching one for Vim. The author has his dotfiles up on Github so I'm planning on checking those out today.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: