2011-06-07 160 views
58

,我有以下的,我在Vim打開的文件的代碼:如何刪除HTML標籤,而不是內容的Vim

<p>Hello stackoverflow!</p> 

如何刪除<p></p>標籤,但保留的內容他們?也就是說,我應該怎麼按,以結束與:

Hello stackoverflow! 

我知道按d我噸會做相反。

我正在使用Janus

回答

69

隨着surround.vim插件安裝,按dst to d elete s urrounding t ag。

類似的快捷方式:

  • d小號 - 刪除周圍括號()
  • d小號 - 刪除周圍的雙引號""
  • d小號 - 刪除周圍的單引號''

等等...

+0

這是我的解決方案。謝謝。 – retro 2011-06-07 11:13:43

+1

這應該是最有價值的答案,因爲每個人都應該使用TPopes真棒插件這些天! :) – walialu 2014-11-14 07:50:05

+0

我知道'di ...'和'da ...',但我剛剛瞭解到'ds ...'。非常有用! – 2017-06-20 16:51:01

3

地圖這對您選擇的關鍵:從http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

+0

謝謝你,我看到了這一點。但我想像dit一樣映射它。例如點。 – retro 2011-06-07 11:09:05

+1

@retro將它映射到「dot」通常不是一個好主意,因爲這會影響其他非常方便的vim命令,即「單重複」 – 2011-06-07 11:14:11

+0

是的,我現在明白了。 – retro 2011-06-07 11:15:44

76

兩者

vat<Esc>da>`<da> 

一個簡單的解決方法是(用光標在標籤內的任何地方):

yitvatp 

什麼這是的:

  • y - 猛拉
  • it - 標籤
  • vat的內側 - 選擇整個標籤
  • p - 糊劑在它前面抽出的文本
+4

非常優雅的方式來做到這一點。 – Benoit 2011-06-07 11:09:32

+0

真的很好蘭迪! 我使用的是surround.vim插件,所以Benoit的答案對我來說是真正的解決方案。 – retro 2011-06-07 11:14:45

+1

非常好。我發佈了一個補充答案,以提供更多細節。感謝蘭迪! – Rob 2011-06-07 15:14:30

6

愛蘭迪(+1)的答案,我只是瞭解了標籤塊!這只是一個補充答案。

所以yit的意思是「內藏標籤塊」,而桶意味着「進入視覺模式並選擇a(全部)標籤塊」。

這僅僅是對於那些懶得看幫助文件:

Tag blocks      *tag-blocks* 

For the "it" and "at" text objects an attempt is done to select blocks between 
matching tags for HTML and XML. But since these are not completely compatible 
there are a few restrictions. 

The normal method is to select a <tag> until the matching </tag>. For "at" 
the tags are included, for "it" they are excluded. But when "it" is repeated 
the tags will be included (otherwise nothing would change). Also, "it" used 
on a tag block with no contents will select the leading tag. 

"<aaa/>" items are skipped. Case is ignored, also for XML where case does 
matter. 

In HTML it is possible to have a tag like <br> or <meta ...> without a 
matching end tag. These are ignored. 

The text objects are tolerant about mistakes. Stray end tags are ignored.