Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comment out HTML/XML element in VIM?

Tags:

html

vim

editor

xml

What's the best shortcut or plugin to comment out HTML/XML elements?

And also need to uncomment.

like image 935
northtree Avatar asked Aug 03 '11 03:08

northtree


3 Answers

If you use emmet-vim, you can select the whole contents of the tag you would like to comment out by pressing v a t and then press Ctrl y /

like image 56
worrawut Avatar answered Oct 12 '22 10:10

worrawut


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 -->/
  1. v - puts you into visual mode
  2. at - selects the whole XML tag
  3. :s/^\(.*\)$/<!-- \1 -->/ - surrounds each line with '<!-- ... -->', the comment delimiters for XML

Alternatively, you can just delete it like this:

dat
  1. d - delete according to the following movements
  2. at - as before

To delete id use then use vat:s/-->// to delete comments

like image 38
Dzoki Avatar answered Oct 12 '22 09:10

Dzoki


I use the tComment plugin. You can find a detailed video tutorial here on how to install and use it.

The plugin is very useful as it allows you to toggle comments from both the command and input interface, and you can do so using both visual mode and motions (like gcw, or gc3w)

like image 24
Moses Avatar answered Oct 12 '22 09:10

Moses