English 中文(简体)
Vim - Deleting XML Comments
原标题:
  • 时间:2009-11-12 14:32:30
  •  标签:
  • xml
  • vim

How do I delete comments in XML?

If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread across many lines?

最佳回答

\_. instead of . allows matching on all characters including newlines. But that alone will cause the regex engine to go overboard since regexes are greedy by default. Use {-} instead of * for a non-greedy match all.

Since the g/ /d command only deletes a single line (even for a multiline match), it would be preferable to switch to s/ / /g (substitute global).

:%s/<!--\_.{-}-->//g

问题回答

You should use XSLT to transform the file to a new file. You use the identity transform with an additional rule to not copy over the comments.

<xsl:template match="node()|@*">
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:apply-templates/>
   </xsl:copy>
 </xsl:template>

<xsl:template match="comment()"/>

You can use "xsltproc" in the libxslt package on Linux to do this as a script which I imagine you can execute from vim.

G day,

Doesn t entering

da>

delete the comment when the cursor is placed somewhere within the comment block?

You have to have a vim with textobjects enabled at compile time. Entering:

:vers

will let you see if textobjects are enabled in your build.

There s a lot of these funky text selections. Have a look at the relevant page in the docs.

Edit: You might have to add

set matchpairs+=<:>

to your .vimrc for this to work.

HTH

cheers,

if its not too much of a hassle and you have gawk on your system. you can try this

$ more file
<!--
asdlfj
sdjf
;jsgsdgjasl -->
text i want
i want
....
<!-- junk junk -->
i want text
anthor text
end

$ gawk -vRS= -->   { gsub(/<!--.*/,"")}1  file


text i want
i want
....


i want text
anthor text
end

I believe this should work:

:g/<!--.*?-->/d

The question mark makes the asterisk "lazy" instead of "greedy", meaning it will match as little as possible to satisfy the expression. This prevents the expression from removing the middle part of this example:

We want to keep this<!-- This is a comment -->We also want to keep this<!-- Another comment -->

EDIT: Looks like the vim flavor of regex doesn t support *? lazy matching. My bad.





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签