I wanted to visually mark a few lines and then surround the whole range with <p> and </p>. After a lot of fiddling I have come up with this substitute command that seams to work:
:'<,'>s/^\(\_.*\)\%V/<p>\1<\/p>/
Is there a better way of doing this or could someone explain why it works?
\_. matches every characters including end-of-line. The ^ (start-of-line) and the \%V (match visual range) seams to behave strange. For example the documentation suggest that you use two \%V to surround your expression but that does not seams to be necessary. Using no \%V or having only one at the start matches the whole buffer. Removing the ^ causes the last line to be matched and substituted separately. A $ at the end seams to be unnecessary also.
1. Use surround vim
You can use surround.vim, in visual mode:
s<pEnterE.g.
vat(visual select ‘around’ tag),s<psurround with<p>...</p>Breakdown:
p2. Use ex commands with the range markers
Edit: without surround you would be able to either
3. Use yank and XML filetype plugin inserting register contents:
Or much simpler:
Note that at 1 my XML filtetype plugin (I think it is default) automatically provided the closing tag (
</p>) so we can just insert the yanked contents using C-r" — without even leaving insert mode!