Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting out XML in vim

Tags:

vim

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?

like image 755
Robert Munteanu Avatar asked Jun 01 '09 08:06

Robert Munteanu


People also ask

How do I comment out XML code?

The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags.

How do you comment out multiple lines in XML?

Select the entire block of xml and hit CTRL+/ to comment the entire block. The comment that was present previously now shows up as part of the file and not as a comment anymore.

How do I comment an entire file in Vim?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

How do I comment a few lines in Vim?

vim-multiline-comment.md For commenting a block of text is almost the same: First, go to the first line you want to comment, press Ctrl``V , and select until the last line. Second, press Shift``I``#``Esc (then give it a second), and it will insert a # character on all selected lines.


4 Answers

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

like image 192
Nathan Fellman Avatar answered Oct 30 '22 14:10

Nathan Fellman


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

like image 40
sjh Avatar answered Oct 30 '22 12:10

sjh


I love the simplicity of https://github.com/tpope/vim-commentary. Easy to add other languages, though seems to support most out of the box. Under 100 lines of code.

gcc to comment the current line, or select the text you want to comment and hit gc. Super easy.

like image 27
River Satya Avatar answered Oct 30 '22 13:10

River Satya


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&script_type=&order_by=rating&direction=descending&search=search

like image 43
michael Avatar answered Oct 30 '22 12:10

michael