Skip to content

Commit 23ca628

Browse files
committed
vim-surround
1 parent dbe1a72 commit 23ca628

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

_posts/vim/2021-10-26-vim_surround.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: vim-surround
3+
category: tools
4+
tags: vim
5+
comments: true
6+
---
7+
## 介绍
8+
9+
> vim-surround,tpope大神的一款life-changed插件,和文本对象组合使用能让更改成对的符号异常方便,安装完成以后直接就可以使用
10+
11+
## 使用
12+
```vim
13+
Old text Command New text ~
14+
"Hello *world!" ds" Hello world!
15+
[123+4*56]/2 cs]) (123+456)/2
16+
"Look ma, I'm *HTML!" cs"<q> <q>Look ma, I'm HTML!</q>
17+
if *x>3 { ysW( if ( x>3 ) {
18+
my $str = *whee!; vlllls' my $str = 'whee!';
19+
<div>Yo!*</div> dst Yo!
20+
<div>Yo!*</div> cst<p> <p>Yo!</p>
21+
22+
# *代表当前光标位置,添加替换时使用后半括号)]},添加的括号和内容间就没有空格(如第2个示例),反之会在内容前后添加一个空格(如第4个实例)
23+
```
24+
25+
## 命令列表
26+
27+
```vim
28+
Normal mode
29+
-----------
30+
ds - delete a surrounding
31+
cs - change a surrounding
32+
ys - add a surrounding
33+
yS - add a surrounding and place the surrounded text on a new line + indent it
34+
yss - add a surrounding to the whole line
35+
ySs - add a surrounding to the whole line, place it on a new line + indent it
36+
ySS - same as ySs
37+
38+
Visual mode
39+
-----------
40+
s - in visual mode, add a surrounding
41+
S - in visual mode, add a surrounding but place text on new line + indent it
42+
43+
Insert mode
44+
-----------
45+
<CTRL-s> - in insert mode, add a surrounding
46+
<CTRL-s><CTRL-s> - in insert mode, add a new line + surrounding + indent
47+
<CTRL-g>s - same as <CTRL-s>
48+
<CTRL-g>S - same as <CTRL-s><CTRL-s>
49+
```
50+
51+
## Vim基于surrounding的文本编辑
52+
提到了surround插件,不得不提一下Vim中广为人知的对surrounding内文本的编辑功能,其实surround插件就是对Vim这部分功能的增强。原理和细节请参照vim中的[text-object motion](http://vimdoc.sourceforge.net/htmldoc/motion.html),这里只列举一些常见用法。可以不夸张的说,任何习惯了vim中[operation+motion](https://blog.carbonfive.com/vim-text-objects-the-definitive-guide/)操作的人都会上瘾的,其他编辑器都是个渣了。
53+
54+
以修改surrounding内文本为例:
55+
```vim
56+
ci[ ci( ci< ci{ 删除一对 [], (), <>, 或{} 中的所有字符并进入插入模式
57+
ci” ci’ ci` 删除一对引号字符 ” ‘ 或 ` 中所有字符并进入插入模式
58+
cit 删除一对 HTML/XML 的标签内部的所有字符并进入插入模式
59+
```
60+
其他常见operation
61+
62+
```vim
63+
ci: 例如,ci(,或者ci),将会修改()之间的文本;
64+
di: 剪切配对符号之间文本;
65+
yi: 复制;
66+
ca: 同ci,但修改内容包括配对符号本身;
67+
da: 同di,但剪切内容包括配对符号本身;
68+
ya: 同yi,但复制内容包括配对符号本身。
69+
PS. dib等同于di(。diB等同于di{。
70+
```
71+
## 转自
72+
[http://zuyunfei.com/2013/04/17/killer-plugin-of-vim-surround/](http://zuyunfei.com/2013/04/17/killer-plugin-of-vim-surround/)

0 commit comments

Comments
 (0)