How do I numbers starting from 1 to a range of lines (thereby creating an ordered list)? For example, the following
foo
bar
baz
would turn into
1 foo
2 bar
3 baz
I know to use the visual block to insert 0 to each line and then gvg<c-a> to get the desired result, but somehow I found that inelegant (though it is looking better and better as I write this). Is there a better method, presumably one that accomplishes this in one step? Perhaps something with :'<,'>s/^/?
PS I know from this answer that I could use :'<,'>s/^/\=(line('.')-line("'<")+1)/, but that would be even clunkier (which is why my solution doesn't seem so bad now).
g<C-A>in Visual mode was what I was going to suggest when I read the top of your question... So, yeah, I think that's pretty much the most straightforward way. – filbranden Dec 24 '21 at 04:29Inc()that increments a variable and returns its previous value, so you could use\=Inc()on the replacement side. I thought of the advantage of that approach (overg<C-A>in Visual mode) and realized it could be used to number non-contiguous lines, if used with:g. Then I looked at Ben's linked question, and noticed I answered with something to that same effect, but without the need of a helper, just using the| let a += 1be part of what:gexecutes... So, in effect, that answer is what I'd suggest here. – filbranden Dec 24 '21 at 06:28