TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Better line numbers for Vim

65 pointsby talmirzaalmost 12 years ago

21 comments

justinvhalmost 12 years ago
I was at one point <i>very</i> 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 &quot;dual line numbering&quot;, and sent the suggestion to Bram Moolenaar.<p>Here&#x27;s what it looked like: <a href="http://i.imgur.com/NTwwB.jpg" rel="nofollow">http:&#x2F;&#x2F;i.imgur.com&#x2F;NTwwB.jpg</a><p>Here was the patch. I wrote it back in 2011: <a href="https://code.google.com/r/justinvh-vim/source/detail?r=c2479730f264091b697932586e085d7199b3173f" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;r&#x2F;justinvh-vim&#x2F;source&#x2F;detail?r=c2479...</a><p>Ultimately Bram Moolenaar sent me a reply:<p>&quot;&quot;&quot;<p>Justin,<p>I think the audience for this would be too small.<p>- Bram<p>&quot;&quot;&quot;<p>However, in 7.3.787 (Jan 2013) a patch was introduced which showed the current line number in :rnu instead of 0. That&#x27;s definitely in the direction that this patch was going in.<p>So, I didn&#x27;t get a patch in, but on the plus side, I did learn a bunch about vim&#x27;s source code.
评论 #6039532 未加载
评论 #6041301 未加载
评论 #6039565 未加载
lazylesteralmost 12 years ago
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<p>I&#x27;d like to suggest that using absolute line numbers shortens this considerably. For example<p>:43,48m63<p>removes lines 43 to 48 and inserts them after line 63 regardless of the current cursor position (no need to move cursor).<p>Not saying relative line numbers are bad, just, there&#x27;s a quicker way, at least for the demo use case.
评论 #6042326 未加载
评论 #6041627 未加载
johncoltranealmost 12 years ago
I&#x27;ll ask it again:<p>What is the rationale behind &quot;relative in normal mode and absolute in insert mode&quot;?<p>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.<p>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&#x27;d argue that the purpose of absolute numbers is more to provide information than to be used as a tool.<p>I&#x27;ve observed that using one or the other is mostly a matter of taste, though, and needs.<p>But the &quot;relative in normal mode and absolute in insert mode&quot; part sounds strange.<p>How are line numbers any useful in insert mode when there&#x27;s traditionally no editing or cursor movement beyond the same line or <i>maybe</i> a couple of lines above&#x2F;below?<p>I recently came across a slightly better take on the rnu vs nu &quot;problem&quot;,<p><a href="https://github.com/vim-scripts/RelOps" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vim-scripts&#x2F;RelOps</a><p>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&#x27;s useful (for defining a target for your operator) and nu elsewhere. Still you get to do 4321G, though.<p>Too bad the author doesn&#x27;t get as much coverage as this one.
spajusalmost 12 years ago
I&#x27;m living happily with this:<p>&quot;relative number switches - show numbers in insert mode, relative in normal<p>autocmd InsertEnter * :set number<p>autocmd InsertLeave * :set relativenumber
评论 #6040210 未加载
tjkoalmost 12 years ago
Nice job with your first vim script!<p>That said, VIM patch 7.3.787 provides a &#x27;better&#x27; solution by replacing the current line&#x27;s number with the absolute number (because seeing 0 on the current line provides nothing meaningful) IMHO:<p><pre><code> with &#x27;relativenumber&#x27; can&#x27;t see the absolute line number </code></pre> ftp:&#x2F;&#x2F;ftp.vim.org&#x2F;pub&#x2F;vim&#x2F;patches&#x2F;7.3&#x2F;7.3.787
philwebsteralmost 12 years ago
In case you&#x27;re wondering about the cool status bars for Vim&#x2F;tmux, it&#x27;s Powerline: <a href="https://github.com/Lokaltog/powerline" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Lokaltog&#x2F;powerline</a>
tsmalmost 12 years ago
If it&#x27;s useful to other people--and obviously it is--it was worth writing, so congratulations on shipping.<p>That said, put me in the category of crotchety old Vim users (even though I&#x27;m only 20) who really don&#x27;t have a use for this. Potential use cases:<p><i>Scrolling:</i> If it&#x27;s a few lines, I tap j a few times--or guesstimate a number prefix. It&#x27;s just scrolling--no precision needed. If it&#x27;s more than a few lines, I use C-d&#x2F;C-u. If I need to teleport to a line I know about, :&lt;absolute linenum&gt; is fine.<p><i>Yanking&#x2F;Deleting&#x2F;Changing&#x2F;Indenting:</i> I turn on visual line mode (V), and proceed as above. Or, depending on the language I&#x27;m in, I&#x27;ll do, for example, yi{ to yank everything in the current block. Or use () judiciously.<p>So...what&#x27;s the actual use case for this? Not trying to be critical--I just want to understand how I&#x27;d integrate it into my workflow.
评论 #6039670 未加载
评论 #6039886 未加载
评论 #6039708 未加载
yetanotherjoshalmost 12 years ago
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.
评论 #6039380 未加载
weitzjalmost 12 years ago
I got to know the numbers plugin from <a href="http://vim.spf13.com/" rel="nofollow">http:&#x2F;&#x2F;vim.spf13.com&#x2F;</a> 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.<p>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-&lt;P&gt; to switch to normal mode, so my hands can stay on the keyboard.
评论 #6041644 未加载
minaguibalmost 12 years ago
Plugin functionality aside, the use case in the video demo is IMO better addressed by not worrying about line numbers at all.<p>Position the cursor at the start of the function, then d} (delete 1 paragraph forward). Alternatively if you don&#x27;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.<p>Then } (move 1 paragraph forward)<p>Then p to paste
jlgrecoalmost 12 years ago
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.<p>TERM=screen-256color-s cleared it up for me using tmux&#x2F;urxvt (with 256 colors).<p>(Also, great plugin!)
fphilipealmost 12 years ago
Tim Pope&#x27;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).<p>[1]: <a href="https://github.com/tpope/vim-unimpaired" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tpope&#x2F;vim-unimpaired</a><p>[2]: <a href="http://vim-doc.heroku.com/view?https://raw.github.com/tpope/vim-unimpaired/master/doc/unimpaired.txt" rel="nofollow">http:&#x2F;&#x2F;vim-doc.heroku.com&#x2F;view?https:&#x2F;&#x2F;raw.github.com&#x2F;tpope&#x2F;...</a>
lopatinalmost 12 years ago
Same thing for Sublime Text: <a href="https://github.com/tmanderson/VintageLines" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tmanderson&#x2F;VintageLines</a>
edanmalmost 12 years ago
I used to use Relative Line Numbers, until I switched over to EasyMotion. (Original fork: <a href="https://github.com/Lokaltog/vim-easymotion" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Lokaltog&#x2F;vim-easymotion</a>. I use a different fork, but don&#x27;t have the URL for it ATM).<p>If you haven&#x27;t tried it, EasyMotion is far, far better and contains much more functionality (covers far more use cases).
ancardaalmost 12 years ago
I was going to complain this didn&#x27;t work then I realised my server (Latest Debian stable) only has Vim 7.2 and this requires 7.3. I&#x27;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.<p>Does anyone know why Debian seems to move so slowly? Should I change to a different distro?
评论 #6040160 未加载
muuckalmost 12 years ago
My problem with relative numbering is that it doesn&#x27;t seem to work correctly when a line is wrapped.<p>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.
评论 #6039621 未加载
评论 #6040279 未加载
barbsalmost 12 years ago
A nice simple solution. Thanks!<p>Here&#x27;s something a little more complicated, which may or may not be more useful:<p><a href="http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement/" rel="nofollow">http:&#x2F;&#x2F;jeffkreeftmeijer.com&#x2F;2012&#x2F;relative-line-numbers-in-vi...</a>
nXqdalmost 12 years ago
Honestly, if you already use powerline or familiar extension, you can look at the status bar for the absolute line. I find it&#x27;s no need for changing between modes.<p>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.
评论 #6041655 未加载
carlosdpalmost 12 years ago
This is actually awesome...a simple solution to a problem I only just realized I had.<p>Hats off to you, sir!
lowglowalmost 12 years ago
This is standard in Mathias Bynen&#x27;s dotfiles that can be found here: <a href="http://dotfiles.github.io/" rel="nofollow">http:&#x2F;&#x2F;dotfiles.github.io&#x2F;</a>
joeblaualmost 12 years ago
Great job on this. I&#x27;ve always had an issue with doing yy or dd from an arbitrary position in VIM. this tool is great-thanks.