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?
The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags.
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.
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.
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.
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 modeat
- 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 movementsat
- as before
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With