你应该知道的vim插件之surround.vim

写代码的时候你会发现这个插件是多么有用!
强烈推荐!

0×01.change

1
2
3
4
5
6
cs"'
cs"<q>
cs)]
cst<p>
csw'
csW'
1
2
3
4
5
6
7
8
9
10
11
文本              命令        结果
---------------   -------    -----------
"Hello |world!"   cs"'       'Hello world!'
"Hello |world!"   cs"<q>     <q>Hello world!</q>
(123+4|56)/2      cs)]       [123+456]/2
(123+4|56)/2      cs)[       [ 123+456 ]/2
<div>foo|</div>   cst<p>     <p>foo</p>
fo|o!             csw'       'foo'!
fo|o!             csW'
      'foo!'

(| 为光标位置)

0×02.adding
比如,我要把这个小标题外围用strong标签包括起来,再也不用像以后那样
敲了,只需要:

1
yss<strong>
1
2
3
4
5
6
7
8
9
10
Text              Command      New Text
---------------   -------      -----------
Hello w|orld!     ysiw)        Hello (world)!
Hello w|orld!     csw)         Hello (world)!
fo|o              ysiwt<html>  <html>foo</html>
foo quu|x baz     yss"         "foo quux baz"
foo quu|x baz     ySS"         "
                                   foo quux baz
                               "

(| is the position of cursor in these examples)

甚至在编辑模式时也可以添加surroundings,这时是通过 ctrl+s 映射来支持的。
千万要注意:
在很多终端里面,这个快捷键会终止输出然后你的会话将被冻结!
如果这样的杯具发生了的话,不要慌,只须按ctrl+q 来解冻。
如果你想从你的终端里移除ctrl+s映射的话,把 stty stop 添加到你的shell启动配置
文件(如bash的.bashrc ,ksh的.kshrc 等 )。
一些插入模式时的例子:

1
2
3
4
5
6
7
8
Command                  New Text
-------                  ------------
<CTRL-s>"                ""
<CTRL-s><CTRL-s><html>   <html>
                             |
                         </html>

(| is the position of cursor in these examples)

0×03.delete

1
ds

全部命令和快捷键参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Normal mode
-----------
ds  - delete a surrounding
cs  - change a surrounding
ys  - add a surrounding
yS  - add a surrounding and place the surrounded text on a new line + indent it
yss - add a surrounding to the whole line
ySs - add a surrounding to the whole line, place it on a new line + indent it
ySS - same as ySs

Visual mode
-----------
s   - in visual mode, add a surrounding
S   - in visual mode, add a surrounding but place text on new line + indent it

Insert mode
-----------
<CTRL-s> - in insert mode, add a surrounding
<CTRL-s><CTRL-s> - in insert mode, add a new line + surrounding + indent
<CTRL-g>s - same as <CTRL-s>
<CTRL-g>S - same as <CTRL-s><CTRL-s>

下载地址:
surround.vim – Delete/change/add parentheses/quotes/XML-tags/much more with ease : vim online

更多信息,详见:

1
:h surround

本文参考资料:
Vim Plugins You Should Know About, Part I: surround.vim – good coders code, great reuse

更多
No Responses Post a comment

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS