你应该知道的vim插件之surround.vim » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

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

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

0×01.change

cs"'
cs"
cs)]
cst

csw' csW'

文本              命令        结果
---------------   -------    -----------
"Hello |world!"   cs"'       'Hello world!'
"Hello |world!"   cs"     Hello world!
(123+4|56)/2      cs)]       [123+456]/2
(123+4|56)/2      cs)[       [ 123+456 ]/2
foo|
cst

foo

fo|o! csw' 'foo'! fo|o! csW' 'foo!' (| 为光标位置)

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

yss
Text              Command      New Text
---------------   -------      -----------
Hello w|orld!     ysiw)        Hello (world)!
Hello w|orld!     csw)         Hello (world)!
fo|o              ysiwt  foo
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 等 )。
一些插入模式时的例子:

Command                  New Text
-------                  ------------
"                ""
   
                             |
                         

(| is the position of cursor in these examples)

0×03.delete

ds

全部命令和快捷键参考:

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
-----------
 - in insert mode, add a surrounding
 - in insert mode, add a new line + surrounding + indent
s - same as 
S - same as 

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

更多信息,详见:

:h surround

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

Tagged in : VIM,plugin,surround

All Comments (0)
Gravatar image
No Comments