On Thu, 21 Jul 2011, Mark Larwill wrote:
I often need to make a large list of XML quickly and was wondering if
there is a fast way to do this with VIM. For example suppose I have
the XML:
<user>user1</user>
<user>user2</user>
<user>user3</user>
But I want to quickly make 100 users. I know I can quickly paste 97
copies of "<user>user1</user>" and then go and manually change this
number to 4, 5, 6, ... etc. But is there anyway way to also automate
the increasing list of integers?
Personally, I use Dr. Chip's visincr plugin. As described in a post of
his when this was still on Yahoo! Groups¹:
http://mysite.verizon.net/astronaut/vim/index.html#VISINCR (cutting edge)
http://vim.sourceforge.net/scripts/script.php?script_id=670 (stable)
I found the usage slightly awkward at first, but am now used to it. For
your example, if you have this already:
<user>user1</user>
Navigate to that line, then:
yy " yank the line
99P " paste 99 more copies of it before the cursor
0f1 " navigate to the '1' (ymmv)
<c-v> " ctrl+v enters visual block mode
99j " navigate to the last '1'
: " enter command mode, '<,'> is pre-filled
I<cr> " run visincr's 'I' command (<cr> = hit <Enter>)
" to trim the extra spaces:
gv " go to previous visual selection
: " command mode, '<,'> prefilled
s/ //g<cr> " trim the extra spaces
For your particular case, you could also do (without any plugin):
yy
99P
:let start=line('.') | ,+99s/1/\=1+line('.')-start/
The last line relies on:
:help line()
:help [range]
:help sub-replace-\=
Which one you prefer probably depends on how frequently you do this,
what your formatting requirements are, etc.
--
Best,
Ben
¹: http://tech.groups.yahoo.com/group/vim/message/85923
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php