vim is a language

92 阅读3分钟

vim is a language

1 Syntax of the language

verb + Noun

e.g. d w ==>  delete word

当光标在单词的第一个位置的时候,可以删除整个单词

备注:command is Repeatable and Undoable

repeatable :按. 可以重复上一条命令

undoable:按u可以撤销上一步命令

1.1 Verb

  • d  == Delete 剪切

  • c  == change

  • >== indent 缩进

  • v  == visually select

  • y  == Yank(copy)

  • p  == paste

  • z  == fold

e.g.

  • c w  ==  change word 光标在word的第一位置

  • > j == 当前行和下一行缩进

  • y w  == copy a word

1 与数字合用

  • nd 删除当前行的下面几行

  • ny 复制当前行的下面几行

2 z的操作

  • za    # 切换(alternative)折叠状态,只能用在已折叠/未折叠的行

  • zR    # 展开所有折叠

  • zM    # 收缩所有折叠

  • zfi{      # 折叠光标当前所在的大括号{里面的文本

  • zfa{      # 折叠光标当前所在的大括号{里面的文本和大括号本身

  • zfG       # 从当前光标所在行开始,折叠到文件尾

  • zf10j     # 从当前光标所在行开始,继续向下折叠10行

  • zfip      # 折叠内部段落

1.2 Nouns in Vim 

####  1 Motions

  • w  == word

  • b  == back

  • 2j == down 2 lines

2 Text Objects

  • iw == inner word

  • it == inner tag

  • i" == inner quotes

  • ip == inner paragrah

  • as == a sentence

e.g 

  • di w/"/'/[/(/{/p删除句子内的 word ""  可以直接查词'' () [] {} paragraph里的内容

  • cit == change inner tag 可用在html中,repeat操作的时候不用在tag中间,即使在tag上面也可以实现

3  Parameterized Text Objects

  • f,F == "find" the next character

  • t,T == "find" the next character 跳到target前面一个字符

  • /   == Search(up to the next match)可以直接查词

2 Tips for mastering the language

2.1 the "dot" command

  • 多用text object(iw rather than w) ciw 《--cw

  • text objects to motions 多用text object 不得已在用motion

  • repeat.vim for plugin repeating

2.2 Relative number

应用场景:


如果想要改变当前编辑行数,下面的几行,你需要做数学运算

但是打开relative number  就可以直接定位到当前行下面的行数了,也可以定位到上面的行数

e.g.

c6j  ==  change 6 down  改变下面六行

3 Visual mode is a smell

visual mode 可能会打断repeatibility

vcw ---> 会打断dot  visual mode

cw

4 Custom operators(plugins)

###  4.1 Tim Pope plugins

1 surround

e.g

  - ds"  == delete surround ""

  - cs"' == change " to '

  - ysiw" == 给词添加""

  • cs"  == 把""变成

  • ysiw[   == 给word用[]包住

2 Commentary

修改了源vim文件,把gc --> 改成了 cm ,具体就是去~/.vim/vundle/ 中的插件去修改

  • cml == comment line

  • cmj == comment current line and next line

  • cmap == comment a paragraph

4.2 replaceWithRegister 

replaceWithRegister  --- 让复制粘贴可以repeatable

一般做法:切换到visual mode 选中后复制粘贴一个词

  • griw === go register inner word 

4.3 titlecase

titlecase  ---- 可以让text object 首字母大写

e.g.

  • gti'  ===  go title inner ''把''的单词首字母大写

  • gtip === 整段

  • sort-motion ----按字母排序 gem文件

  • gsip  ===  go sort inner paragraph

system-copy  ---另外开辟一个clipborad

e.g.

  • cpiw => copy word into system clipboard

  • cpi' => copy inside single quotes to system clipboard

  • cvi' => paste inside single quotes from system clipboard

sort-motion

  • gs2j => Sort down two lines (current + 2 below)

  • gsip => Sort the current paragraphddd

  • gsii => Sort the current indentation level (requires text-obj-indent plugin)

 

custom Text object

indent

  • cmii === comment inner indent

  • cmai === comment all indent 即使中间还有空的indent

entire

  • cmae == comment an entire thing 注释整个文档

  • dae == delete an emtire

line

  • cil === change inner line

code block

  • cmar  === comment a ruby block