配置见dotfiles
快捷键
Movement
h- Move leftj- Move downk- Move upl- Move right0- Move to beginging of line, 也可以使用Home.^- 在有tab或space的代码行里,0是移到最行首, 而^是移到代码行首$- Move to end of linegg- Move to first line of fileG- Move to last line of filengg- 移动到指定的第n行, 也可以用nGw- Move forward to next wordb- Move backward to next word%- 在匹配的括号、块的首尾移动C-o- 返回到上次移动前的位置, 也可以用两个单引号'C-i- 前进到后一次移动的位置f- 后接字符,移动到当前行的下一个指定字符,然后按;继续搜索下一个F- 同上,移动到上一个|- 竖线,前接数字,移动到当前行的指定列,如30|,移动到当前行的第30列
Search
*- Search forward for word under cursor#- Search backward for word under curor/word- Search forward for word. Support RE?word- Search backward for word. Support REn- Repeat the last/or?commandN- Repeat the last/or?command in opposite direction
在搜索后, 被搜索的单词都会高亮, 一般想取消那些高亮的单词, 可以再次搜索随便输入一些字母, 搜索不到自然就取消了. 另外也可以使用 nohl 取消这些被高亮的词.
Deletion
x- Delete character forward(under cursor), and remain in normal modeX- Delete character backward(before cursor), and remain in normal moder- Replace single character under cursor, and remain in normal modes- Delete single character under cursor, and switch to insert modeshift+~- 这个可以把光标下的单词转换为大写/小写, 并自动移到下一个字符dw- Delete a word forwarddaw- 上面的dw是删除一个单词的前向部分, 而这个是删除整个单词, 不论cursor是否在单词中间db- Delete a word backwarddd- Delete entire current lineD- Delete until end of line
Yank & Put
y- Yank(copy)yy- Yank current linenyy- Yanknlines form current linep- Put(paste) yanked text below current lineP- Put(paste) yanked text above current line
Insert Mode
i- Enter insert mode to the left of the cursora- Enter insert mode to the right of the cursoro- Enter insert mode to the line below the current lineO- Enter insert mode to the line above the current line
Visual Mode
v- Enter visual mode, highlight charactersV- Enter visual mode, highlight linesC-v- Enter visual mode, highlight block
Other
u- UndoU- Undo all changes on current lineC-r- Redo
Read More
Vim自定义插件
依葫芦画瓢, 写了一个针对特定格式的高亮插件时的需求。
vimdiff
vim套件中的对比工具
]c- 跳到下一个差异点[c- 上一个差异点dp- diff put, 将差异点的内容从当前文件复制到另一文件do- diff get, 相反,从另一文件复制到当前文件]
其它版本
- neovim 号称是重构了vim,速度变快?暂时没感觉出和传统vim的区别
-
macvim 试了下,还不错,可以替换本地一直作为记事本的TextMate了
使用brew安装上macvim后, 终端使用
mvim打开App, 但是按任何键都没有反应。需要把当前shell退出,重新打开才行。这个issue可以参考下。(折腾了我半天。。。)
技巧
shell多行注释
命令行模式下,注释掉line1与line2之间的行
line1,line2s/^/#/g
自动补全
有什么omni(智能补全?), 自动补全啥的, 没去研究.
- Are there any autocompletion plugins for vim?
- Vim autocomplete for Python
- Is it possible to have vim auto-complete function names, variables, etc. when using it to program?
- vi/vim使用进阶: 智能补全
- vi/vim使用进阶: 自动补全
- VIM的JavaScript补全 介绍了ins-completion的几个快捷键
左右分割打开help文档
默认是上下分割来打开文档,但是对于宽屏,左右分割反而更加方便
:vert help xxx
逐个替换
全文直接替换:
:%s/old_str/new_str/g
加上参数c可以逐个替换,这样可以对每一个再确认:
:%s/old_str/new_str/gc
关于 search/replace 中的换行符
Search:
\n is newline, \r is CR(carriage return = Ctrl-M = ^M)
Replace:
\r is newline, \n is a null byte(0x00)
比如字符串 test1,test2,test3 把逗号换成换行:
%s/,/\r/g
关于.vimrc和plugin的加载
具体见:help --noplugin:
--noplugin Skip loading plugins. Resets the 'loadplugins' option. {not in Vi} Note that the |-u| argument may also disable loading plugins: argument load vimrc files load plugins (nothing) yes yes -u NONE no no -u NORC no yes --noplugin yes no
重新加载配置:
# % 表示当前文件, so是source简写 :so % # 指定配置文件 :so ~/.vimrc
删除所有匹配的行
# 删除所有包含abcd的行 :g/abcd/d
参考: Delete all lines containing a pattern
vim调试插件
在使用dash.vim时遇到的一个问题, 想看看插件中某些变量的值是多少。搜到这篇文章:Echoing Messages
比如在插件里加上:
echom var_name
然后在触发到这条后, 可以执行下面来查看输出
:messages
忽略大小写搜索
首先有两个bool开关:
ignorecase忽略大小写smartcase智能选择匹配, 如果搜索pattern是全小写,则忽略大小写匹配;如果pattern包含大写,则会精准匹配
另外,\c, \C可以覆盖ignorecase和smartcase的配置。
\c表示搜索pattern忽略大小写,如下都会忽略大小写匹配hello:
# 最前的`/`表示vi中的开始搜索 /\chello /\cHello /hello\c /HELLO\c
\C和\c相关,表示大小写敏感。
具体可以:
:help /\c :help /\C :help smartcase :help ignorecase
参考 How to do case insensitive search in Vim
查看某个按键的映射
一般用于排查按键冲突,可以看某个按键实际被谁绑定了。
以tab为例,本地被supertab插件绑定了:
:verbose imap <tab> i <Tab> <Plug>SuperTabForward Last set from ~/.vim/bundle/supertab/plugin/supertab.vim
查看加载的文件
:scriptnames
快速跳回到上次位置
''或者``
具体见:Move cursor to its last position
插入特殊字符
最近看Go的字符串编码那块, 想在vim上敲一些特殊字符, 但是不知道怎么弄, 只能在网上找到这个字符, 然后C-v复制到vim里, 但是不行, 于是研究了下这块。
:digraphs可以看到支持的特殊字符列表, 比如u: ü 252, vim定义了两个字符的组合(即digraph)来映射特殊字符, 如这里使用u:来表示ü; 第三个252表示赋予它的十进制编码.
其中有两种方式来键入特殊字符(:h digraphs-use):
CTRL-K {char1} {char2}# 任何模式都行{char1} <BS> {char2}# 只支持在digraph模式下,即:set digraph
第二种方式还有点疑问, 貌似只能在命令行模式里输入
光标在字符上, 普通模式敲入ga, 可以查看这个字符(包括特殊字符)的十进制,十六进制等: <ü> 252, Hex 00fc, Octal 374
还可以通过敲入16进制符来输出特殊字符, <C-v>uXXXX; 比如上面这个十六进制是00fc, 插入模式输入 <C-v>u00fc
其它参考:
- Vim: enter Unicode characters with 8-digit hex code
- Vim digraphs 里面列了一些比较有趣的字符
- chrisbra/unicode.vim 针对unicode字符加强功能的插件
参考:
- How to replace a character for a newline in Vim?
- Why is \r a newline for Vim?
- How can I add a string to the end of each line in Vim?
- VIM参考手册