Question

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?

Was it helpful?

Solution

@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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top