English 中文(简体)
使用 vim 删除 div 内的新行
原标题:Remove new lines inside a div using vim
  • 时间:2012-05-26 11:43:29
  •  标签:
  • vim
  • macvim

我有这样的数据

<div>
This
is
some text
</div>

我想最终与这个

<div>
This is some text
</div>

I know I can search and replace for , but I want to limit that search for only the lines inside the <div> tag.

我有一个很长的 HTML 文件, 上千个Divs, 没有阶级或ID。

我怎么能这样对Vim?

最佳回答
  1. Start recording a macro (in register d) with qd
  2. Search for an opening DIV tag with /<div (then press return)
  3. Use visual mode to select everything inside the DIV with vit
  4. Change the visual selection so that it doesn t include the line with the opening tag with oj
  5. Join the lines with J
  6. Stop recording with q

现在您可以使用 (或者,对于固定的重复次数,您可以用数字前缀它,例如 20@d ) 以所需的倍数回放宏。

(这个解决方案假设,如您的例子所示, 开放标签和贴近标签总是在他们自己的线条上。 另外, 如果 DIV 中只有一行内容, 那么关闭标签最终会和内容一样的线条上 。)

问题回答

将您的光标移动到 块, 请尝试 :

v i t o j J


要加入所有未覆盖的 区块, 请尝试 :

:g/<div>/+1,/</div>/-1 join

你可以做一个这样的:

  • Select the tree lines with V, and use J to put them in the same line.
  • Put the cursor on the second line and use 3J.
  • Put your cursor on the second line, and do J two times.

如果您在“ 此行” 上, VjjJ (选择整行, 下两次, 用分隔符加入行 。 )

-- -- -- -- --

如果您想要最终结果为 : 将光标放置在标签中任何地方, 并执行 vatJ (视觉模式, 选择包容性, 选择标签, 加入行) 。

:g/<div>/norm! jVjjJ 

:................... command
g................... global
/<div> ............. search for <div>
norm! .............. in normal mode
j................... down one line  j , 
V .................. visual by line  V  
jj ................. down more two lines
J .................. upper  J  join lines

对于可变行数试行数

:g/<div>/norm! jV//div^MkJ

:............. command
g ............ global
/<div> ....... search div
norm!  ....... execute in normal mode
j ............ down one line
V ............ start visual mode
//div ....... search div close
^M ........... type <ctrl-v> in linux or <ctrl-k> on windows, after that <enter>
k ............ up one line
J ............ joine all selected lines

On linux to insert <Enter> press <ctrl-v><enter>
On windows try <ctrl-k><enter>




相关问题
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 ...