I have a source document with the following text
Here is a bunch of text
...
Collect underpants
???
Profit!
...
More text
I would like to visually select the middle three lines and insert numbers in front of them:
Here is a bunch of text
...
1. Collect underpants
2. ???
3. Profit!
...
More text
All the solutions I found either put the numbers on their own new lines or prepended the actual line of the file.
How can I prepend a range of numbers to existing lines, starting with 1?
In Vim 8, the first number in a selection can be incremented by pressing Ctrl-A. If the selection covers several lines, the first number in the selection on each line is incremented. Alternatively, numbers in a selection covering several lines can be converted to a sequence by typing g Ctrl-A. For example, start with the following line:
The result is: With the cursor on the first 0 in the first line, start a blockwise select by pressing Ctrl-V (or Ctrl-Q if you use Ctrl-V for pasting). Move the cursor down to select the first column of zeros, then press g Ctrl-A. Using Vim 8, that will give: Vim 7 and earlier need a script or a macro to create a sequence.
In Python, you use enumerate (): fruit = [ 'banana', 'apple', 'pear', 'peach' ] for i, item in enumerate (fruit, 1): print (i, '. ' + item, sep= '', end = '') How enumerate () Works The enumerate (iterable, start) function will return a sequence of tuples.
See Python: Tips and Tricks for similar articles. You have a list of items and want to print it out as a numbered list using Python. In another programming language, you might create a variable called i or num and then loop through the list incrementing the variable value by 1 with each iteration.
It makes for a good macro.
qq
(or q<any letter>
)yf<space>
(y
ank f
ind
)j
P
0
0
(incrementing positions you at the end of the number)q
again@q
(or @<the letter you picked>
)<number>@@
(@@
replays the last macro)To summarize the fun way, this GIF image is i1. <Esc>0qqyf jP0^a0q10@q
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With