8

I frequently use the vi command

:set number

I recently was trying to align some data that had a zero based index with the line numbers in vim, which has a 1 based index. Is there a way to have vi start numbering lines from 0, or more broadly from any other starting point?

Paul
  • 7,155
  • 8
  • 41
  • 40
  • 1
    It doesn't look like this is possible. See http://stackoverflow.com/questions/1176326/vim-zero-indexed-line-numbers-in-set-number – David Mar 12 '14 at 20:18
  • The Q&A you posted indeed indicates that this is not possible. Another Q&A that I missed is here: http://stackoverflow.com/questions/10213373/is-there-a-way-to-use-base-zero-numbering-in-vi However perhaps (hopefully) something has changed in 4+ years. One of the comments introduced me to the set relativenumber command. This ironically partially solves my problem when I keep the cursor over the first line of the file! – Paul Mar 13 '14 at 12:47

1 Answers1

0

@David's comment led me on a bit of a goose chase eventually finding this link: Add line numbers to source code in vim

For my purposes what I needed was not necessarily a persistent numbering, but just a temporary one. Quickly insert/remove line numbers and then resort to the standard numbering. For this case, I hadn't thought of simply inserting then deleting my own line numbering. The below works well. And the possibilities are endless for numbering (see for example https://stackoverflow.com/a/252774/2816571)

%!awk '{print NR,$0}'

For a native vi solution to adding numbers then (from https://stackoverflow.com/a/253041/2816571) the below will insert line numbers then the space character:

:%s/^/\=line('.').' '/

Then (thanks Lance) https://stackoverflow.com/a/4674574/2816571 when done with the numbering, do something like this, but you might need to tweak the search pattern depending if you put a space, comma separator

:%s/^[0-9]* //

If there is something I am missing inside vim that can do the persistent 0 based numbering, please let me know.

Community
  • 1
  • 1
Paul
  • 7,155
  • 8
  • 41
  • 40