I often find myself removing and adding XML sections in configuration files:
- tomcat's server.xml
- maven's settings.xml
and many others.
Is there a vim plugin/command to make this simple?
I often find myself removing and adding XML sections in configuration files:
and many others.
Is there a vim plugin/command to make this simple?
Best it'd be if you'd find a command that adds things in the beginning and end of the selection.
When I'm commenting python code, I'm doing this:
:2,4s/^/#/g
You can use a combination of matching XML tags, as can be seen in this question and Perl's search and replace.
For instance, given this snippet:
<TypeDef name="a">
<ArrayType high="14" low="0">
<UndefType type="node">
</UndefType>
</ArrayType>
</TypeDef>
Put the cursor on either the opening or closing TypeDef
and type the following sequence:
vat:s/^\(.*\)$/<--\1-->/
v
- puts you into visual mode
at
- selects the whole XML tag
:s/^\(.*\)$/<--\1-->/
- surrounds each line with '<--...-->'
, the comment delimiters for XML
Alternatively, you can just delete it like this:
dat
d
- delete according to the following movements
at
- as before
Vim doesn't have smart commenting for all file types by itself. You should get a script for your commenting needs.
I use the enhcomentify script which has been around and maintained for a long time
http://www.vim.org/scripts/script.php?script_id=23
It seems to do xml well and you get the advantage of the same key bindings for any filetype you are using.
There are others.. notably the NERD Commenter
use surround.vim for general tag matching, deleting, inserting, surrounding etc,
For commenting tags, it is easy to use vim text objects & and a simple macro
Example:
enter
vmap ,c <esc>a--><esc>'<i<!--<esc>'>$
somewhere suitable, then place your cursor at the capital "A" of "ArrayType" on line two of the following (borrowed from Nathan Fellmans example above)
<TypeDef name="a">
<ArrayType high="14" low="0">
<UndefType type="node">
</UndefType>
</ArrayType>
</TypeDef>
then hit
vat,c
and you will get:
<TypeDef name="a">
<!--<ArrayType high="14" low="0">
<UndefType type="node">
</UndefType>
</ArrayType>-->
</TypeDef>
with your cursor at the end of the comment
Hello!
I think that adapting this vim tip might be useful.
I propose adding:
" Wrap visual selection in an XML comment
vmap <Leader>c <Esc>:call CommentWrap()<CR>
function! CommentWrap()
normal `>
if &selection == 'exclusive'
exe "normal i-->"
else
exe "normal a-->"
endif
normal `<
exe "normal i<!--"
normal `<
endfunction
to your .vimrc
Then, with a visual selection active (V), hit \c (backslash then c) to wrap your block in <!-- -->
XML-style comments.
Alternatively, as suggested on the wiki you can put the code in ~/.vim/scripts/wrapwithcomment.vim and add to your .vimrc:
au Filetype html,xml source ~/.vim/scripts/wrapwithcomment.vim
to only load that functionality when working on a html or xml file.