Just one of those days. I have a large page in XWiki format and I want to add a column to a lot of lines in a table, but not to all lines. I also want that column to contain a counter and I don’t want to keep typing the rest of the day myself.
Enter VIM and it’s scripting capabilities.
I guess you know about mapping keys. So I can say at the “:” prompt something like this:
map <F5> iHello<Esc>j
But now I want to add a counter variable and have it inserted. It is at first not so obvious but the result is this:
map <F5> 0i\|<C-r>=g:c<Enter><Esc>:let g:c=g:c+1<Enter>j
it maps F5 to the following sequence:
- 0 (zero) means beginning of line
- i means insert
- \| means | character
- <C-r>= means insert variable
- g:c means global variable c
- let g:c=g:c+1 means off course add 1 to global variable c
Guess what, it works!