I like to use the vim global command to accomplish tasks like this. This applies to adding iteration to the beginning of a line or modify a symbol in the text. It looks more complicated than the other solutions, but is a pretty flexible pattern to use when you have it handy, and is easy to modify without a lot of thought.
First, pick your range (which lines you want to apply this). I usually use marks (e.g. ma
on the first line and mb
on the second, but you can also use line numbers or visual selection), then enter a modification of the following command (currently tweaked for your use case)
:let i=1|'a,'bg/^/s/^/\=i.". "/|let i=i+1
Deconstruction
:let i=1
This sets up variable i
with a start value. Usually lists start with 1, so I'm setting i to 1.
|
The bar starts a new command
'a,'b
This sets the range of the next command. I'm going from mark a
to mark b
, which would be set on the first line and last line of your list.
g/^/
This is the global command. It searches the file (or range) for a given regular expression, and will execute the rest of the command line on each of the lines that matched. I'm matching every line by searching for "beginning of line". If you had text like
Item some txt
other text
Item second item
whatever
Item third
and only want to put these labels in front of Item
and ignore the other lines, do g/Item/
or g/^Item/
instead (assuming the literal Item text)
s/^/\=i.". "/
This executes the regular expression to replace the beginning of the line with the value of i
concatenated with a .
. Generally you can do this to anything (replace the label Item
with the number, for example).
|let i=i+1
Even though the bar starts a new command, it sets up a second command to run within the global command, instead of after the global is complete. The result is we increment i
before the next line is processed by g. Here is another place of flexibility. The modification of i can be anything (increment by 2, call a function that generates the next element of the Fibonacci sequence, whatever).
cat
ornl
, which can do number lines, but aren't generally available on Windows (as OP indicates from their comment aboutawk
). The top two solutions are pure Vim. Lastly, all lines vs one para is just a matter of range selection. Clearly not a big issue.cat -n
) covers that case, but not for paragraphs, I suppose.