如何在位于`{`和`}`之间的行中添加四个空格?

时间:2016-11-26 05:07:36

标签: vim format

目标文本文件。

<style type="text/css">
  #wrap{ 
  height: 550px; 
  width: 660px; 
  }
  #wrap ul{ 
  list-style: none; 
  }
  #wrap li{ 
  border-radius:3px; 
  cursor:pointer; 
  }
  #wrap{ 
  height: 550px; 
  width: 660px; 
  }
  #wrap ul{ 
  list-style: none; 
  }
  #wrap li{ 
  border-radius:3px; 
  cursor:pointer; 
  }
</style> 

现在我想在位于{}之间的行的开头添加四个空格,以使上面的内容显示如下。

<style type="text/css">
  #wrap{ 
       height: 550px; 
       width: 660px; 
  }
  #wrap ul{ 
      list-style: none; 
  }
  #wrap li{ 
      border-radius:3px; 
      cursor:pointer; 
  }
  #wrap{ 
      height: 550px; 
      width: 660px; 
  }
  #wrap ul{ 
      list-style: none; 
  }
  #wrap li{ 
      border-radius:3px; 
      cursor:pointer; 
  }
</style> 

执行所有任务以执行任务可能是愚蠢的。

3,4s/^/    /g
7s/^/    /g
10,11s/^/    /g
14,15s/^/    /g
18s/^/    /g
21,22s/^/    /g

有更简单明智的方法吗?

2 个答案:

答案 0 :(得分:0)

假设您的文档的文件类型已正确设置为html且您的制表设置正确,则以下内容就足够了:

:2,23norm ==

但是,要直接回答您的问题,您应该定位包含:

的行
:2,23g/:/s/^/    /

如果您不想手动定义范围,可以执行以下操作:

vit:g/:/s/^/    /

自动插入'<,'>范围。

答案 1 :(得分:-1)

:g/^[^<#}]/ normal >>

说明:

: ........... command
g ........... global command (executes on the following pattern
/ ........... start search pattern
^ ........... beginning of line
[^ ] ........ denied list 
<#} ......... denied chars
/ ........... search pattern's end
normal ...... execute in normal mode
>> .......... indentation

如果此模式过于复发,您可以在~/.vimrc文件上放置一个地图,如下所示:

map <F2> <esc>:g/^[^<#}]/ normal >><cr>

<cr>表示回车

相关问题