tags:

views:

578

answers:

5

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?

+1  A: 

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
Cheery
Thanks for the answer, it does point me in the right direction. But how would I go about appending '<!--' to the first row and prepeding '-->' to the last one?
Robert Munteanu
vim macros make this easy, by either using the '< and '> marks that go with visual selections, or moving the cursor from one end of the visual selection to the other with "o"
dysmsyd
+2  A: 

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

Nathan Fellman
Thanks for the answer, it looks good. Unfortunately XML is not escaped with '#'. How would I go about appending '<!--' to the first row and prepeding '-->' to the last one?
Robert Munteanu
Your update is very insightful.I can then use vat:s/-->// to delete comments.Thank you.
Robert Munteanu
+3  A: 

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

http://www.vim.org/scripts/script_search_results.php?keywords=comment&amp;script_type=&amp;order_by=rating&amp;direction=descending&amp;search=search

michael
+2  A: 

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

dysmsyd
+2  A: 

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.

NicDumZ