Blog 1

Random Talk on Random Thoughts

Looping Through Lines

| Comments |

Problem

I copied lines from an enumerated list in this blog and pasted it to Vim. The content in each list item was seen in the current Vim buffer, but not the numbers.

An illustration of the problem

A sample ordered list with 2015 items

  1. Item one
  2. Item two
  3. Item three

  1. Item 2015

What is seen in Vim after copy and paste

Item one
Item two
Item three
...
Item 2015

If one writes in Markdown and he/she copies a numbered list from elsewhere to a text editor, then it will be very inconvenient to manually add back the numbers. To exaggerate this inconvenience, I put “2015” above.

Goal

Insert the item number at the beginning.

1. Item one
2. Item two
3. Item three
...
2015. Item 2015

Solution

I read chapters 28–30 and 36 of Learn Vimscript the Hard Way by Steve Losh, and searched “vim loop through lines” on Google. After I saw while liner < line("$") in a forum thread, I typed the following editor commands in Vim.

A while-loop which runs through the lines
1
2
3
4
5
6
let l = 1
1
wh l <= line("$")
  exe "norm! I" . l . "\<esc>j"
  let l += 1
endwh

Why is exe "norm! used instead of norm? Since \<esc> can’t go with norm. Similarly, to include compound keys like <S-v> in exe norm!, insert a backslash \ before <.

Comments