├── .gitignore ├── chapter ├── Technical quirks.md ├── Common problems.md ├── Miscellaneous.md ├── function-search-undo.md ├── why-hjkl-for-navigation.md ├── editing-huge-files-is-slow.md ├── vim-distributions.md ├── bracketed-paste.md ├── editing-small-files-is-slow.md ├── List-Of-Colorschemes.md ├── map-capslock-to-control.md ├── standard-plugins.md └── List-Of-Plugins.md ├── contents └── minimal-vimrc.vim ├── tools └── chinese_linter.vim ├── CONTRIBUTING.md ├── PLUGINS.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | tags 2 | -------------------------------------------------------------------------------- /chapter/Technical quirks.md: -------------------------------------------------------------------------------- 1 | # 技术怪癖 2 | 3 | ## NUL 符用新行表示 4 | -------------------------------------------------------------------------------- /chapter/Common problems.md: -------------------------------------------------------------------------------- 1 | # 常见问题 2 | 3 | ## 编辑小文件很慢 4 | 5 | ## 编辑大文件很慢 6 | 7 | ## 相同部分粘贴 (要不为什么我总要设置‘粘贴’?) 8 | 9 | ## 使用终端中 Esc 键时的延迟 10 | 11 | ## 无法重复函数中执行的搜索 12 | -------------------------------------------------------------------------------- /chapter/Miscellaneous.md: -------------------------------------------------------------------------------- 1 | # 杂项 2 | 3 | ## 附加资源 4 | 5 | ## Vim 配置集合 6 | 7 | ## 内置插件 8 | 9 | ## 映射大写锁定键(CapsLock)到控制键(Ctrl) 10 | 11 | ## 复活节彩蛋 12 | 13 | ## 为什么用 hjkl 移动光标? 14 | 15 | -------------------------------------------------------------------------------- /chapter/function-search-undo.md: -------------------------------------------------------------------------------- 1 | ## 无法重复函数中执行的搜索 2 | 3 | - 一个命令的搜索关键词(`/`, `:代替`, ...)改变了“上一次搜索所用的关键词”。(这个改变保存在 `/` 的注册表里;使用 `:echo @/` 可以打印出来) 4 | - 一个简单的文本修改可以用 `.` 再次执行。(这个执行保存在 `.` 的注册表里;使用 `:echo @.` 可以打印出来) 5 | 6 | 如果你在一个函数里做以上操作,两件都_not_会是那么回事儿。所以你不能简单地在一个函数里高亮词汇同时再用这个函数做文本修改。 7 | 8 | 帮助: `:h function-search-undo` -------------------------------------------------------------------------------- /chapter/why-hjkl-for-navigation.md: -------------------------------------------------------------------------------- 1 | ## 为什么用hjkl移动光标? 2 | 3 | 当 [Bill Joy](https://en.wikipedia.org/wiki/Bill_Joy) 创建[vi](https://en.wikipedia.org/wiki/Vi),Vim 的前身时, 他的工作是在一台[ADM-3A](https://en.wikipedia.org/wiki/ADM-3A)完成的,而这个机器没有用别的键控制光标,而正是如你猜到的 hjkl 作为光标控制键。 4 | 5 | 键盘布局: [请点击](https://raw.githubusercontent.com/mhinz/vim-galore/master/contents/images/content-adm-3a-layout.jpg) 6 | 7 | 这也告诉了我们为什么 Unix 系统会以`~`代表主目录。 8 | 9 | -------------------------------------------------------------------------------- /chapter/editing-huge-files-is-slow.md: -------------------------------------------------------------------------------- 1 | ## 编辑大文件很慢 2 | 3 | 大文件最大的问题,就是 Vim 会一次性读取整个文件。这一过程的完成取决于内部如何提供缓存空间。 4 | ([Vim 开发的讨论](https://groups.google.com/forum/#!topic/vim_dev/oY3i8rqYGD4/discussion)) 5 | 6 | 如果你只是想读取, `tail hugefile | vim -` 会是一个很好的解决方法. 7 | 8 | 如果不用语法,设定和插件,你也能生活自理一段时间: 9 | 10 | ``` 11 | $ vim -u NONE -N 12 | ``` 13 | 14 | 这将使得导航明显提速,因为代价高昂的语法高亮正则表达式不再被使用。你也应该让 Vim 不要使用交换文件和 Vim 信息文件,以避免写入时的长延迟。 15 | 16 | ``` 17 | $ vim -n -u NONE -i NONE -N 18 | ``` 19 | 20 | 简而言之,真的写入超大文件的时候尽量别用 Vim。 :\ 21 | -------------------------------------------------------------------------------- /chapter/vim-distributions.md: -------------------------------------------------------------------------------- 1 | ## Vim 配置集合 2 | 3 | Vim 配置集合是集成多多种插件以及相关配置的组合。 4 | 5 | 高阶用户总是知道如何构建自己的编辑器,所以配置集合一般是针对初学者。 6 | 如果你想要尝试,那么将面临一个辩证的想法:加入很多额外的东西来学习 Vim 是否会让这变得更加简单。 7 | 8 | 我知道很多人不想在配置编辑器上花过多时间(但实际上,当你卡住时,你就会不停地花时间定制自己的 vimrc) 9 | 因此,想要节约时间,那么就因该真正的去了解和学习 Vim 原生功能,只有这样你才能中获益。 10 | 11 | 再次强调一下:“一个程序员应当懂得他的工具”。 12 | 13 | 总之,如果你知道自己在做什么,你也许能从以下几个 Vim 配置集合中获得一些灵感。 14 | 15 | - [cream](http://cream.sourceforge.net) 16 | - [janus](https://github.com/carlhuda/janus.git) 17 | - [spacevim](https://github.com/SpaceVim/SpaceVim) 18 | - [spf13](https://github.com/spf13/spf13-vim) 19 | -------------------------------------------------------------------------------- /chapter/bracketed-paste.md: -------------------------------------------------------------------------------- 1 | ## 粘贴归类 (要不为什么我总要设置‘粘贴’?) 2 | 3 | 粘贴归类模式使得终端虚拟器可以区分键入文本和粘贴文本。 4 | 5 | 你是否曾经尝试过向 Vim 粘贴过代码,结果弄得一团糟? 6 | 7 | 这只会在你通过 `cmd+v`, `shift-insert`, `middle-click` 等等命令进行粘贴时发生。 8 | 因为那时你只是在向终端虚拟器里丢文本。Vim 并不知道你在粘贴文本,它天真地认为你是一个熟练的打字员。因此,它尝试排版,但是失败了。 9 | 10 | 如果你使用 Vim 命令集里的粘贴,比如 `"+p` ,那这就不会成为问题,因为 Vim 知道你做的是粘贴操作。 11 | 12 | 为了解决这个,你需要设置粘贴 `:set paste` ,以便获取粘贴现状。 参考 `:h 'paste'` 和 `:h 'pastetoggle'`. 13 | 14 | 如果你已经受够了不停地切换 `'paste'` ,看一看这个不错的插件能为你做什么:[粘贴归类](https://github.com/ConradIrwin/vim-bracketed-paste)。 15 | 16 | 插件作者的拓展阅读:[欢迎点击](http://cirw.in/blog/bracketed-paste). 17 | 18 | **Neovim**: Neovim 努力无缝地完成这些工作,并在终端虚拟器支持时自动设定粘贴归类模式。 19 | -------------------------------------------------------------------------------- /chapter/editing-small-files-is-slow.md: -------------------------------------------------------------------------------- 1 | ## 编辑小文件很慢 2 | 3 | 对性能有重大影响的有两个方面: 4 | 5 | 1. 复杂的**正则表达式**。尤其是 Ruby 语法文件导致人们过去一直慢慢吞吞。([Debugging syntax files](#debugging-syntax-files)里也有说明。) 6 | 2. **页面刷新**。一些特性强制所有行都刷新。 7 | 8 | | 典型败笔 | 原因? | 解决方法? | 9 | |-----------------|------|-----------| 10 | | `:set cursorline` | 导致所有行刷新。 | `:set nocursorline` | 11 | | `:set cursorcolumn` | 导致所有行刷新。 | `:set nocursorcolumn` | 12 | | `:set relativenumber` | 导致所有行刷新。 | `:set norelativenumber` | 13 | | `:set foldmethod=syntax` | 语法文件算减速的话,这无异于刹车。 | `:set foldmethod=manual`, `:set foldmethod=marker` or [快速折叠](https://github.com/Konfekt/FastFold) | 14 | | `:set synmaxcol=3000` | 根据内部表示规则, Vim 一般不待见对很长的行。这一设定导致3000列时才会把所在行高亮。 | `:set synmaxcol=200` | 15 | | matchparen.vim           | 使用默认加载。使用正则表达式去匹配括回。 | 禁用插件: `:h matchparen` | 16 | 17 | **注意**: 只有当真的遭遇实质的性能缺陷时,你才需要做这些改进。多数情况下,上面提到的这些方面还都凑合。 18 | -------------------------------------------------------------------------------- /chapter/List-Of-Colorschemes.md: -------------------------------------------------------------------------------- 1 | # 配色方案列表 2 | 3 | 这儿列举了一些常用的Vim配色方案: 4 | 5 | - [acme-colors](https://github.com/plan9-for-vimspace/acme-colors) 6 | - [base16](https://github.com/chriskempson/base16-vim) 7 | - [gotham](https://github.com/whatyouhide/vim-gotham) 8 | - [gruvbox](https://github.com/morhetz/gruvbox) 9 | - [janah](https://github.com/mhinz/vim-janah) 10 | - [jellybeans](https://github.com/nanotech/jellybeans.vim) 11 | - [lucius](https://github.com/jonathanfilip/vim-lucius) 12 | - [molokai](https://github.com/tomasr/molokai) 13 | - [railscasts](https://github.com/jpo/vim-railscasts-theme) 14 | - [seoul256](https://github.com/junegunn/seoul256.vim) 15 | - [solarized](https://github.com/altercation/vim-colors-solarized) (或者它浅色衍生方案 [flattened](https://github.com/romainl/flattened)) 16 | - [tomorrow](https://github.com/chriskempson/vim-tomorrow-theme) 17 | - [vividchalk](https://github.com/tpope/vim-vividchalk) 18 | - [yowish](https://github.com/kabbamine/yowish.vim) 19 | - [zenburn](https://github.com/jnurmine/Zenburn) 20 | 21 | -------------------------------------------------------------------------------- /chapter/map-capslock-to-control.md: -------------------------------------------------------------------------------- 1 | ## 映射大写锁定键(CapsLock)到控制键(Ctrl) 2 | 3 | 大写锁定键属于键盘上最少用到的按键,但是它要比控制键更加容易按到, 4 | 因为它位于你手放在键盘时的[起始行](https://raw.githubusercontent.com/mhinz/vim-galore/master/contents/images/content-homerow.png)。 5 | 如果你敲很多代码的话,映射大写锁定键(CapsLock)到控制键(Ctrl)可以有效降低 6 | [重复使力伤害](https://de.wikipedia.org/wiki/Repetitive-Strain-Injury-Syndrom) (ps:为什么将 jk 映射为 esc,而不是 jj,重复点击同一个按键对手指伤害很大)。 7 | 8 | 友情提示:当你习惯这个设定后,你就离不开它了。 9 | 10 | **OSX**: 11 | 12 | `System Preferences -> Keyboard -> Keyboard Tab -> Modifier Keys`. 变更 13 | "CapsLock" 为 "Control"。 14 | 15 | **Linux**: 16 | 17 | 为了使按键生效,请把以下几行放入 `~/.xmodmap` 文件: 18 | 19 | remove Lock = Caps_Lock 20 | keysym Caps_Lock = Control_L 21 | add Control = Control_L 22 | 23 | 用以下命令使之生效
`$ xmodmap ~/.xmodmap`。 24 | 25 | 替代方法有[caps2esc](https://github.com/oblitum/caps2esc) 或者 26 | [xcape](https://github.com/alols/xcape)。 27 | 28 | **Windows**: 29 | 30 | 请参看 [superuser.com: 在Windows8.1映射大写锁定键到控制键](http://superuser.com/questions/764782/map-caps-lock-to-control-in-windows-8-1)。 31 | -------------------------------------------------------------------------------- /chapter/standard-plugins.md: -------------------------------------------------------------------------------- 1 | ## 内置插件 2 | 3 | 出乎很多人的意料,Vim自己默认加载了大把大把的插件。启动 Vim 后,用`:scriptnames`查看所有加载的源文件 4 | 5 | 这些文件多数永远不会被用到,所以请禁止如你所见的以下插件。 6 | 这些插件仍旧会在源文件列出,但是当Vim启动时,只有第一行会被读取,而之后的配置,命令和日志都不会执行。 7 | 8 | | 插件 | 禁用 | 帮助 | 9 | |------------|-------------------------------------|------| 10 | | 2html | `let g:loaded_2html_plugin = 1` | `:h 2html` | 11 | | getscript | `let g:loaded_getscriptPlugin = 1` | `:h pi_getscript` | 12 | | gzip | `let g:loaded_gzip = 1` | `:h pi_gzip` | 13 | | logipat | `let g:loaded_logipat = 1` | `:h pi_logipat` | 14 | | matchparen | `let g:loaded_matchparen = 1` | `:h pi_paren` | 15 | | netrw | `let g:loaded_netrwPlugin = 1` | `:h pi_netrw` | 16 | | rrhelper | `let g:loaded_rrhelper = 1` | `:e $VIMRUNTIME/plugin/rrhelper.vim` | 17 | | spellfile | `let g:loaded_spellfile_plugin = 1` | `:h spellfile.vim` | 18 | | tar | `let g:loaded_tarPlugin = 1` | `:h pi_tar` | 19 | | vimball | `let g:loaded_vimballPlugin = 1` | `:h pi_vimball` | 20 | | zip | `let g:loaded_zipPlugin = 1` | `:h pi_zip` | 21 | -------------------------------------------------------------------------------- /contents/minimal-vimrc.vim: -------------------------------------------------------------------------------- 1 | " 2 | " A (not so) minimal vimrc. 3 | " 4 | 5 | " You want Vim, not vi. When Vim finds a vimrc, 'nocompatible' is set anyway. 6 | " We set it explicitely to make our position clear! 7 | set nocompatible 8 | 9 | filetype plugin indent on " Load plugins according to detected filetype. 10 | syntax on " Enable syntax highlighting. 11 | 12 | set autoindent " Indent according to previous line. 13 | set expandtab " Use spaces instead of tabs. 14 | set softtabstop =4 " Tab key indents by 4 spaces. 15 | set shiftwidth =4 " >> indents by 4 spaces. 16 | set shiftround " >> indents to next multiple of 'shiftwidth'. 17 | 18 | set backspace =indent,eol,start " Make backspace work as you would expect. 19 | set hidden " Switch between buffers without having to save first. 20 | set laststatus =2 " Always show statusline. 21 | set display =lastline " Show as much as possible of the last line. 22 | 23 | set showmode " Show current mode in command-line. 24 | set showcmd " Show already typed keys when more are expected. 25 | 26 | set incsearch " Highlight while searching with / or ?. 27 | set hlsearch " Keep matches highlighted. 28 | 29 | set ttyfast " Faster redrawing. 30 | set lazyredraw " Only redraw when necessary. 31 | 32 | set splitbelow " Open new windows below the current window. 33 | set splitright " Open new windows right of the current window. 34 | 35 | set cursorline " Find the current line quickly. 36 | set wrapscan " Searches wrap around end-of-file. 37 | set report =0 " Always report changed lines. 38 | set synmaxcol =200 " Only highlight the first 200 columns. 39 | 40 | set list " Show non-printable characters. 41 | if has('multi_byte') && &encoding ==# 'utf-8' 42 | let &listchars = 'tab:▸ ,extends:❯,precedes:❮,nbsp:±' 43 | else 44 | let &listchars = 'tab:> ,extends:>,precedes:<,nbsp:.' 45 | endif 46 | 47 | " The fish shell is not very compatible to other shells and unexpectedly 48 | " breaks things that use 'shell'. 49 | if &shell =~# 'fish$' 50 | set shell=/bin/bash 51 | endif 52 | 53 | " Put all temporary files under the same directory. 54 | " https://github.com/mhinz/vim-galore#handling-backup-swap-undo-and-viminfo-files 55 | set backup 56 | set backupdir =$HOME/.vim/files/backup/ 57 | set backupext =-vimbackup 58 | set backupskip = 59 | set directory =$HOME/.vim/files/swap// 60 | set updatecount =100 61 | set undofile 62 | set undodir =$HOME/.vim/files/undo/ 63 | set viminfo ='100,n$HOME/.vim/files/info/viminfo 64 | -------------------------------------------------------------------------------- /tools/chinese_linter.vim: -------------------------------------------------------------------------------- 1 | scriptencoding utf-8 2 | " 忽略的错误 3 | let g:chinese_linter_disabled_nr = get(g:,'chinese_linter_disabled_nr', []) 4 | " 中文标点符号(更全) 5 | let s:CHINEXE_PUNCTUATION = "\u3002\uff1f\uff01\uff0c\u3001\uff1b\uff1a\u201c\u201d\u2018\u2019\uff08\uff09\u300a\u300b\u3008\u3009\u3010\u3011\u300e\u300f\u300c\u300d\ufe43\ufe44\u3014\u3015\u2026\u2014\uff5e\ufe4f\uffe5" 6 | " 英文标点 7 | let s:punctuation = ',' 8 | " 中文标点符号 9 | let s:punctuation_cn = '[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]' 10 | " 中文汉字 11 | let s:chars_cn = '[\u4e00-\u9fa5]' 12 | " 数字 13 | let s:numbers = '[0-9]' 14 | " 英文字母 15 | let s:chars = '[a-zA-Z]' 16 | let s:ERRORS = { 17 | \ 'E001' : ['中文字符后存在英文标点', s:chars_cn . s:punctuation], 18 | \ 'E002' : ['中英文之间没有空格', '\(' . s:chars_cn . s:chars . '\)\|\(' . s:chars . s:chars_cn . '\)'], 19 | \ 'E003' : ['中文与数字之间没有空格', '\(' . s:chars_cn . s:numbers . '\)\|\(' . s:numbers . s:chars_cn . '\)'], 20 | \ 'E004' : ['中文标点之后存在空格', s:punctuation_cn . '\s\+'], 21 | \ 'E005' : ['行尾含有空格', '\s\+$'], 22 | \ } 23 | 24 | command! -nargs=? CheckChinese call s:check() 25 | 26 | function! s:check(...) abort 27 | let s:file = getline(1,'$') 28 | let s:bufnr = bufnr('%') 29 | let s:linenr = 0 30 | let s:colnr = 0 31 | let s:qf = [] 32 | for l:line in s:file 33 | let s:linenr += 1 34 | call s:parser(l:line) 35 | endfor 36 | let s:linenr = 0 37 | let s:colnr = 0 38 | if !empty(s:qf) 39 | let g:wsd = s:qf 40 | call s:update_qf(s:qf) 41 | copen 42 | else 43 | call setqflist([]) 44 | cclose 45 | endif 46 | endfunction 47 | 48 | function! s:parser(line) abort 49 | for l:error_nr in keys(s:ERRORS) 50 | if index(g:chinese_linter_disabled_nr, l:error_nr) == -1 51 | call s:find_error(l:error_nr, a:line) 52 | endif 53 | endfor 54 | endfunction 55 | 56 | function! s:find_error(nr, line) abort 57 | let l:error = s:ERRORS[a:nr] 58 | let s:colnr = matchend(a:line, l:error[1]) 59 | if s:colnr != -1 60 | call s:add_to_qf(a:nr) 61 | endif 62 | endfunction 63 | 64 | function! s:add_to_qf(nr) abort 65 | let l:error_item = { 66 | \ 'bufnr' : s:bufnr, 67 | \ 'lnum' : s:linenr, 68 | \ 'col' : s:colnr, 69 | \ 'vcol' : 0, 70 | \ 'text' : a:nr . ' ' . s:ERRORS[a:nr][0], 71 | \ 'nr' : a:nr, 72 | \ 'type' : 'E' 73 | \ } 74 | call add(s:qf, l:error_item) 75 | endfunction 76 | 77 | " TODO 加入语法分析 78 | 79 | 80 | function! s:update_qf(dict) abort 81 | call setqflist(a:dict) 82 | endfunction 83 | -------------------------------------------------------------------------------- /chapter/List-Of-Plugins.md: -------------------------------------------------------------------------------- 1 | ## 插件列表 2 | 3 | #### [以功能区分](#以功能区分-1) 4 | 5 | - [文本对齐](#文本对齐) 6 | - [构建和可疑代码标记](#构建和可疑代码标记) 7 | - [代码补全](#代码补全) 8 | - [Cycle](#cycle) 9 | - [注释工具](#注释工具) 10 | - [分割符](#分割符) 11 | - [模糊搜索](#模糊搜索) 12 | - [Grep 工具](#grep-工具) 13 | - [缩进](#缩进) 14 | - [文件导航](#文件导航) 15 | - [代码片段](#代码片段) 16 | - [状态栏](#状态栏) 17 | - [文字环绕符](#文字环绕符) 18 | - [记笔记](#记笔记) 19 | - [文本对象](#文本对象) 20 | - [Tmux](#tmux) 21 | - [撤销历史](#撤销历史) 22 | - [版本控制](#版本控制) 23 | - [写作工具](#写作工具) 24 | - [其他](#其他) 25 | 26 | #### [以文件类型区分](#以文件类型区分-1) 27 | 28 | - [C and C++](#c-and-c) 29 | - [Clojure](#clojure) 30 | - [Go](#go) 31 | - [HTML](#html) 32 | - [Java](#java) 33 | - [Javascript](#javascript) 34 | - [Lua](#lua) 35 | - [Python](#python) 36 | - [TeX](#tex) 37 | - [VimL](#viml) 38 | 39 | ## 以功能区分 40 | 41 | #### 文本对齐 42 | 43 | - [tabular](https://github.com/godlygeek/tabular) 44 | - [vim-easy-align](https://github.com/junegunn/vim-easy-align) 45 | 46 | #### 构建和可疑代码标记 47 | 48 | - [ale](https://github.com/w0rp/ale) 49 | - [neomake](https://github.com/neomake/neomake) 50 | - [syntastic](https://github.com/vim-syntastic/syntastic) 51 | 52 | #### 代码补全 53 | 54 | - [VimCompletesMe](https://github.com/ajh17/VimCompletesMe) 55 | - [YouCompleteMe](https://github.com/Valloric/YouCompleteMe) 56 | - [completor.vim](https://github.com/maralla/completor.vim) 57 | - [deoplete.nvim](https://github.com/Shougo/deoplete.nvim) 58 | - [neocomplete.vim](https://github.com/Shougo/neocomplete.vim) 59 | - [supertab](https://github.com/ervandew/supertab) 60 | - [vim-mucomplete](https://github.com/lifepillar/vim-mucomplete) 61 | 62 | #### Cycle 63 | 64 | - [switch.vim](https://github.com/AndrewRadev/switch.vim) 65 | - [vim-speeddating](https://github.com/tpope/vim-speeddating) 66 | 67 | #### 注释工具 68 | 69 | - [nerdcommenter](https://github.com/scrooloose/nerdcommenter) 70 | - [tcomment_vim](https://github.com/tomtom/tcomment_vim) 71 | - [vim-commentary](https://github.com/tpope/vim-commentary) 72 | 73 | #### 分割符 74 | 75 | - [auto-pairs](https://github.com/jiangmiao/auto-pairs) 76 | - [delimitMate](https://github.com/Raimondi/delimitMate) 77 | - [vim-endwise](https://github.com/tpope/vim-endwise) 78 | 79 | #### 模糊搜索 80 | 81 | - [Command-T](https://github.com/wincent/Command-T) (_requires +ruby_) 82 | - [ctrlp.vim](https://github.com/ctrlpvim/ctrlp.vim) 83 | - [denite.nvim](https://github.com/Shougo/denite.nvim) (_requires +python3_) 84 | - [fzf](https://github.com/junegunn/fzf) 85 | - [unite.vim](https://github.com/Shougo/unite.vim) 86 | 87 | #### Grep 工具 88 | 89 | - [ctrlsf.vim](https://github.com/dyng/ctrlsf.vim) 90 | - [ferret](https://github.com/wincent/ferret) 91 | - [vim-grepper](https://github.com/mhinz/vim-grepper) 92 | 93 | #### 缩进 94 | 95 | - [indentLine](https://github.com/Yggdroot/indentLine) 96 | - [vim-indent-guides](https://github.com/nathanaelkane/vim-indent-guides) 97 | 98 | #### 文件导航 99 | 100 | - [nerdtree](https://github.com/scrooloose/nerdtree) 101 | - [tagbar](https://github.com/majutsushi/tagbar) 102 | - [vim-dirvish](https://github.com/justinmk/vim-dirvish) 103 | - [vim-easymotion](https://github.com/easymotion/vim-easymotion) 104 | - [vim-sneak](https://github.com/justinmk/vim-sneak) 105 | - [vim-vinegar](https://github.com/tpope/vim-vinegar) 106 | - [vimfiler.vim](https://github.com/Shougo/vimfiler.vim) (_depends on other plugins_) 107 | 108 | 见[fuzzy finders](#fuzzy-finders). 109 | 110 | #### 代码片段 111 | 112 | - [neosnippet.vim](https://github.com/Shougo/neosnippet.vim) (_depends on other plugins_) 113 | - [ultisnips](https://github.com/SirVer/ultisnips) 114 | - [vim-snipmate](https://github.com/garbas/vim-snipmate) (_depends on other plugins_) 115 | - [xptemplate](https://github.com/drmingdrmer/xptemplate) 116 | 117 | #### 状态栏 118 | 119 | - [lightline.vim](https://github.com/itchyny/lightline.vim) 120 | - [powerline](https://github.com/powerline/powerline) 121 | - [vim-airline](https://github.com/vim-airline/vim-airline) 122 | - [vim-flagship](https://github.com/tpope/vim-flagship) 123 | 124 | #### 文字环绕符 125 | 126 | - [vim-operator-surround](https://github.com/rhysd/vim-operator-surround) 127 | - [vim-sandwich](https://github.com/machakann/vim-sandwich) 128 | - [vim-surround](https://github.com/tpope/vim-surround) 129 | 130 | #### 记笔记 131 | 132 | - [vim-dotoo](https://github.com/dhruvasagar/vim-dotoo) 133 | - [vim-journal](https://github.com/junegunn/vim-journal) 134 | - [vim-notes](https://github.com/xolox/vim-notes) 135 | - [vim-orgmode](https://github.com/jceb/vim-orgmode) 136 | - [vim-pad](https://github.com/fmoralesc/vim-pad) 137 | - [vimwiki](https://github.com/vimwiki/vimwiki) 138 | 139 | #### 文本对象 140 | 141 | - [targets.vim](https://github.com/wellle/targets.vim) 142 | - [vim-exchange](https://github.com/tommcdo/vim-exchange) 143 | - [vim-textobj-user](https://github.com/kana/vim-textobj-user) 144 | 145 | #### Tmux 146 | 147 | - [tmux-complete.vim](https://github.com/wellle/tmux-complete.vim) 148 | - [vim-dispatch](https://github.com/tpope/vim-dispatch) 149 | - [vim-tmux-navigator](https://github.com/christoomey/vim-tmux-navigator) 150 | - [vitality.vim](https://github.com/sjl/vitality.vim) 151 | 152 | #### 撤销历史 153 | 154 | - [gundo.vim](https://github.com/sjl/gundo.vim) 155 | - [undotree](https://github.com/mbbill/undotree) 156 | 157 | #### 版本控制 158 | 159 | - [agit.vim](https://github.com/cohama/agit.vim) 160 | - [committia.vim](https://github.com/rhysd/committia.vim) 161 | - [gist-vim](https://github.com/mattn/gist-vim) 162 | - [github-issues.vim](https://github.com/jaxbot/github-issues.vim) 163 | - [gitv](https://github.com/gregsexton/gitv) 164 | - [gv.vim](https://github.com/junegunn/gv.vim) 165 | - [nerdtree-git-plugin](https://github.com/Xuyuanp/nerdtree-git-plugin) 166 | - [vim-fugitive](https://github.com/tpope/vim-fugitive) 167 | - [vim-gitgutter](https://github.com/airblade/vim-gitgutter) 168 | - [vim-github-dashboard](https://github.com/junegunn/vim-github-dashboard) 169 | - [vim-lawrencium](https://bitbucket.org/ludovicchabant/vim-lawrencium) 170 | - [vim-signify](https://github.com/mhinz/vim-signify) 171 | - [vimagit](https://github.com/jreybert/vimagit) 172 | 173 | #### 写作工具 174 | 175 | - [vim-grammarous](https://github.com/rhysd/vim-grammarous) 176 | - [vim-online-thesaurus](https://github.com/beloglazov/vim-online-thesaurus) 177 | 178 | #### 其他 179 | 180 | - [CoVim](https://github.com/FredKSchott/CoVim) 181 | - [FastFold](https://github.com/Konfekt/FastFold) 182 | - [NrrwRgn](https://github.com/chrisbra/NrrwRgn) 183 | - [calendar.vim](https://github.com/itchyny/calendar.vim) 184 | - [goyo.vim](https://github.com/junegunn/goyo.vim) 185 | - [sideways.vim](https://github.com/AndrewRadev/sideways.vim) 186 | - [splitjoin.vim](https://github.com/AndrewRadev/splitjoin.vim) 187 | - [targets.vim](https://github.com/wellle/targets.vim) 188 | - [unicode.vim](https://github.com/chrisbra/unicode.vim) 189 | - [vim-bracketed-paste](https://github.com/ConradIrwin/vim-bracketed-paste) 190 | - [vim-devicons](https://github.com/ryanoasis/vim-devicons) 191 | - [vim-diminactive](https://github.com/blueyed/vim-diminactive) 192 | - [vim-fixkey](https://github.com/drmikehenry/vim-fixkey) 193 | - [vim-gnupg](https://github.com/jamessan/vim-gnupg) 194 | - [vim-hackernews](https://github.com/ryanss/vim-hackernews) 195 | - [vim-move](https://github.com/matze/vim-move) 196 | - [vim-multiple-cursors](https://github.com/terryma/vim-multiple-cursors) 197 | - [vim-projectionist](https://github.com/tpope/vim-projectionist) 198 | - [vim-rsi](https://github.com/tpope/vim-rsi) 199 | - [vim-startify](https://github.com/mhinz/vim-startify) 200 | - [vim-unimpaired](https://github.com/tpope/vim-unimpaired) 201 | 202 | ## 以文件类型区分 203 | 204 | #### C and C++ 205 | 206 | - [a.vim](https://github.com/vim-scripts/a.vim) 207 | - [clang_complete](https://github.com/Rip-Rip/clang_complete) 208 | - [color_coded](https://github.com/jeaye/color_coded) 209 | - [lh-cpp](https://github.com/LucHermitte/lh-cpp) 210 | 211 | #### Clojure 212 | 213 | - [paredit](https://github.com/kovisoft/paredit) 214 | - [rainbow_parentheses.vim](https://github.com/junegunn/rainbow_parentheses.vim) 215 | - [vim-clojure-highlight](https://github.com/guns/vim-clojure-highlight) 216 | - [vim-fireplace](https://github.com/tpope/vim-fireplace) 217 | - [vim-salve](https://github.com/tpope/vim-salve) 218 | - [vim-sexp-mappings-for-regular-people](https://github.com/tpope/vim-sexp-mappings-for-regular-people) 219 | - [vim-sexp](https://github.com/guns/vim-sexp) 220 | 221 | #### HTML 222 | 223 | - [emmet-vim](https://github.com/mattn/emmet-vim) 224 | - [html5.vim](https://github.com/othree/html5.vim) 225 | 226 | #### Go 227 | 228 | - [gofmt.vim](https://github.com/tweekmonster/gofmt.vim) 229 | - [hl-goimport.vim](https://github.com/tweekmonster/hl-goimport.vim) 230 | - [vim-go](https://github.com/fatih/vim-go) 231 | - [vim-godebug](https://github.com/jodosha/vim-godebug) 232 | 233 | #### Java 234 | 235 | - [vim-javacomplete2](https://github.com/artur-shaik/vim-javacomplete2) 236 | 237 | #### Javascript 238 | 239 | - [es.next.syntax.vim](https://github.com/othree/es.next.syntax.vim) 240 | - [javascript-libraries-syntax.vim](https://github.com/othree/javascript-libraries-syntax.vim) 241 | - [node-vim-debugger](https://github.com/sidorares/node-vim-debugger) 242 | - [tern_for_vim](https://github.com/ternjs/tern_for_vim) 243 | - [vim-esformatter](https://github.com/millermedeiros/vim-esformatter) 244 | - [vim-javascript-syntax](https://github.com/jelera/vim-javascript-syntax) 245 | - [vim-javascript](https://github.com/pangloss/vim-javascript) 246 | - [vim-node](https://github.com/moll/vim-node) 247 | - [yajs.vim](https://github.com/othree/yajs.vim) 248 | 249 | #### Lua 250 | 251 | - [vim-lua-ftplugin](https://github.com/xolox/vim-lua-ftplugin) 252 | - [vim-lua-inspect](https://github.com/xolox/vim-lua-inspect) 253 | 254 | #### Python 255 | 256 | - [braceless.vim](https://github.com/tweekmonster/braceless.vim) 257 | - [impsort.vim](https://github.com/tweekmonster/impsort.vim) 258 | - [jedi-vim](https://github.com/davidhalter/jedi-vim) 259 | - [python-mode](https://github.com/klen/python-mode) 260 | - [vim-flake8](https://github.com/nvie/vim-flake8) 261 | 262 | #### TeX 263 | 264 | - [vimtex](https://github.com/lervag/vimtex) 265 | 266 | #### VimL 267 | 268 | - [vim-scriptease](https://github.com/tpope/vim-scriptease) 269 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 有兴趣加入本项目的话,请关注下面的认领列表,选择相应的章节翻译,或者协助校验正在翻译的项目。如果有兴趣加入,可以fork本项目,开始翻译后提交PR,便于我们协助校验,不必翻译结束后才提交PR. 2 | 3 | ## 认领方式 4 | 5 | **开issue** 6 | 7 | ```md 8 | 章节名称:***** 9 | 译者:****** 10 | PR: PR的链接 11 | ``` 12 | 13 | ## 提交方式 14 | 15 | 每个章节独立翻译,并提交至 [chapter/](chapter/) 文件夹,文件名为英文标题小写,以`-`链接单词。例如翻译`What is Vim?`章节, 文件名即为`chapter/what-is-vim.md`. 合并到主仓库后,将定期将这些章节文件合并到`README.dm`。 16 | 17 | ## 章节列表 18 | 19 | 基于[mhinz/vim-galore@d50d48b](https://github.com/mhinz/vim-galore/tree/d50d48bce40bbbd99a0528a2893b00cf54a6f4b9), 后期如果有改动,定期在更新。 20 | 21 | |章节名称|认领状态|PR链接| 22 | |:------|:------|:------| 23 | | [Intro](https://github.com/mhinz/vim-galore#intro) | [wsdjeg] | [#4] | 24 | | [What is Vim?](https://github.com/mhinz/vim-galore#what-is-vim) | [wsdjeg] | [#4] | 25 | | [The Vim Philosophy](https://github.com/mhinz/vim-galore#the-vim-philosophy) | [wsdjeg] | [#4] | 26 | | [First steps](https://github.com/mhinz/vim-galore#first-steps) | [wsdjeg] | [#4] | 27 | | [Minimal vimrc](https://github.com/mhinz/vim-galore#minimal-vimrc) | [wsdjeg] | [#4] | 28 | | [What kind of Vim am I running?](https://github.com/mhinz/vim-galore#what-kind-of-vim-am-i-running) | [wsdjeg] | [#4] | 29 | | [Cheatsheets](https://github.com/mhinz/vim-galore#cheatsheets) | [wsdjeg] | [#4] | 30 | | [Basics](https://github.com/mhinz/vim-galore#basics-1) | [wsdjeg] | [#4] | 31 | | [Buffers, windows, tabs](https://github.com/mhinz/vim-galore#buffers-windows-tabs) | [wsdjeg] | [#4] | 32 | | [Active, loaded, listed, named buffers](https://github.com/mhinz/vim-galore#active-loaded-listed-named-buffers) | [wsdjeg] | [#4] | 33 | | [Argument list](https://github.com/mhinz/vim-galore#argument-list) | [wsdjeg] | [#4] | 34 | | [Mappings](https://github.com/mhinz/vim-galore#mappings) | [wsdjeg] | [#4] | 35 | | [Mapleader](https://github.com/mhinz/vim-galore#mapleader) | [星星丶S1nG] | [#7] | 36 | | [Registers](https://github.com/mhinz/vim-galore#registers) | [星星丶S1nG] | [#7] | 37 | | [Ranges](https://github.com/mhinz/vim-galore#ranges) | [星星丶S1nG] | [#7] | 38 | | [Marks](https://github.com/mhinz/vim-galore#marks) | [星星丶S1nG] | [#7] | 39 | | [Completion](https://github.com/mhinz/vim-galore#completion) | [星星丶S1nG] | [#7] | 40 | | [Motions, operators, text objects](https://github.com/mhinz/vim-galore#motions-operators-text-objects) | [星星丶S1nG] | [#7] | 41 | | [Autocmds](https://github.com/mhinz/vim-galore#autocmds) | [星星丶S1nG] | [#7] | 42 | | [Changelist, jumplist](https://github.com/mhinz/vim-galore#changelist-jumplist) | [星星丶S1nG] | [#7] | 43 | | [Undo tree](https://github.com/mhinz/vim-galore#undo-tree) | [星星丶S1nG] | [#7] | 44 | | [Quickfix and location lists](https://github.com/mhinz/vim-galore#quickfix-and-location-lists) | [星星丶S1nG] | [#7] | 45 | | [Macros](https://github.com/mhinz/vim-galore#macros) | [星星丶S1nG] | [#7] | 46 | | [Colorschemes](https://github.com/mhinz/vim-galore#colorschemes) | [星星丶S1nG] | [#7] | 47 | | [Folding](https://github.com/mhinz/vim-galore#folding) | [星星丶S1nG] | [#7] | 48 | | [Sessions](https://github.com/mhinz/vim-galore#sessions) | [星星丶S1nG] | [#7] | 49 | | [Locality](https://github.com/mhinz/vim-galore#locality) | [星星丶S1nG] | [#7] | 50 | | [Usage](https://github.com/mhinz/vim-galore#usage-1) | [hotleave] | [#13] | 51 | | [Getting help offline](https://github.com/mhinz/vim-galore#getting-help-offline) | [hotleave] | [#13] | 52 | | [Getting help offline (alternative)](https://github.com/mhinz/vim-galore#getting-help-offline-alternative) | [hotleave] | [#13] | 53 | | [Getting help online](https://github.com/mhinz/vim-galore#getting-help-online) | [hotleave] | [#13] | 54 | | [Autocmds in practice](https://github.com/mhinz/vim-galore#autocmds-in-practice) | [hotleave] | [#13] | 55 | | [User events](https://github.com/mhinz/vim-galore#user-events) | [hotleave] | [#13] | 56 | | [Nested autocmds](https://github.com/mhinz/vim-galore#nested-autocmds) | [hotleave] | [#13] | 57 | | [Clipboard](https://github.com/mhinz/vim-galore#clipboard) | [hotleave] | [#13] | 58 | | [Clipboard usage (Windows, OSX)](https://github.com/mhinz/vim-galore#clipboard-usage-windows-osx) | [hotleave] | [#13] | 59 | | [Clipboard usage (Linux, BSD, ...)](https://github.com/mhinz/vim-galore#clipboard-usage-linux-bsd-) | [hotleave] | [#13] | 60 | | [Restore cursor position when opening file](https://github.com/mhinz/vim-galore#restore-cursor-position-when-opening-file) | [hotleave] | [#13] | 61 | | [Handling backup, swap, undo, and viminfo files](https://github.com/mhinz/vim-galore#handling-backup-swap-undo-and-viminfo-files) | [hotleave] | [#13] | 62 | | [Editing remote files](https://github.com/mhinz/vim-galore#editing-remote-files) | [hotleave] | [#13] | 63 | | [Managing plugins](https://github.com/mhinz/vim-galore#managing-plugins) | [hotleave] | [#13] | 64 | | [Block insert](https://github.com/mhinz/vim-galore#block-insert) | [hotleave] | [#13] | 65 | | [Running external programs and using filters](https://github.com/mhinz/vim-galore#running-external-programs-and-using-filters) | [hotleave] | [#13] | 66 | | [Cscope](https://github.com/mhinz/vim-galore#cscope) | [hotleave] | [#13] | 67 | | [MatchIt](https://github.com/mhinz/vim-galore#matchit) | [hotleave] | [#13] | 68 | | [Tips](https://github.com/mhinz/vim-galore#tips-1) | [星星丶S1nG] | [#15] | 69 | | [Saner behavior of n and N](https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n) | [星星丶S1nG] | [#15] | 70 | | [Saner command-line history](https://github.com/mhinz/vim-galore#saner-command-line-history) | [星星丶S1nG] | [#15] | 71 | | [Saner CTRL-L](https://github.com/mhinz/vim-galore#saner-ctrl-l) | [星星丶S1nG] | [#15] | 72 | | [Disable audible and visual bells](https://github.com/mhinz/vim-galore#disable-audible-and-visual-bells) | [星星丶S1nG] | [#15] | 73 | | [Quickly move current line](https://github.com/mhinz/vim-galore#quickly-move-current-line) | [星星丶S1nG] | [#15] | 74 | | [Quickly add empty lines](https://github.com/mhinz/vim-galore#quickly-add-empty-lines) | [星星丶S1nG] | [#15] | 75 | | [Quickly edit your macros](https://github.com/mhinz/vim-galore#quickly-edit-your-macros) | [星星丶S1nG] | [#15] | 76 | | [Quickly jump to header or source file](https://github.com/mhinz/vim-galore#quickly-jump-to-header-or-source-file) | [星星丶S1nG] | [#15] | 77 | | [Quickly change font size in GUI](https://github.com/mhinz/vim-galore#quickly-change-font-size-in-gui) | [星星丶S1nG] | [#15] | 78 | | [Change cursor style dependent on mode](https://github.com/mhinz/vim-galore#change-cursor-style-dependent-on-mode) | [星星丶S1nG] | [#15] | 79 | | [Don't lose selection when shifting sidewards](https://github.com/mhinz/vim-galore#dont-lose-selection-when-shifting-sidewards) | [星星丶S1nG] | [#15] | 80 | | [Reload a file on saving](https://github.com/mhinz/vim-galore#reload-a-file-on-saving) | [星星丶S1nG] | [#15] | 81 | | [Smarter cursorline](https://github.com/mhinz/vim-galore#smarter-cursorline) | [星星丶S1nG] | [#15] | 82 | | [Faster keyword completion](https://github.com/mhinz/vim-galore#faster-keyword-completion) | [星星丶S1nG] | [#15] | 83 | | [Cosmetic changes to colorschemes](https://github.com/mhinz/vim-galore#cosmetic-changes-to-colorschemes) | [星星丶S1nG] | [#15] | 84 | | [Commands](https://github.com/mhinz/vim-galore#commands-1) | [hotleave] | [#18] | 85 | | [:global and :vglobal](https://github.com/mhinz/vim-galore#global-and-vglobal) - Execute a command on all matching lines. | [hotleave] | [#18] | 86 | | [:normal and :execute](https://github.com/mhinz/vim-galore#normal-and-execute) - The scripting dream team. | [hotleave] | [#18] | 87 | | [:redir](https://github.com/mhinz/vim-galore#redir) - Redirect messages. | [hotleave] | [#18] | 88 | | [Debugging](https://github.com/mhinz/vim-galore#debugging-1) | YDWX | | 89 | | [General tips](https://github.com/mhinz/vim-galore#general-tips) | YDWX | | 90 | | [Verbosity](https://github.com/mhinz/vim-galore#verbosity) | YDWX | | 91 | | [Profiling startup time](https://github.com/mhinz/vim-galore#profiling-startup-time) | YDWX | | 92 | | [Profiling at runtime](https://github.com/mhinz/vim-galore#profiling-at-runtime) | YDWX | | 93 | | [Debugging Vim scripts](https://github.com/mhinz/vim-galore#debugging-vim-scripts) | | | 94 | | [Debugging syntax files](https://github.com/mhinz/vim-galore#debugging-syntax-files) | | | 95 | | [Miscellaneous](https://github.com/mhinz/vim-galore#miscellaneous-1) | #11 | | 96 | | [Additional resources](https://github.com/mhinz/vim-galore#additional-resources) | #11 | | 97 | | [Vim distributions](https://github.com/mhinz/vim-galore#vim-distributions) | #11 | | 98 | | [Standard plugins](https://github.com/mhinz/vim-galore#standard-plugins) | #11 | | 99 | | [Map CapsLock to Control](https://github.com/mhinz/vim-galore#map-capslock-to-control) | #11 | | 100 | | [Easter eggs](https://github.com/mhinz/vim-galore#easter-eggs) | #11 | | 101 | | [Why hjkl for navigation?](https://github.com/mhinz/vim-galore#why-hjkl-for-navigation) | #11 | | 102 | | [Common problems](https://github.com/mhinz/vim-galore#common-problems-1) | [hotleave] | [#18] | 103 | | [Editing small files is slow](https://github.com/mhinz/vim-galore#editing-small-files-is-slow) | [hotleave] | [#18] | 104 | | [Editing huge files is slow](https://github.com/mhinz/vim-galore#editing-huge-files-is-slow) | [hotleave] | [#18] | 105 | | [Bracketed paste (or why do I have to set 'paste' all the time?)](https://github.com/mhinz/vim-galore#bracketed-paste-or-why-do-i-have-to-set-paste-all-the-time) | [hotleave] | [#18] | 106 | | [Delays when using escape key in terminal](https://github.com/mhinz/vim-galore#delays-when-using-escape-key-in-terminal) | [hotleave] | [#18] | 107 | | [Function search undo](https://github.com/mhinz/vim-galore#function-search-undo) | [hotleave] | [#18] | 108 | | [Technical quirks](https://github.com/mhinz/vim-galore#technical-quirks-1) | | [#18] | 109 | | [Newline used for NUL](https://github.com/mhinz/vim-galore#newline-used-for-nul) | | | 110 | | [List of colorschemes](https://github.com/mhinz/vim-galore#list-of-colorschemes-1) | | | 111 | | [List of plugins](https://github.com/mhinz/vim-galorecontents/plugins.md) | | | 112 | 113 | 114 | 115 | [#4]: https://github.com/wsdjeg/vim-galore-zh_cn/pull/4 116 | [#7]: https://github.com/wsdjeg/vim-galore-zh_cn/pull/7 117 | [#13]: https://github.com/wsdjeg/vim-galore-zh_cn/pull/13 118 | [#15]: https://github.com/wsdjeg/vim-galore-zh_cn/pull/15 119 | [#18]: https://github.com/wsdjeg/vim-galore-zh_cn/pull/18 120 | 121 | 122 | [wsdjeg]: https://github.com/wsdjeg 123 | [星星丶S1nG]: https://github.com/S1ngS1ng 124 | [Hotleave]: https://github.com/hotleave 125 | 126 | -------------------------------------------------------------------------------- /PLUGINS.md: -------------------------------------------------------------------------------- 1 | ## 插件列表 2 | 3 | 4 | 5 | 6 | - [颜色主题](#颜色主题) 7 | - [按功能分类](#按功能分类) 8 | - [文本对齐](#文本对齐) 9 | - [语法检查](#语法检查) 10 | - [代码补全](#代码补全) 11 | - [Cycle](#cycle) 12 | - [代码注释](#代码注释) 13 | - [自动补全括号](#自动补全括号) 14 | - [模糊搜索](#模糊搜索) 15 | - [文本搜索](#文本搜索) 16 | - [代码对齐线](#代码对齐线) 17 | - [Navigation](#navigation) 18 | - [插件管理](#插件管理) 19 | - [代码片段](#代码片段) 20 | - [状态栏](#状态栏) 21 | - [Surround](#surround) 22 | - [Taking notes](#taking-notes) 23 | - [Testing](#testing) 24 | - [文本对象](#文本对象) 25 | - [Tmux](#tmux) 26 | - [编辑历史管理](#编辑历史管理) 27 | - [版本控制](#版本控制) 28 | - [Writing](#writing) 29 | - [Misc](#misc) 30 | - [编程语言](#编程语言) 31 | - [C、C++](#cc) 32 | - [Clojure](#clojure) 33 | - [Elixir](#elixir) 34 | - [Go](#go) 35 | - [HTML](#html) 36 | - [Java](#java) 37 | - [Javascript](#javascript) 38 | - [Lua](#lua) 39 | - [PHP](#php) 40 | - [Python](#python) 41 | - [TeX](#tex) 42 | - [VimL](#viml) 43 | 44 | 45 | 46 | ### 颜色主题 47 | 48 | 以下为一些比较流行的颜色主题: 49 | 50 | - [acme-colors](https://github.com/plan9-for-vimspace/acme-colors) 51 | - [apprentice](https://github.com/romainl/Apprentice) 52 | - [base16](https://github.com/chriskempson/base16-vim) 53 | - [gotham](https://github.com/whatyouhide/vim-gotham) 54 | - [gruvbox](https://github.com/morhetz/gruvbox) 55 | - [janah](https://github.com/mhinz/vim-janah) 56 | - [jellybeans](https://github.com/nanotech/jellybeans.vim) 57 | - [lucius](https://github.com/jonathanfilip/vim-lucius) 58 | - [molokai](https://github.com/tomasr/molokai) 59 | - [nofrils](https://github.com/robertmeta/nofrils) 60 | - [oceanic-next](https://github.com/mhartington/oceanic-next) 61 | - [paramount](https://github.com/owickstrom/vim-colors-paramount) 62 | - [railscasts](https://github.com/jpo/vim-railscasts-theme) 63 | - [seoul256](https://github.com/junegunn/seoul256.vim) 64 | - [solarized](https://github.com/altercation/vim-colors-solarized) (or [solarized8](https://github.com/lifepillar/vim-solarized8) or [flattened](https://github.com/romainl/flattened)) 65 | - [tomorrow](https://github.com/chriskempson/vim-tomorrow-theme) 66 | - [vividchalk](https://github.com/tpope/vim-vividchalk) 67 | - [yowish](https://github.com/kabbamine/yowish.vim) 68 | - [zenburn](https://github.com/jnurmine/Zenburn) 69 | 70 | Alternatively, generate your own colorscheme using [themer](https://github.com/mjswensen/themer) 71 | or [Colortemplate](https://github.com/lifepillar/vim-colortemplate). 72 | 73 | ### 按功能分类 74 | 75 | #### 文本对齐 76 | 77 | - [tabular](https://github.com/godlygeek/tabular) 78 | - [vim-easy-align](https://github.com/junegunn/vim-easy-align) 79 | - [vim-lion](https://github.com/tommcdo/vim-lion) 80 | 81 | #### 语法检查 82 | 83 | - [ale](https://github.com/w0rp/ale) 84 | - [neomake](https://github.com/neomake/neomake) 85 | - [syntastic](https://github.com/vim-syntastic/syntastic) 86 | 87 | #### 代码补全 88 | 89 | - [asyncomplete.vim](https://github.com/prabirshrestha/asyncomplete.vim) 90 | - [completor.vim](https://github.com/maralla/completor.vim) 91 | - [deoplete.nvim](https://github.com/Shougo/deoplete.nvim) 92 | - [neocomplete.vim](https://github.com/Shougo/neocomplete.vim) 93 | - [nvim-completion-manager](https://github.com/roxma/nvim-completion-manager) 94 | - [supertab](https://github.com/ervandew/supertab) 95 | - [vim-mucomplete](https://github.com/lifepillar/vim-mucomplete) 96 | - [VimCompletesMe](https://github.com/ajh17/VimCompletesMe) 97 | - [YouCompleteMe](https://github.com/Valloric/YouCompleteMe) 98 | 99 | #### Cycle 100 | 101 | - [switch.vim](https://github.com/AndrewRadev/switch.vim) 102 | - [vim-speeddating](https://github.com/tpope/vim-speeddating) 103 | 104 | #### 代码注释 105 | 106 | - [nerdcommenter](https://github.com/scrooloose/nerdcommenter) 107 | - [tcomment_vim](https://github.com/tomtom/tcomment_vim) 108 | - [vim-commentary](https://github.com/tpope/vim-commentary) 109 | 110 | #### 自动补全括号 111 | 112 | - [auto-pairs](https://github.com/jiangmiao/auto-pairs) 113 | - [delimitMate](https://github.com/Raimondi/delimitMate) 114 | - [vim-endwise](https://github.com/tpope/vim-endwise) 115 | 116 | #### 模糊搜索 117 | 118 | - [Command-T](https://github.com/wincent/Command-T) (_requires +ruby_) 119 | - [ctrlp.vim](https://github.com/ctrlpvim/ctrlp.vim) 120 | - [denite.nvim](https://github.com/Shougo/denite.nvim) (_requires +python3_) 121 | - [fzf](https://github.com/junegunn/fzf) (and [fzf.vim](https://github.com/junegunn/fzf.vim)) 122 | - [unite.vim](https://github.com/Shougo/unite.vim) 123 | - [vim-fz](https://github.com/mattn/vim-fz) 124 | 125 | #### 文本搜索 126 | 127 | - [ctrlsf.vim](https://github.com/dyng/ctrlsf.vim) 128 | - [ferret](https://github.com/wincent/ferret) 129 | - [vim-grepper](https://github.com/mhinz/vim-grepper) 130 | 131 | #### 代码对齐线 132 | 133 | - [indentLine](https://github.com/Yggdroot/indentLine) 134 | - [vim-indent-guides](https://github.com/nathanaelkane/vim-indent-guides) 135 | 136 | #### Navigation 137 | 138 | - [nerdtree](https://github.com/scrooloose/nerdtree) 139 | - [tagbar](https://github.com/majutsushi/tagbar) 140 | - [vim-dirvish](https://github.com/justinmk/vim-dirvish) 141 | - [vim-easymotion](https://github.com/easymotion/vim-easymotion) 142 | - [vim-sneak](https://github.com/justinmk/vim-sneak) 143 | - [vim-vinegar](https://github.com/tpope/vim-vinegar) 144 | - [vimfiler.vim](https://github.com/Shougo/vimfiler.vim) (_depends on other plugins_) 145 | 146 | Also see [fuzzy finders](#fuzzy-finders). 147 | 148 | #### 插件管理 149 | 150 | - [apt-vim](https://github.com/egalpin/apt-vim) 151 | - [dein.vim](https://github.com/Shougo/dein.vim) 152 | - [minpac](https://github.com/k-takata/minpac) 153 | - [vim-addon-manager](https://github.com/MarcWeber/vim-addon-manager) 154 | - [vim-pathogen](https://github.com/tpope/vim-pathogen) 155 | - [vim-plug](https://github.com/junegunn/vim-plug) 156 | - [vundle.vim](https://github.com/VundleVim/Vundle.vim) 157 | 158 | #### 代码片段 159 | 160 | - [neosnippet.vim](https://github.com/Shougo/neosnippet.vim) (_depends on other plugins_) 161 | - [ultisnips](https://github.com/SirVer/ultisnips) 162 | - [vim-snipmate](https://github.com/garbas/vim-snipmate) (_depends on other plugins_) 163 | - [xptemplate](https://github.com/drmingdrmer/xptemplate) 164 | 165 | #### 状态栏 166 | 167 | - [lightline.vim](https://github.com/itchyny/lightline.vim) 168 | - [powerline](https://github.com/powerline/powerline) 169 | - [vim-airline](https://github.com/vim-airline/vim-airline) 170 | - [vim-flagship](https://github.com/tpope/vim-flagship) 171 | 172 | #### Surround 173 | 174 | - [vim-operator-surround](https://github.com/rhysd/vim-operator-surround) 175 | - [vim-sandwich](https://github.com/machakann/vim-sandwich) 176 | - [vim-surround](https://github.com/tpope/vim-surround) 177 | 178 | #### Taking notes 179 | 180 | - [vim-dotoo](https://github.com/dhruvasagar/vim-dotoo) 181 | - [vim-journal](https://github.com/junegunn/vim-journal) 182 | - [vim-notes](https://github.com/xolox/vim-notes) 183 | - [vim-orgmode](https://github.com/jceb/vim-orgmode) 184 | - [vim-pad](https://github.com/fmoralesc/vim-pad) 185 | - [vimwiki](https://github.com/vimwiki/vimwiki) 186 | 187 | #### Testing 188 | 189 | - [vim-test](https://github.com/janko-m/vim-test) 190 | 191 | #### 文本对象 192 | 193 | - [targets.vim](https://github.com/wellle/targets.vim) 194 | - [vim-exchange](https://github.com/tommcdo/vim-exchange) 195 | - [vim-indent-object](https://github.com/michaeljsmith/vim-indent-object) 196 | - [vim-matchup](https://github.com/andymass/vim-matchup) 197 | - [vim-textobj-user](https://github.com/kana/vim-textobj-user) 198 | 199 | #### Tmux 200 | 201 | - [tmux-complete.vim](https://github.com/wellle/tmux-complete.vim) 202 | - [vim-dispatch](https://github.com/tpope/vim-dispatch) 203 | - [vim-tmux-navigator](https://github.com/christoomey/vim-tmux-navigator) 204 | - [vitality.vim](https://github.com/sjl/vitality.vim) 205 | 206 | #### 编辑历史管理 207 | 208 | - [vim-mundo](https://github.com/simnalamburt/vim-mundo) 209 | - [gundo.vim](https://github.com/sjl/gundo.vim) 210 | - [undotree](https://github.com/mbbill/undotree) 211 | 212 | #### 版本控制 213 | 214 | - [agit.vim](https://github.com/cohama/agit.vim) 215 | - [committia.vim](https://github.com/rhysd/committia.vim) 216 | - [gist-vim](https://github.com/mattn/gist-vim) 217 | - [github-issues.vim](https://github.com/jaxbot/github-issues.vim) 218 | - [gitv](https://github.com/gregsexton/gitv) 219 | - [gv.vim](https://github.com/junegunn/gv.vim) 220 | - [nerdtree-git-plugin](https://github.com/Xuyuanp/nerdtree-git-plugin) 221 | - [vim-auto-programming](https://github.com/haya14busa/vim-auto-programming) 222 | - [vim-fugitive](https://github.com/tpope/vim-fugitive) 223 | - [vim-gitgutter](https://github.com/airblade/vim-gitgutter) 224 | - [vim-github-dashboard](https://github.com/junegunn/vim-github-dashboard) 225 | - [vim-lawrencium](https://bitbucket.org/ludovicchabant/vim-lawrencium) 226 | - [vim-signify](https://github.com/mhinz/vim-signify) 227 | - [vimagit](https://github.com/jreybert/vimagit) 228 | - [git-messenger.vim](https://github.com/rhysd/git-messenger.vim/) 229 | 230 | #### Writing 231 | 232 | - [vim-grammarous](https://github.com/rhysd/vim-grammarous) 233 | - [vim-online-thesaurus](https://github.com/beloglazov/vim-online-thesaurus) 234 | 235 | #### Misc 236 | 237 | - [calendar.vim](https://github.com/itchyny/calendar.vim) 238 | - [CoVim](https://github.com/FredKSchott/CoVim) 239 | - [FastFold](https://github.com/Konfekt/FastFold) 240 | - [goyo.vim](https://github.com/junegunn/goyo.vim) 241 | - [is.vim](https://github.com/haya14busa/is.vim) 242 | - [NrrwRgn](https://github.com/chrisbra/NrrwRgn) 243 | - [sideways.vim](https://github.com/AndrewRadev/sideways.vim) 244 | - [splitjoin.vim](https://github.com/AndrewRadev/splitjoin.vim) 245 | - [unicode.vim](https://github.com/chrisbra/unicode.vim) 246 | - [vim-abolish](https://github.com/tpope/vim-abolish) 247 | - [vim-bracketed-paste](https://github.com/ConradIrwin/vim-bracketed-paste) 248 | - [vim-devicons](https://github.com/ryanoasis/vim-devicons) 249 | - [vim-diff-enhanced](https://github.com/chrisbra/vim-diff-enhanced) 250 | - [vim-diminactive](https://github.com/blueyed/vim-diminactive) 251 | - [vim-fixkey](https://github.com/drmikehenry/vim-fixkey) 252 | - [vim-gnupg](https://github.com/jamessan/vim-gnupg) 253 | - [vim-gutentags](https://github.com/ludovicchabant/vim-gutentags) 254 | - [vim-hackernews](https://github.com/ryanss/vim-hackernews) 255 | - [vim-move](https://github.com/matze/vim-move) 256 | - [vim-multiple-cursors](https://github.com/terryma/vim-multiple-cursors) 257 | - [vim-projectionist](https://github.com/tpope/vim-projectionist) 258 | - [vim-qf](https://github.com/romainl/vim-qf) 259 | - [vim-rsi](https://github.com/tpope/vim-rsi) 260 | - [vim-sleuth](https://github.com/tpope/vim-sleuth) 261 | - [vim-startify](https://github.com/mhinz/vim-startify) 262 | - [vim-unimpaired](https://github.com/tpope/vim-unimpaired) 263 | 264 | ### 编程语言 265 | 266 | #### C、C++ 267 | 268 | - [a.vim](https://github.com/vim-scripts/a.vim) 269 | - [clang_complete](https://github.com/Rip-Rip/clang_complete) 270 | - [color_coded](https://github.com/jeaye/color_coded) 271 | - [lh-cpp](https://github.com/LucHermitte/lh-cpp) 272 | - [vim-cpp-enhanced-highlight](https://github.com/octol/vim-cpp-enhanced-highlight) 273 | 274 | #### Clojure 275 | 276 | - [paredit](https://github.com/kovisoft/paredit) 277 | - [rainbow_parentheses.vim](https://github.com/junegunn/rainbow_parentheses.vim) 278 | - [vim-clojure-highlight](https://github.com/guns/vim-clojure-highlight) 279 | - [vim-fireplace](https://github.com/tpope/vim-fireplace) 280 | - [vim-salve](https://github.com/tpope/vim-salve) 281 | - [vim-sexp-mappings-for-regular-people](https://github.com/tpope/vim-sexp-mappings-for-regular-people) 282 | - [vim-sexp](https://github.com/guns/vim-sexp) 283 | 284 | #### Elixir 285 | 286 | - [alchemist.vim](https://github.com/slashmili/alchemist.vim) 287 | - [vim-elixir](https://github.com/elixir-editors/vim-elixir) 288 | - [vim-mix-format](https://github.com/mhinz/vim-mix-format) 289 | 290 | #### Go 291 | 292 | - [gofmt.vim](https://github.com/tweekmonster/gofmt.vim) 293 | - [hl-goimport.vim](https://github.com/tweekmonster/hl-goimport.vim) 294 | - [vim-go](https://github.com/fatih/vim-go) 295 | - [vim-godebug](https://github.com/jodosha/vim-godebug) 296 | 297 | #### HTML 298 | 299 | - [emmet-vim](https://github.com/mattn/emmet-vim) 300 | - [html5.vim](https://github.com/othree/html5.vim) 301 | 302 | #### Java 303 | 304 | - [vim-javacomplete2](https://github.com/artur-shaik/vim-javacomplete2) 305 | 306 | #### Javascript 307 | 308 | - [es.next.syntax.vim](https://github.com/othree/es.next.syntax.vim) 309 | - [javascript-libraries-syntax.vim](https://github.com/othree/javascript-libraries-syntax.vim) 310 | - [node-vim-debugger](https://github.com/sidorares/node-vim-debugger) 311 | - [tern_for_vim](https://github.com/ternjs/tern_for_vim) 312 | - [vim-esformatter](https://github.com/millermedeiros/vim-esformatter) 313 | - [vim-flow](https://github.com/flowtype/vim-flow) 314 | - [vim-javascript-syntax](https://github.com/jelera/vim-javascript-syntax) 315 | - [vim-javascript](https://github.com/pangloss/vim-javascript) 316 | - [vim-node](https://github.com/moll/vim-node) 317 | - [vim-prettier](https://github.com/prettier/vim-prettier) 318 | - [yajs.vim](https://github.com/othree/yajs.vim) 319 | - [yats.vim](https://github.com/HerringtonDarkholme/yats.vim) 320 | 321 | #### Lua 322 | 323 | - [vim-lua-ftplugin](https://github.com/xolox/vim-lua-ftplugin) 324 | - [vim-lua-inspect](https://github.com/xolox/vim-lua-inspect) 325 | 326 | #### PHP 327 | 328 | - [php.vim](https://github.com/StanAngeloff/php.vim) 329 | - [vim-php-cs-fixer](https://github.com/stephpy/vim-php-cs-fixer) 330 | - [vim-php-namespace](https://github.com/arnaud-lb/vim-php-namespace) 331 | - [vim-php-refactoring-toolbox](https://github.com/adoy/vim-php-refactoring-toolbox) 332 | 333 | #### Python 334 | 335 | - [braceless.vim](https://github.com/tweekmonster/braceless.vim) 336 | - [impsort.vim](https://github.com/tweekmonster/impsort.vim) 337 | - [jedi-vim](https://github.com/davidhalter/jedi-vim) 338 | - [python-mode](https://github.com/klen/python-mode) 339 | - [SimpylFold](https://github.com/tmhedberg/SimpylFold) 340 | - [vim-flake8](https://github.com/nvie/vim-flake8) 341 | 342 | #### TeX 343 | 344 | - [vimtex](https://github.com/lervag/vimtex) 345 | 346 | #### VimL 347 | 348 | - [exception.vim](https://github.com/tweekmonster/exception.vim) 349 | - [helpful.vim](https://github.com/tweekmonster/helpful.vim) 350 | - [vader.vim](https://github.com/junegunn/vader.vim) 351 | - [vim-lookup](https://github.com/mhinz/vim-lookup) 352 | - [vim-scriptease](https://github.com/tpope/vim-scriptease) 353 | - [vim-themis](https://github.com/thinca/vim-themis) 354 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vim 从入门到精通 2 | 3 | > 本文主要在翻译 [mhinz/vim-galore](https://github.com/mhinz/vim-galore) 4 | > 的基础添加了一些我在使用 Vim 及开发 Vim 插件的过程中积累的一些知识和常用插件列表。 5 | 6 | **Vim 中文同步聊天室** 7 | 8 | - telegram: [@VimHub](https://t.me/VimHub) 9 | - gitter: [vim-china/Lobby](https://gitter.im/vim-china/Lobby) 10 | - IRC: [#vim-china](https://webchat.freenode.net/?channels=vim-china) 11 | 12 | 13 | 14 | - [简介](#简介) 15 | - [什么是 Vim?](#什么是-vim) 16 | - [Vim 哲学](#vim-哲学) 17 | - [入门](#入门) 18 | - [精简的 vimrc](#精简的-vimrc) 19 | - [我正在使用什么样的 Vim](#我正在使用什么样的-vim) 20 | - [备忘录](#备忘录) 21 | - [基础](#基础) 22 | - [缓冲区,窗口,标签](#缓冲区窗口标签) 23 | - [已激活、已载入、已列出、已命名的缓冲区](#已激活已载入已列出已命名的缓冲区) 24 | - [参数列表](#参数列表) 25 | - [按键映射](#按键映射) 26 | - [映射前置键](#映射前置键) 27 | - [寄存器](#寄存器) 28 | - [范围](#范围) 29 | - [标注](#标注) 30 | - [补全](#补全) 31 | - [动作,操作符,文本对象](#动作操作符文本对象) 32 | - [自动命令](#自动命令) 33 | - [变更历史,跳转历史](#变更历史跳转历史) 34 | - [内容变更历史记录](#内容变更历史记录) 35 | - [全局位置信息表,局部位置信息表](#全局位置信息表局部位置信息表) 36 | - [宏](#宏) 37 | - [颜色主题](#颜色主题) 38 | - [折叠](#折叠) 39 | - [会话](#会话) 40 | - [局部化](#局部化) 41 | - [用法](#用法) 42 | - [获取离线帮助](#获取离线帮助) 43 | - [获取离线帮助(补充)](#获取离线帮助补充) 44 | - [获取在线帮助](#获取在线帮助) 45 | - [执行自动命令](#执行自动命令) 46 | - [用户自定义事件](#用户自定义事件) 47 | - [事件嵌套](#事件嵌套) 48 | - [剪切板](#剪切板) 49 | - [剪贴板的使用(Windows, OSX)](#剪贴板的使用windows-osx) 50 | - [剪贴板的使用(Linux, BSD, ...)](#剪贴板的使用linux-bsd-) 51 | - [打开文件时恢复光标位置](#打开文件时恢复光标位置) 52 | - [临时文件](#临时文件) 53 | - [备份文件](#备份文件) 54 | - [交换文件](#交换文件) 55 | - [撤销文件](#撤销文件) 56 | - [viminfo 文件](#viminfo-文件) 57 | - [临时文件管理设置示例](#临时文件管理设置示例) 58 | - [编辑远程文件](#编辑远程文件) 59 | - [插件管理](#插件管理) 60 | - [多行编辑](#多行编辑) 61 | - [使用外部程序和过滤器](#使用外部程序和过滤器) 62 | - [Cscope](#cscope) 63 | - [1. 构建数据库](#1-构建数据库) 64 | - [2. 添加数据库](#2-添加数据库) 65 | - [3. 查询数据库](#3-查询数据库) 66 | - [MatchIt](#matchit) 67 | - [在 Vim 8 中安装](#在-vim-8-中安装) 68 | - [在 Vim 7 或者更早的版本中安装](#在-vim-7-或者更早的版本中安装) 69 | - [简短的介绍](#简短的介绍) 70 | - [技巧](#技巧) 71 | - [聪明地使用 n 和 N](#聪明地使用-n-和-n) 72 | - [聪明地使用命令行历史](#聪明地使用命令行历史) 73 | - [智能 Ctrl-l](#智能-ctrl-l) 74 | - [禁用错误报警声音和图标](#禁用错误报警声音和图标) 75 | - [快速移动当前行](#快速移动当前行) 76 | - [快速添加空行](#快速添加空行) 77 | - [运行时检测](#运行时检测) 78 | - [查看启动时间](#查看启动时间) 79 | - [NUL 符用新行表示](#nul-符用新行表示) 80 | - [快速编辑自定义宏](#快速编辑自定义宏) 81 | - [快速跳转到源(头)文件](#快速跳转到源头文件) 82 | - [在 GUI 中快速改变字体大小](#在-gui-中快速改变字体大小) 83 | - [根据模式改变光标类型](#根据模式改变光标类型) 84 | - [防止水平滑动的时候失去选择](#防止水平滑动的时候失去选择) 85 | - [重新载入保存文件](#重新载入保存文件) 86 | - [更加智能的当前行高亮](#更加智能的当前行高亮) 87 | - [更快的关键字补全](#更快的关键字补全) 88 | - [改变颜色主题的默认外观](#改变颜色主题的默认外观) 89 | - [命令](#命令) 90 | - [:global 和 :vglobal - 在所有匹配行执行命令](#global-和-vglobal---在所有匹配行执行命令) 91 | - [:normal 和 :execute - 脚本梦之队](#normal-和-execute---脚本梦之队) 92 | - [重定向消息](#重定向消息) 93 | - [调试](#调试) 94 | - [常规建议](#常规建议) 95 | - [调整日志等级](#调整日志等级) 96 | - [查看启动日志](#查看启动日志) 97 | - [查看运行时日志](#查看运行时日志) 98 | - [Vim 脚本调试](#vim-脚本调试) 99 | - [语法文件调试](#语法文件调试) 100 | - [杂项](#杂项) 101 | - [附加资源](#附加资源) 102 | - [Vim 配置集合](#vim-配置集合) 103 | - [常见问题](#常见问题) 104 | - [编辑小文件时很慢](#编辑小文件时很慢) 105 | - [编辑大文件的时候很慢](#编辑大文件的时候很慢) 106 | - [持续粘贴(为什么我每次都要设置 'paste' 模式)](#持续粘贴为什么我每次都要设置-paste-模式) 107 | - [在终端中按 ESC 后有延时](#在终端中按-esc-后有延时) 108 | - [无法重复函数中执行的搜索](#无法重复函数中执行的搜索) 109 | - [进阶阅读](#进阶阅读) 110 | - [加入我们](#加入我们) 111 | 112 | 113 | 114 | # 简介 115 | 116 | ## 什么是 Vim? 117 | 118 | [Vim](https://github.com/vim/vim) 是一个历史悠久的文本编辑器,可以追溯到 119 | [qed]()。 120 | [Bram Moolenaar](https://en.wikipedia.org/wiki/Bram_Moolenaar) 于 121 | 1991 年发布初始版本。 122 | 123 | Linux、Mac 用户,可以使用包管理器安装 Vim,对于 Windows 用户,可以从 124 | [我的网盘](https://share.weiyun.com/da2be5937ac0e2bd3abc26355fad1204) 下载。 125 | 该版本可轻易添加 `python` 、`python3` 、`lua` 等支持,只需要安装 python、lua 126 | 即可。 127 | 128 | 项目在 [Github](https://github.com/vim/vim) 上开发,项目讨论请订阅 129 | [`vim_dev`](https://groups.google.com/forum/#!forum/vim_dev) 邮件列表。 130 | 131 | 通过阅读 [Why, oh WHY, do those #?@! nutheads use vi?](http://www.viemu.com/a-why-vi-vim.html) 132 | 来对 Vim 进行大致的了解。 133 | 134 | ## Vim 哲学 135 | 136 | Vim 采用模式编辑的理念,即它提供了多种模式,按键在不同的模式下作用不同。 137 | 你可以在**普通模式** 下浏览文件,在**插入模式**下插入文本, 138 | 在**可视模式**下选择行,在**命令模式**下执行命令等等。起初这听起来可能很复杂, 139 | 但是这有一个很大的优点:不需要通过同时按住多个键来完成操作, 140 | 大多数时候你只需要依次按下这些按键即可。越常用的操作,所需要的按键数量越少。 141 | 142 | 和模式编辑紧密相连的概念是 **操作符** 和 **动作**。**操作符** 指的是开始某个行为, 143 | 例如:修改、删除或者选择文本,之后你要用一个 **动作** 来指定需要操作的文本区域。 144 | 比如,要改变括号内的文本,需要执行 `ci(` (读做 `change inner parentheses`); 145 | 删除整个段落的内容,需要执行 `dap` (读做:`delete around paragraph`)。 146 | 147 | 如果你能看见 Vim 老司机操作,你会发现他们使用 Vim 脚本语言就如同钢琴师弹钢琴一样。复杂的操作只需要几个按键就能完成。他们甚至不用刻意去想,因为这已经成为[肌肉记忆](https://en.wikipedia.org/wiki/Muscle_memory)了。这减少[认识负荷](https://en.wikipedia.org/wiki/Cognitive_load)并帮助人们专注于实际任务。 148 | 149 | ## 入门 150 | 151 | Vim 自带一个交互式的教程,内含你需要了解的最基础的信息,你可以通过终端运行以下命令打开教程: 152 | 153 | $ vimtutor 154 | 155 | 不要因为这个看上去很无聊而跳过,按照此教程多练习。你以前用的 IDE 或者其他编辑器很少是有“模式”概念的,因此一开始你会很难适应模式切换。但是你 Vim 使用的越多,[肌肉记忆](https://en.wikipedia.org/wiki/Muscle_memory) 将越容易形成。 156 | 157 | Vim 基于一个 [vi](https://en.wikipedia.org/wiki/Vi) 克隆,叫做 [Stevie](),支持两种运行模式:"compatible" 和 "nocompatible"。在兼容模式下运行 Vim 意味着使用 vi 的默认设置,而不是 Vim 的默认设置。除非你新建一个用户的 `vimrc` 或者使用 `vim -N` 命令启动 Vim,否则就是在兼容模式下运行 Vim!请大家不要在兼容模式下运行 Vim。 158 | 159 | 下一步 160 | 161 | 1. 创建你自己的 [vimrc](#精简的-vimrc)。 162 | 2. 在第一周准备[备忘录](#备忘录)。 163 | 3. 通读[基础](#基础-1)章节了解 Vim 还有哪些功能。 164 | 4. 按需学习!Vim 是学不完的。如果你遇到了问题,先上网寻找解决方案,你的问题可能已经被解决了。Vim 拥有大量的参考文档,知道如何利用这些参考文档很有必要:[获取离线帮助](#获取离线帮助)。 165 | 5. 浏览[附加资源](#附加资源)。 166 | 167 | 最后一个建议:使用[插件](#插件管理)之前,请先掌握 Vim 的基本操作。很多插件都只是对 Vim 自带功能的封装。 168 | 169 | 返回主目录 [:arrow_heading_up:](#简介) 170 | 171 | ## 精简的 vimrc 172 | 173 | 用户的 vimrc 配置文件可以放在 `~/.vimrc`,或者为了更好的分离放在 `~/.vim/vimrc`,后者更便于通过版本控制软件备份和同步整个配置,比方说 Github。 174 | 175 | 你可以在网上找到许多精简的 vimrc 配置文件,我的版本可能并不是最简单的版本,但是我的版本提供了一套我认为良好的,非常适合入门的设置。 176 | 177 | 最终你需要阅读完那些设置,然后自行决定需要使用哪些。:-) 178 | 179 | 精简的 vimrc 地址:[minimal-vimrc](contents/minimal-vimrc.vim) 180 | 181 | 如果你有兴趣,这里是我(原作者)的 [vimrc](https://github.com/mhinz/dotfiles/blob/master/vim/vimrc)。 182 | 183 | **建议**:大多数插件作者都维护不止一个插件并且将他们的 vimrc 放在 Github 上展示(通常放在叫做 "vim-config" 或者 "dotfiles" 的仓库中),所以当你发现你喜欢的插件时,去插件维护者的 Github 主页看看有没有这样的仓库。 184 | 185 | 返回主目录 [:arrow_heading_up:](#简介) 186 | 187 | ## 我正在使用什么样的 Vim 188 | 189 | 使用 `:version` 命令将向你展示当前正在运行的 Vim 的所有相关信息,包括它是如何编译的。 190 | 191 | 第一行告诉你这个二进制文件的编译时间和版本号,比如:7.4。接下来的一行呈现 `Included patches: 1-1051`,这是补丁版本包。因此你 Vim 确切的版本号是 7.4.1051。 192 | 193 | 另一行显示着一些像 `Tiny version without GUI` 或者 `Huge version with GUI` 的信息。很显然这些信息告诉你当前的 Vim 是否支持 GUI,例如:从终端中运行 `gvim` 或者从终端模拟器中的 Vim 内运行 `:gui` 命令。另一个重要的信息是 `Tiny` 和 `Huge`。Vim 的特性集区分被叫做 `tiny`,`small`,`normal`,`big` and `huge`,所有的都实现不同的功能子集。 194 | 195 | `:version` 主要的输出内容是特性列表。`+clipboard` 意味这剪贴板功能被编译支持了,`-clipboard` 意味着剪贴板特性没有被编译支持。 196 | 197 | 一些功能特性需要编译支持才能正常工作。例如:为了让 `:prof` 工作,你需要使用 `huge` 模式编译的 Vim,因为那种模式启用了 `+profile` 特性。 198 | 199 | 如果你的输出情况并不是那样,并且你是从包管理器安装 Vim 的,确保你安装了 `vim-x`,`vim-x11`,`vim-gtk`,`vim-gnome` 这些包或者相似的,因为这些包通常都是 `huge` 模式编译的。 200 | 201 | 你也可以运行下面这段代码来测试 Vim 版本以及功能支持: 202 | 203 | ```vim 204 | " Do something if running at least Vim 7.4.42 with +profile enabled. 205 | if (v:version > 704 || v:version == 704 && has('patch42')) && has('profile') 206 | " do stuff 207 | endif 208 | ``` 209 | 210 | 相关帮助: 211 | 212 | :h :version 213 | :h feature-list 214 | :h +feature-list 215 | :h has-patch 216 | 217 | 返回主目录 [:arrow_heading_up:](#简介) 218 | 219 | ## 备忘录 220 | 221 | 为了避免版权问题,我只贴出链接: 222 | 223 | - 224 | - 225 | - 226 | - 227 | - 228 | 229 | 或者在 Vim 中快速打开备忘录:[vim-cheat40](https://github.com/lifepillar/vim-cheat40)。 230 | 231 | 返回主目录 [:arrow_heading_up:](#简介) 232 | 233 | # 基础 234 | 235 | ## 缓冲区,窗口,标签 236 | 237 | Vim 是一个文本编辑器。每次文本都是作为**缓冲区**的一部分显示的。每一份文件都是在他们自己独有的缓冲区打开的,插件显示的内容也在它们自己的缓冲区中。 238 | 239 | 缓冲区有很多属性,比如这个缓冲区的内容是否可以修改,或者这个缓冲区是否和文件相关联,是否需要同步保存到磁盘上。 240 | 241 | **窗口** 是缓冲区上一层的视窗。如果你想同时查看几个文件或者查看同一文件的不同位置,那样你会需要窗口。 242 | 243 | 请别把他们叫做 _分屏_ 。你可以把一个窗口分割成两个,但是这并没有让这两个窗口完全 _分离_ 。 244 | 245 | 窗口可以水平或者竖直分割并且现有窗口的高度和宽度都是可以被调节设置的,因此,如果你需要多种窗口布局,请考虑使用标签。 246 | 247 | **标签页** (标签)是窗口的集合。因此当你想使用多种窗口布局时候请使用标签。 248 | 249 | 简单的说,如果你启动 Vim 的时候没有附带任何参数,你会得到一个包含着一个呈现一个缓冲区的窗口的标签。 250 | 251 | 顺带提一下,缓冲区列表是全局可见的,你可以在任何标签中访问任何一个缓冲区。 252 | 253 | 返回主目录 [:arrow_heading_up:](#基础) 254 | 255 | ## 已激活、已载入、已列出、已命名的缓冲区 256 | 257 | 用类似 `vim file1` 的命令启动 Vim 。这个文件的内容将会被加载到缓冲区中,你现在有一个**已载入的缓冲区**。如果你在 Vim 中保存这个文件,缓冲区内容将会被同步到磁盘上(写回文件中)。 258 | 259 | 由于这个缓冲区也在一个窗口上显示,所以他也是一个**已激活的缓冲区**。如果你现在通过 `:e file2` 命令加载另一个文件,`file1` 将会变成一个**隐藏的缓冲区**,并且 `file2` 变成已激活缓冲区。 260 | 261 | 使用 `:ls` 我们能够列出所有可以列出的缓冲区。插件缓冲区和帮助缓冲区通常被标记为不可以列出的缓冲区,因为那并不是你经常需要在编辑器中编辑的常规文件。通过 `:ls!` 命令可以显示被放入缓冲区列表的和未被放入列表的缓冲区。 262 | 263 | **未命名的缓冲区**是一种没有关联特定文件的缓冲区,这种缓冲区经常被插件使用。比如 `:enew` 将会创建一个无名临时缓冲区。添加一些文本然后使用 `:w /tmp/foo` 将他写入到磁盘,这样这个缓冲区就会变成一个**已命名的缓冲区**。 264 | 265 | 返回主目录 [:arrow_heading_up:](#基础) 266 | 267 | ## 参数列表 268 | 269 | [全局缓冲区列表](#缓冲区窗口标签)是 Vim 的特性。在这之前的 vi 中,仅仅只有参数列表,参数列表在 Vim 中依旧可以使用。 270 | 271 | 每一个通过 shell 命令传递给 Vim 的文件名都被记录在一个参数列表中。可以有多个参数列表:默认情况下所有参数都被放在全局参数列表下,但是你可以使用 `:arglocal` 命令去创建一个新的本地窗口的参数列表。 272 | 273 | 使用 `:args` 命令可以列出当前参数。使用 `:next`,`:previous`,`:first`,`:last` 命令可以在切换在参数列表中的文件。通过使用 `:argadd`,`:argdelete` 或者 `:args` 等命令加上一个文件列表可以改变参数列表。 274 | 275 | 偏爱缓冲区列表还是参数列表完全是个人选择,我的印象中大多数人都是使用缓冲区列表的。 276 | 277 | 然而参数列表在有些情况下被大量使用:批处理 278 | 使用 `:argdo`! 一个简单的重构例子: 279 | 280 | ```vim 281 | :args **/*.[ch] 282 | :argdo %s/foo/bar/ge | update 283 | ``` 284 | 285 | 这条命令将替换掉当前目录下以及当前目录的子目录中所有的 C 源文件和头文件中的“foo”,并用“bar”代替。 286 | 287 | 相关帮助:`:h argument-list` 288 | 289 | 返回主目录 [:arrow_heading_up:](#基础) 290 | 291 | ## 按键映射 292 | 293 | 使用 `:map` 命令家族你可以定义属于你自己的快捷键。该家族的每一个命令都限定在特定的模式下。从技术上来说 Vim 自带高达 12 中模式,其中 6 种可以被映射。另外一些命令作用于多种模式: 294 | 295 | |   递归   |   非递归     | 模式                           | 296 | | -------- | ------------ | -------------------------------- | 297 | | `:map` | `:noremap` | normal, visual, operator-pending | 298 | | `:nmap` | `:nnoremap` | normal | 299 | | `:xmap` | `:xnoremap` | visual | 300 | | `:cmap` | `:cnoremap` | command-line | 301 | | `:omap` | `:onoremap` | operator-pending | 302 | | `:imap` | `:inoremap` | insert | 303 | 304 | 例如:这个自定义的快捷键只在普通模式下工作。 305 | 306 | ```vim 307 | :nmap :echo "foo" 308 | ``` 309 | 310 | 使用 `:nunmap ` 可以取消这个映射。 311 | 312 | 对于更少数,不常见的模式(或者他们的组合),查看 `:h map-modes`。 313 | 314 | 到现在为止还好,对新手而言有一个问题会困扰他们:`:nmap` 是**递归执行**的!结果是,右边执行可能的映射。 315 | 316 | 你自定义了一个简单的映射去输出“Foo”: 317 | 318 | ```vim 319 | :nmap b :echo "Foo" 320 | ``` 321 | 322 | 但是如果你想要映射 `b` (回退一个单词)的默认功能到一个键上呢? 323 | 324 | ```vim 325 | :nmap a b 326 | ``` 327 | 328 | 如果你敲击a,我们期望着光标回退到上一个单词,但是实际情况是“Foo”被输出到命令行里!因为在右边,`b` 已经被映射到别的行为上了,换句话说就是 `:echo "Foo"`。 329 | 330 | 解决此问题的正确方法是使用一种 _非递归_ 的映射代替: 331 | 332 | ```vim 333 | :nnoremap a b 334 | ``` 335 | 336 | 经验法则:除非递归是必须的,否则总是使用非递归映射。 337 | 338 | 通过不给一个右值来检查你的映射。比如`:nmap` 显示所以普通模式下的映射,`:nmap ` 显示所有以 `` 键开头的普通模式下的映射。 339 | 340 | 如果你想禁止用标准映射,把他们映射到特殊字符 `` 上,例如:`:noremap `。 341 | 342 | 相关帮助: 343 | 344 | ```vim 345 | :h key-notation 346 | :h mapping 347 | :h 05.3 348 | ``` 349 | 350 | 返回主目录 [:arrow_heading_up:](#基础) 351 | 352 | ## 映射前置键 353 | 354 | 映射前置键(Leader 键)本身就是一个按键映射,默认为 \\。我们可以通过在 `map` 中调用 `` 来为把它添加到其他按键映射中。 355 | 356 | ```vim 357 | nnoremap h :helpgrep 358 | ``` 359 | 360 | 这样,我们只需要先按 \\ 然后连续按 \\h 就可以激活这个映射 `:helpgrep`。如果你想通过先按 空格 键来触发,只需要这样做: 361 | 362 | ```vim 363 | let mapleader = ' ' 364 | nnoremap h :helpgrep 365 | ``` 366 | 367 | 另外,还有一个叫 `` 的,可以把它理解为局部环境中的 ``,默认值依然为 \\。当我们需要只对某一个条件下(比如,特定文件类型的插件)的缓冲区设置特别的 `` 键,那么我们就可以通过修改当前环境下的 `` 来实现。 368 | 369 | **注意**:如果你打算设置 Leader 键,请确保在设置按键映射之前,先设置好 Leader 键。如果你先设置了含有 Leader 键的映射,然后又修改了 Leader 键,那么之前映射内的 Leader 键是不会因此而改变的。你可以通过执行 `:nmap ` 来查看普通模式中已绑定给 Leader 键的所有映射。 370 | 371 | 请参阅 `:h mapleader` 与 `:h maploacalleader` 来获取更多帮助。 372 | 373 | 返回主目录 [:arrow_heading_up:](#基础) 374 | 375 | ## 寄存器 376 | 377 | 寄存器就是存储文本的地方。我们常用的「复制」操作就是把文本存储到寄存器,「 粘贴」 操作就是把文本从寄存器中读出来。顺便,在 Vim 中复制的快捷键是 y,粘贴的快捷键是 p。 378 | 379 | Vim 为我们提供了如下的寄存器: 380 | 381 | | 类型 | 标识 | 读写者 | 是否为只读 | 包含的字符来源 | 382 | | ------------------- | ------------------ | ------ | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 383 | | Unnamed | `"` | vim | 否 | 最近一次的复制或删除操作 (`d`, `c`, `s`, `x`, `y`) | 384 | | Numbered | `0`至`9` | vim | 否 | 寄存器 `0`: 最近一次复制。寄存器 `1`: 最近一次删除。寄存器 `2`: 倒数第二次删除,以此类推。对于寄存器 `1` 至 `9`,他们其实是只读的最多包含 9 个元素的队列。这里的队列即为数据类型 [queue]() | 385 | | Small delete | `-` | vim | 否 | 最近一次行内删除 | 386 | | Named | `a`至`z`, `A`至`Z` | 用户 | 否 | 如果你通过复制操作存储文本至寄存器 `a`,那么 `a` 中的文本就会被完全覆盖。如果你存储至 `A`,那么会将文本添加给寄存器 `a`,不会覆盖之前已有的文本 | 387 | | Read-only | `:`与`.`和`%` | vim | 是 | `:`: 最近一次使用的命令,`.`: 最近一次添加的文本,`%`: 当前的文件名 | 388 | | Alternate buffer | `#` | vim | 否 | 大部分情况下,这个寄存器是当前窗口中,上一次访问的缓冲区。请参阅 `:h alternate-file` 来获取更多帮助 | 389 | | Expression | `=` | 用户 | 否 | 复制 VimL 代码时,这个寄存器用于存储代码片段的执行结果。比如,在插入模式下复制 `=5+5`,那么这个寄存器就会存入 10 | 390 | | Selection | `+`和`*` | vim | 否 | `*` 和 `+` 是 [剪贴板](#剪贴板) 寄存器 | 391 | | Drop | `~` | vim | 是 | 最后一次拖拽添加至 Vim 的文本(需要 "+dnd" 支持,暂时只支持 GTK GUI。请参阅 `:help dnd` 及 `:help quote~`) | 392 | | Black hole | `_` | vim | 否 | 一般称为黑洞寄存器。对于当前操作,如果你不希望在其他寄存器中保留文本,那就在命令前加上 `_`。比如,`"_dd` 命令不会将文本放到寄存器 `"`、`1`、`+` 或 `*` 中 | 393 | | Last search pattern | `/` | vim | 否 | 最近一次通过 `/`、`?` 或 `:global` 等命令调用的匹配条件 | 394 | 395 | 只要不是只读的寄存器,用户都有权限修改它的内容,比如: 396 | 397 | ```vim 398 | :let @/ = 'register' 399 | ``` 400 | 401 | 这样,我们按 n 的时候就会跳转到单词"register" 出现的地方。 402 | 403 | 有些时候,你的操作可能已经修改了寄存器,而你没有察觉到。请参阅 `:h registers` 获取更多帮助。 404 | 405 | 上面提到过,复制的命令是 y,粘贴的命令是 p 或者 P。但请注意,Vim 会区分「字符选取」与「行选取」。请参阅 `:h linewise` 获取更多帮助。 406 | 407 | **行选取**: 408 | 命令 `yy` 或 `Y` 都是复制当前行。这时移动光标至其他位置,按下 `p` 就可以在光标下方粘贴复制的行,按下 `P` 就可以在光标上方粘贴至复制的行。 409 | 410 | **字符选取**: 411 | 命令 `0yw` 可以复制第一个单词。这时移动光标至其他位置,按下 `p` 就可以在当前行、光标后的位置粘贴单词,按下 `P` 就可以在当前行、光标前的位置粘贴单词。 412 | 413 | **将文本存到指定的寄存器中**: 414 | 命令 `"aY` 可以将当前行复制,并存储到寄存器 `a` 中。这时移动光标至其他位置,通过命令 `"AY` 就可以把这一行的内容扩展到寄存器 `a` 中,而之前存储的内容也不会丢失。 415 | 416 | 为了便于理解和记忆,建议大家现在就试一试上面提到的这些操作。操作过程中,你可以随时通过 `:reg` 来查看寄存器的变化。 417 | 418 | **有趣的是**: 419 | 在 Vim 中,`y` 是复制命令,源于单词 "yanking"。而在 Emacs 中,"yanking" 代表的是粘贴(或者说,重新插入刚才删掉的内容),而并不是复制。 420 | 421 | 返回主目录 [:arrow_heading_up:](#基础) 422 | 423 | ## 范围 424 | 425 | 范围 (Ranges) 其实很好理解,但很多 Vim 用户的理解不到位。 426 | 427 | - 很多命令都可以加一个数字,用于指明操作范围 428 | - 范围可以是一个行号,用于指定某一行 429 | - 范围也可以是一对通过 `,` 或 `;` 分割的行号 430 | - 大部分命令,默认只作用于当前行 431 | - 只有 `:write` 和 `:global` 是默认作用于所有行的 432 | 433 | 范围的使用是十分直观的。以下为一些例子(其中,`:d` 为 `:delete` 的缩写): 434 | 435 | | 命令 | 操作的行 | 436 | | ------------------- | ----------------------------------------------------------------- | 437 | | `:d` | 当前行 | 438 | | `:.d` | 当前行 | 439 | | `:1d` | 第一行 | 440 | | `:$d` | 最后一行 | 441 | | `:1,$d` | 所有行 | 442 | | `:%d` | 所有行(这是 `1,$` 的语法糖) | 443 | | `:.,5d` | 当前行至第 5 行 | 444 | | `:,5d` | 同样是当前行至第 5 行 | 445 | | `:,+3d` | 当前行及接下来的 3 行 | 446 | | `:1,+3d` | 第一行至当前行再加 3 行 | 447 | | `:,-3d` | 当前行及向上的 3 行(Vim 会弹出提示信息,因为这是一个保留的范围) | 448 | | `:3,'xdelete` | 第三行至[标注](#标注) 为 x 的那一行 | 449 | | `:/^foo/,$delete` | 当前行以下,以字符 "foo" 开头的那一行至结尾 | 450 | | `:/^foo/+1,$delete` | 当前行以下,以字符 "foo" 开头的那一行的下一行至结尾 | 451 | 452 | 需要注意的是,`;` 也可以用于表示范围。区别在于,`a,b` 的 `b` 是以当前行作为参考的。而 `a;b` 的 `b` 是以 `a` 行作为参考的。举个例子,现在你的光标在第 5 行。这时 `:1,+1d` 会删除第 1 行至第 6 行,而 `:1;+1d` 会删除第 1 行和第 2 行。 453 | 454 | 如果你想设置多个寻找条件,只需要在条件前加上 `/`,比如: 455 | 456 | ```vim 457 | :/foo//bar//quux/d 458 | ``` 459 | 460 | 这就会删除当前行之后的某一行。定位方式是,先在当前行之后寻找第一个包含 "foo" 字符的那一行,然后在找到的这一行之后寻找第一个包含 "bar" 字符的那一行,然后再在找到的这一行之后寻找第一个包含 "quux" 的那一行。删除的就是最后找到的这一行。 461 | 462 | 有时,Vim 会在命令前自动添加范围。举个例子,如果你先通过 `V` 命令进入行选取模式,选中一些行后按下 `:` 进入命令模式,这时候你会发现 Vim 自动添加了 `'<,'>` 范围。这表示,接下来的命令会使用之前选取的行号作为范围。但如果后续命令不支持范围,Vim 就会报错。为了避免这样的情况发生,有些人会设置这样的按键映射:`:vnoremap foo :command`,组合键 Ctrl + u 可以清除当前命令行中的内容。 463 | 464 | 另一个例子是在普通模式中按下 `!!`,命令行中会出现 `:.!`。如果这时你如果输入一个外部命令,那么当前行的内容就会被这个外部命令的输出替换。你也可以通过命令 `:?^$?+1,/^$/-1!ls` 把当前段落的内容替换成外部命令 `ls` 的输出,原理是向前和向后各搜索一个空白行,删除这两个空白行之间的内容,并将外部命令 `ls` 的输出放到这两个空白行之间。 465 | 466 | 请参阅以下两个命令来获取更多帮助: 467 | 468 | ```vim 469 | :h cmdline-ranges 470 | :h 10.3 471 | ``` 472 | 473 | 返回主目录 [:arrow_heading_up:](#基础) 474 | 475 | ## 标注 476 | 477 | 你可以使用标注功能来标记一个位置,也就是记录文件某行的某个位置。 478 | 479 | | 标注 | 设置者 | 使用 | 480 | | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------- | 481 | | `a`-`z` | 用户 | 仅对当前的一个文件生效,也就意味着只可以在当前文件中跳转 | 482 | | `A`-`Z` | 用户 | 全局标注,可以作用于不同文件。大写标注也称为「文件标注」。跳转时有可能会切换到另一个缓冲区 | 483 | | `0`-`9` | viminfo | `0` 代表 viminfo 最后一次被写入的位置。实际使用中,就代表 Vim 进程最后一次结束的位置。`1` 代表 Vim 进程倒数第二次结束的位置,以此类推 | 484 | 485 | 如果想跳转到指定的标注,你可以先按下 `'` / `g'` 或者 `` ` `` / `` g` `` 然后按下标注名。 486 | 487 | 如果你想定义当前文件中的标注,可以先按下 `m` 再按下标注名。比如,按下 `mm` 就可以把当前位置标注为 `m`。在这之后,如果你的光标切换到了文件的其他位置,只需要通过 `'m` 或者 `` `m ``即可回到刚才标注的行。区别在于,`'m`会跳转回被标记行的第一个非空字符,而`` `m ``会跳转回被标记行的被标记列。根据 viminfo 的设置,你可以在退出 Vim 的时候保留小写字符标注。请参阅`:h viminfo-'` 来获取更多帮助。 488 | 489 | 如果你想定义全局的标注,可以先按下 `m` 再按下大写英文字符。比如,按下 `mM` 就可以把当前文件的当前位置标注为 `M`。在这之后,就算你切换到其他的缓冲区,依然可以通过 `'M` 或 `` `M `` 跳转回来。 490 | 491 | 关于跳转,还有以下的方式: 492 | 493 | | 按键 | 跳转至 | 494 | | ---------------- | ---------------------------------------------- | 495 | | `'[` 与 `` `[ `` | 上一次修改或复制的第一行或第一个字符 | 496 | | `']` 与 `` `] `` | 上一次修改或复制的最后一行或最后一个字符 | 497 | | `'<` 与 `` `< `` | 上一次在可视模式下选取的第一行或第一个字符 | 498 | | `'>` 与 `` `> `` | 上一次在可视模式下选取的最后一行或最后一个字符 | 499 | | `''` 与 `` `' `` | 上一次跳转之前的光标位置 | 500 | | `'"` 与 `` `" `` | 上一次关闭当前缓冲区时的光标位置 | 501 | | `'^` 与 `` `^ `` | 上一次插入字符后的光标位置 | 502 | | `'.` 与 `` `. `` | 上一次修改文本后的光标位置 | 503 | | `'(` 与 `` `( `` | 当前句子的开头 | 504 | | `')` 与 `` `) `` | 当前句子的结尾 | 505 | | `'{` 与 `` `{ `` | 当前段落的开头 | 506 | | `'}` 与 `` `} `` | 当前段落的结尾 | 507 | 508 | 标注也可以搭配 [范围](#范围) 一起使用。前面提到过,如果你在可视模式下选取一些文本,然后按下 `:`,这时候你会发现命令行已经被填充了 `:'<,'>`。对照上面的表格,现在你应该明白了,这段代表的就是可视模式下选取的范围。 509 | 510 | 请使用 `:marks` 命令来显示所有的标注,参阅 `:h mark-motions` 来获取关于标注的更多帮助。 511 | 512 | 返回主目录 [:arrow_heading_up:](#基础) 513 | 514 | ## 补全 515 | 516 | Vim 在插入模式中为我们提供了多种补全方案。如果有多个补全结果,Vim 会弹出一个菜单供你选择。 517 | 518 | 常见的补全有标签、项目中引入的模块或库中的方法名、文件名、字典及当前缓冲区的字段。 519 | 520 | 针对不同的补全方案,Vim 为我们提供了不同的按键映射。这些映射都是在**插入模式中**通过 Ctrl + x 来触发: 521 | 522 | | 映射 | 类型 | 帮助文档 | 523 | | ------------ | ----------------------------------------------- | ---------- | 524 | | `` | 整行 | `:h i^x^l` | 525 | | `` | 当前缓冲区中的关键字 | `:h i^x^n` | 526 | | `` | 字典(请参阅 `:h 'dictionary'`)中的关键字 | `:h i^x^k` | 527 | | `` | 同义词字典(请参阅 `:h 'thesaurus'`)中的关键字 | `:h i^x^t` | 528 | | `` | 当前文件以及包含的文件中的关键字 | `:h i^x^i` | 529 | | `` | 标签 | `:h i^x^]` | 530 | | `` | 文件名 | `:h i^x^f` | 531 | | `` | 定义或宏定义 | `:h i^x^d` | 532 | | `` | Vim 命令 | `:h i^x^v` | 533 | | `` | 用户自定义补全(通过 `'completefunc'` 定义) | `:h i^x^u` | 534 | | `` | Omni Completion(通过 `'omnifunc'` 定义) | `:h i^x^o` | 535 | | `s` | 拼写建议 | `:h i^Xs` | 536 | 537 | 尽管用户自定义补全与 Omni Completion 是不同的,但他们做的事情基本一致。共同点在于,他们都是一个监听当前光标位置的函数,返回值为一系列的补全建议。用户自定义补全是由用户定义的,基于用户的个人用途,因此你可以根据自己的喜好和需求随意定制。而 Omni Completion 是针对文件类型的补全,比如在 C 语言中补全一个结构体(struct)的成员(members),或者补全一个类的方法,因而它通常都是由文件类型插件设置和调用的。 538 | 539 | 如果你设置了 `'complete'` 选项,那么你就可以在一次操作中采用多种补全方案。这个选项默认包含了多种可能性,因此请按照自己的需求来配置。你可以通过 `` 来调用下一个补全建议,或通过 `` 来调用上一个补全建议。当然,这两个映射同样可以直接调用补全函数。请参阅 `:h i^n` 与 `:h 'complete'` 来获得更多帮助。 540 | 541 | 如果你想配置弹出菜单的行为,请一定要看一看 `:h 'completeopt'` 这篇帮助文档。默认的配置已经不错了,但我个人(原作者)更倾向于把 "noselect" 加上。 542 | 543 | 请参阅以下文档获取更多帮助: 544 | 545 | ```vim 546 | :h ins-completion 547 | :h popupmenu-keys 548 | :h new-omni-completion 549 | ``` 550 | 551 | 返回主目录 [:arrow_heading_up:](#基础) 552 | 553 | ## 动作,操作符,文本对象 554 | 555 | **动作**也就是指移动光标的操作,你肯定很熟悉 `h`、`j`、`k` 和 `l`,以及 `w` 和 `b`。但其实,`/` 也是一个动作。他们都可以搭配数字使用,比如 `2?the` 可以将光标移动到倒数第二个 "the" 出现的位置。 556 | 557 | 以下会列出一些常用的动作。你也可以通过 `:h navigation` 来获取更多的帮助。 558 | 559 | **操作符**是对某个区域文本执行的操作。比如,`d`、`~`、`gU` 和 `>` 都是操作符。这些操作符既可以在普通模式下使用,也可以在可视模式下使用。在普通模式中,顺序是先按操作符,再按动作指令,比如 `>j`。在可视模式中,选中区域后直接按操作符就可以,比如 `Vjd`。 560 | 561 | 与动作一样,操作符也可以搭配数字使用,比如 `2gUw` 可以将当前单词以及下一个单词转成大写。由于动作和操作符都可以搭配数字使用,因此 `2gU2w` 与执行两次 `gU2w` 效果是相同的。 562 | 563 | 请参阅 `:h operator` 来查看所有的操作符。你也可以通过 `:set tildeop` 命令把 `~` 也变成一个操作符 564 | 565 | 值得注意的是,动作是单向的,而**文本对象**是双向的。文本对象不仅作用于符号(比如括号、中括号和大括号等)标记的范围内,也作用于整个单词、整个句子等其他情况。 566 | 567 | 文本对象不能用于普通模式中移动光标的操作,因为光标还没有智能到可以向两个方向同时跳转。但这个功能可以在可视模式中实现,因为在对象的一端选中的情况下,光标只需要跳转到另一端就可以了。 568 | 569 | 文本对象操作一般用 `i` 或 `a` 加上对象标识符操作,其中 `i` 表示在对象内(英文 inner)操作,`a` 表示对整个对象(英文 around)操作,这时开头和结尾的空格都会被考虑进来。举个例子,`diw` 可以删除当前单词,`ci(` 可以改变括号中的内容。 570 | 571 | 文本对象同样可以与数字搭配使用。比如,像 `((( )))` 这样的文本,假如光标位于最内层的括号上或最内层的括号内,那么 `d2a(` 将会删除从最内层开始的两对括号,以及他们之间的所有内容。其实,`d2a(` 这个操作等同于 `2da(`。在 Vim 的命令中,如果有两处都可以接收数字作为参数,那么最终结果就等同于两个数字相乘。在这里,`d` 与 `a(` 都是可以接收参数的,一个参数是 1,另一个是 2,我们可以把它们相乘然后放到最前面。 572 | 573 | 请参阅 `:h text-objects` 来获取更多关于文本对象的帮助。 574 | 575 | 返回主目录 [:arrow_heading_up:](#基础) 576 | 577 | ## 自动命令 578 | 579 | 在特定的情况下,Vim 会传出事件。如果你想针对这些事件执行回调方法,那么就需要用到自动命令这个功能。 580 | 581 | 如果没有了自动命令,那你基本上是用不了 Vim 的。自动命令一直都在执行,只是很多时候你没有注意到。不信的话,可以执行命令 `:au` ,不要被结果吓到,这些是当前有效的所有自动命令。 582 | 583 | 请使用 `:h {event}` 来查看 Vim 中所有事件的列表,你也可以参考 `:h autocmd-events-abc` 来获取关于事件的更多帮助。 584 | 585 | 一个很常用的例子,就是针对文件类型执行某些设置: 586 | 587 | ```vim 588 | autocmd FileType ruby setlocal shiftwidth=2 softtabstop=2 comments-=:# 589 | ``` 590 | 591 | 但是缓冲区是如何知道当前的文件中包含 Ruby 代码呢?这其实是另一个自动命令检测的到的,然后把文件类型设置成为 Ruby,这样就触发了上面的 `FileType` 事件。 592 | 593 | 在配置 vimrc 的时候,一般第一行加进去的就是 `filetype on`。这就意味着,Vim 启动时会读取 `filetype.vim` 文件,然后根据文件类型来触发相应的自动命令。 594 | 595 | 如果你勇于尝试,可以查看下 `:e $VIMRUNTIME/filetype.vim`,然后在输出中搜索 "Ruby"。这样,你就会发现其实 Vim 只是通过文件扩展名 `.rb` 判断某个文件是不是 Ruby 的。 596 | 597 | **注意**:对于相同事件,如果有多个自动命令,那么自动命令会按照定义时的顺序执行。通过 `:au` 就可以查看它们的执行顺序。 598 | 599 | ```vim 600 | au BufNewFile,BufRead *.rb,*.rbw setf ruby 601 | ``` 602 | 603 | `BufNewFile` 与 `BufRead` 事件是被写在 Vim 源文件中的。因此,每当你通过 `:e` 或者类似的命令打开文件,这两个事件都会触发。然后,就是读取 `filetype.vim` 文件来判断打开的文件类型。 604 | 605 | 简单来说,事件和自动命令在 Vim 中的应用十分广泛。而且,Vim 为我们留出了一些易用的接口,方便用户配置适合自己的事件驱动回调。 606 | 607 | 请参阅 `:h autocommand` 来获取更多帮助 608 | 609 | 返回主目录 [:arrow_heading_up:](#基础) 610 | 611 | ## 变更历史,跳转历史 612 | 613 | 在 Vim 中,用户最近 100 次的文字改动都会被保存在**变更历史**中。如果在同一行有多个小改动,那么 Vim 会把它们合并成一个。尽管内容改动会合并,但作用的位置还是会只记录下最后一次改动的位置。 614 | 615 | 在你移动光标或跳转的时候,每一次的移动或跳转前的位置会被记录到**跳转历史**中。类似地,跳转历史也可以最多保存 100 条记录。对于每个窗口,跳转记录是独立的。但当你分离窗口时(比如使用 `:split` 命令),跳转历史会被复制过去。 616 | 617 | Vim 中的跳转命令,包括 `'`、`` ` ``、`G`、`/`、`?`、`n`、`N`、`%`、`(`、`)`、`[[`、`]]`、`{`、`}`、`:s`、`:tag`、`L`、`M`、`H` 以及开始编辑一个新文件的命令。 618 | 619 | | 列表 | 显示所有条目 | 跳转到上一个位置 | 跳转到下一个位置 | 620 | | -------- | ------------ | ---------------- | ---------------- | 621 | | 跳转历史 | `:jumps` | `[count]` | `[count]` | 622 | | 变更历史 | `:changes` | `[count]g;` | `[count]g,` | 623 | 624 | 如果你执行第二列的命令显示所有条目,这时 Vim 会用 `>` 标记来为你指示当前位置。通常这个标记位于 1 的下方,也就代表最后一次的位置。 625 | 626 | 如果你希望关闭 Vim 之后还保留这些条目,请参阅 `:h viminfo-'` 来获取更多帮助。 627 | 628 | **注意**:上面提到过,最后一次跳转前的位置也会记录在[标注](#标注)中,也可以通过连按 \`\`'' 跳转到那个位置 629 | 630 | 请参阅以下两个命令来获取更多帮助: 631 | 632 | ```vim 633 | :h changelist 634 | :h jumplist 635 | ``` 636 | 637 | 返回主目录 [:arrow_heading_up:](#基础) 638 | 639 | ## 内容变更历史记录 640 | 641 | Vim 会记录文本改变之前的状态。因此,你可以使用「撤销」操作 u 来取消更改,也可以通过「重做」操作 Ctrl + r 来恢复更改。 642 | 643 | 值得注意的是,Vim 采用 [tree]() 数据结构来存储内容变更的历史记录,而不是采用 [queue]()。你的每次改动都会成为存储为树的节点。而且,除了第一次改动(根节点),之后的每次改动都可以找到一个对应的父节点。每一个节点都会记录改动的内容和时间。其中,「分支」代表从任一节点到根节点的路径。当你进行了撤销操作,然后又输入了新的内容,这时候就相当于创建了分支。这个原理和 git 中的 branch(分支)十分类似。 644 | 645 | 考虑以下这一系列按键操作: 646 | 647 | ```vim 648 | ifoo 649 | obar 650 | obaz 651 | u 652 | oquux 653 | ``` 654 | 655 | 那么现在,Vim 中会显示三行文本,分别是 "foo"、"bar" 和 "quux"。这时候,存储的树形结构如下: 656 | 657 | foo(1) 658 | / 659 | bar(2) 660 | / \ 661 | baz(3) quux(4) 662 | 663 | 这个树形结构共包含四次改动,括号中的数字就代表时间顺序。 664 | 665 | 现在,我们有两种方式遍历这个树结构。一种叫「按分支遍历」,一种叫「按时间遍历」。 666 | 667 | 撤销 u 与重做 Ctrl + r 操作是按分支遍历。对于上面的例子,现在我们有三行字符。这时候按 u 会回退到 "bar" 节点,如果再按一次 u 则会回退到 "foo" 节点。这时,如果我们按下 Ctrl + r 就会前进至 "bar" 节点,再按一次就回前进至 "quux" 节点。在这种方式下,我们无法访问到兄弟节点(即 "baz" 节点)。 668 | 669 | 与之对应的是按时间遍历,对应的按键是 `g-` 和 `g+`。对于上面的例子,按下 `g-` 会首先回退到 "baz" 节点。再次按下 `g-` 会回退到 "bar" 节点。 670 | 671 | | 命令/按键 | 执行效果 | 672 | | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | 673 | | `[count]u` 或 `:undo [count]` | 回退到 `[count]` 次改动之前 | 674 | | `[count]` 或 `:redo [count]` | 重做 `[count]` 次改动 | 675 | | `U` | 回退至最新的改动 | 676 | | `[count]g-` 或 `:earlier [count]?` | 根据时间回退到 `[count]` 次改动之前。"?" 为 "s"、"m"、"h"、"d" 或 "f"之一。例如,`:earlier 2d` 会回退到两天之前。`:earlier 1f` 则会回退到最近一次文件保存时的内容 | 677 | | `[count]g+` 或 `:later [count]?` | 类似 `g-`,但方向相反 | 678 | 679 | 内容变更记录会储存在内存中,当 Vim 退出时就会清空。如果需要持久化存储内容变更记录,请参阅[备份文件,交换文件,撤销文件以及 viminfo 文件的处理](#备份文件交换文件撤销文件以及viminfo文件的处理)章节的内容。 680 | 681 | 如果你觉得这一部分的内容难以理解,请参阅 [undotree](https://github.com/mbbill/undotree),这是一个可视化管理内容变更历史记录的插件。类似的还有 [vim-mundo](https://github.com/simnalamburt/vim-mundo)。 682 | 683 | 请参阅以下链接获取更多帮助: 684 | 685 | ```vim 686 | :h undo.txt 687 | :h usr_32 688 | ``` 689 | 690 | 返回主目录 [:arrow_heading_up:](#基础) 691 | 692 | ## 全局位置信息表,局部位置信息表 693 | 694 | 在某一个动作返回一系列「位置」的时候,我们可以利用「全局位置信息表」和「局部位置信息表」来存储这些位置信息,方便以后跳转回对应的位置。每一个存储的位置包括文件名、行号和列号。 695 | 696 | 比如,编译代码是出现错误,这时候我们就可以把错误的位置直接显示在全局位置信息表,或者通过外部抓取工具使位置显示在局部位置信息表中。 697 | 698 | 尽管我们也可以把这些信息显示到一个空格缓冲区中,但用这两个信息表显示的好处在于接口调用很方便,而且也便于浏览输出。 699 | 700 | Vim 中,全局位置信息表只能有一个,但每一个窗口都可以有自己的局部位置信息表。这两个信息表的外观看上去很类似,但在操作上会稍有不同。 701 | 702 | 以下为两者的操作比较: 703 | 704 | | 动作 | 全局位置信息表 | 局部位置信息表 | 705 | | ------------ | -------------- | -------------- | 706 | | 打开窗口 | `:copen` | `:lopen` | 707 | | 关闭窗口 | `:cclose` | `:lclose` | 708 | | 下一个条目 | `:cnext` | `:lnext` | 709 | | 上一个条目 | `:cprevious` | `:lprevious` | 710 | | 第一个条目 | `:cfirst` | `:lfirst` | 711 | | 最后一个条目 | `:clast` | `:llast` | 712 | 713 | 请参阅 `:h :cc` 以及底下的内容,来获取更多命令的帮助。 714 | 715 | **应用实例**: 716 | 如果我们想用 `grep` 递归地在当前文件夹中寻找某个关键词,然后把输出结果放到全局位置信息表中,只需要这样: 717 | 718 | ```vim 719 | :let &grepprg = 'grep -Rn $* .' 720 | :grep! foo 721 | 722 | :copen 723 | ``` 724 | 725 | 执行了上面的代码,你就能看到所有包含字符串 "foo" 的文件名以及匹配到的相关字段都会显示在全局位置信息表中。 726 | 727 | 返回主目录 [:arrow_heading_up:](#基础) 728 | 729 | ## 宏 730 | 731 | 你可以在 Vim 中录制一系列按键,并把他们存储到[寄存器](#寄存器)中。对于一些需要临时使用多次的一系列操作,把它们作为宏保存起来会显著地提升效率。对于一些复杂的操作,建议使用 Vim 脚本来实现。 732 | 733 | - 首先,按下 q,然后按下你想要保存的寄存器,任何小写字母都可以。比如我们来把它保存到 `q` 这个寄存器中。按下 `qq`,你会发现命令行里已经显示了 "recording @q"。 734 | - 如果你已经录制完成,那么只需要再按一次 q 就可以结束录制。 735 | - 如果你想调用刚才录制的宏,只需要 `[count]@q` 736 | - 如果你想调用上一次使用的宏,只需要 `[count]@@` 737 | 738 | **实例 1**: 739 | 740 | 一个插入字符串 "abc" 后换行的宏,重复调用十次: 741 | 742 | ```vim 743 | qq 744 | iabc 745 | q 746 | 10@q 747 | ``` 748 | 749 | (对于上面这个功能,你同样可以通过如下的按键: oabc 然后 ESC 然后 10. 来实现)。 750 | 751 | **实例 2**: 752 | 753 | 一个在每行前都加上行号的宏。从第一行开始,行号为 1,后面依次递增。我们可以通过 Ctrl + a 来实现递增的行号,在定义宏的时候,它会显示成 `^A`。 754 | 755 | ```vim 756 | qq 757 | 0yf jP0^A 758 | q 759 | 1000 @q 760 | ``` 761 | 762 | 这里能实现功能,是因为我们假定了文件最多只有 1000 行。但更好的方式是使用「递归」宏,它会一直执行,知道不能执行为止: 763 | 764 | ```vim 765 | qq 766 | 0yf jP0^A@q 767 | q 768 | @q 769 | ``` 770 | 771 | (对于上面这个插入行号的功能,如果你不愿意使用宏,同样可以通过这段按键操作来实现:`:%s/^/\=line('.') . '. '`)。 772 | 773 | 这里向大家展示了如何不用宏来达到相应的效果,但要注意,这些不用宏的实现方式只适用于这些简单的示例。对于一些比较复杂的自动化操作,你确实应该考虑使用宏。 774 | 775 | 请参阅以下文档获取更多帮助: 776 | 777 | ```vim 778 | :h recording 779 | :h 'lazyredraw' 780 | ``` 781 | 782 | 返回主目录 [:arrow_heading_up:](#基础) 783 | 784 | ## 颜色主题 785 | 786 | 颜色主题可以把你的 Vim 变得更漂亮。Vim 是由多个组件构成的,我们可以给每一个组件都设置不同的文字颜色、背景颜色以及文字加粗等等。比如,我们可以通过这个命令来设置背景颜色: 787 | 788 | ```vim 789 | :highlight Normal ctermbg=1 guibg=red 790 | ``` 791 | 792 | 执行后你会发现,现在背景颜色变成红色了。请参阅 `:h :highlight` 来获取更多帮助。 793 | 794 | 其实,颜色主题就是一系列的 `:highlight` 命令的集合。 795 | 796 | 事实上,大部分颜色主题都包含两套配置。一套适用于例如 xterm 和 iTerm 这样的终端环境(使用前缀 `cterm`),另一套适用于例如 gvim 和 MacVim 的图形界面环境(使用前缀 `gui`)。对于上面的例子,`ctermbg` 就是针对终端环境的,而 `guibg` 就是针对图形界面环境的。 797 | 798 | 如果你下载了一个颜色主题,并且在终端环境中打开了 Vim,然后发现显示的颜色与主题截图中差别很大,那很可能是配置文件只设置了图形界面环境的颜色。反之同理,如果你使用的是图形界面环境,发现显示颜色有问题,那就很可能是配置文件只设置了终端环境的颜色。 799 | 800 | 第二种情况(图形界面环境的显示问题)其实不难解决。如果你使用的是 Neovim 或者 Vim 7.4.1830 的后续版本,可以通过打开[真彩色](https://zh.wikipedia.org/wiki/真彩色)设置来解决显示问题。这就可以让终端环境的 Vim 使用 GUI 的颜色定义,但首先,你要确认一下你的终端环境和环境内的组件(比如 tmux)是否都支持真彩色。可以看一下[这篇文档](https://gist.github.com/XVilka/8346728),描述的十分详细。 801 | 802 | 请参阅以下文档或链接来获取更多帮助: 803 | 804 | - `:h 'termguicolors'` 805 | - [主题列表](#主题列表) 806 | - [自定义主题中的颜色](#自定义主题中的颜色) 807 | 808 | 返回主目录 [:arrow_heading_up:](#基础) 809 | 810 | ## 折叠 811 | 812 | 每一部分文字(或者代码)都会有特定的结构。对于存在结构的文字和代码,也就意味着它们可以按照一定的逻辑分割成不同区域。Vim 中的折叠功能,就是按照特定的逻辑把文字和代码折叠成一行,并显示一些简短的描述。折叠功能涉及到很多操作,而且折叠功能可以嵌套使用。 813 | 814 | 在 Vim 中,有以下 6 中折叠类型: 815 | 816 | | 折叠方式 | 概述 | 817 | | -------- | ------------------------------------------ | 818 | | diff | 在「比较窗口」中折叠未改变的文本 | 819 | | expr | 使用 `'foldexpr'` 来创建新的折叠逻辑 | 820 | | indent | 基于缩进折叠 | 821 | | manual | 使用 `zf`、`zF` 或 `:fold` 来自定义折叠 | 822 | | marker | 根据特定的文本标记折叠(通常用于代码注释) | 823 | | syntax | 根据语法折叠,比如折叠 `if` 代码块 | 824 | 825 | **注意**:折叠功能可能会显著地影响性能。如果你在使用折叠功能的时候出现了打字卡顿之类的问题,请考虑使用 [FastFold 插件](https://github.com/Konfekt/FastFold)。这个插件可以让 Vim 按需更新折叠内容,而不是一直调用。 826 | 827 | 请参阅以下文档获取更多帮助: 828 | 829 | ```vim 830 | :h usr_28 831 | :h folds 832 | ``` 833 | 834 | ## 会话 835 | 836 | 如果你保存了当前的「视图」(请参阅 `:h :mkview`),那么当前窗口、配置和按键映射都会被保存下来(请参阅 `:h :loadview`)。 837 | 838 | 「会话」就是存储所有窗口的相关设置,以及全局设置。简单来说,就是给当前的 Vim 运行实例拍个照,然后把相关信息存储到会话文件中。存储之后的改动就不会在会话文件中显示,你只需要在改动后更新一下会话文件就可以了。 839 | 840 | 你可以把当前工作的「项目」存储起来,然后可以在不同的「项目」之间切换。 841 | 842 | 现在就来试试吧。打开几个窗口和标签,然后执行 `:mksession Foo.vim`。如果你没有指定文件名,那就会默认保存为 `Session.vim`。这个文件会保存在当前的目录下,你可以通过 `:pwd` 来显示当前路径。重启 Vim 之后,你只需要执行 `:source Foo.vim`,就可以恢复刚才的会话了。所有的缓冲区、窗口布局、按键映射以及工作路径都会恢复到保存时的状态。 843 | 844 | 其实 Vim 的会话文件就只是 Vim 命令的集合。你可以通过命令 `:vs Foo.vim` 来看看会话文件中究竟有什么。 845 | 846 | 你可以决定 Vim 会话中究竟要保存哪些配置,只需要设置一下 `'sessionoptions'` 就可以了。 847 | 848 | 为了方便开发,Vim 把最后一次调用或写入的会话赋值给了一个内部变量 `v:this_session`。 849 | 850 | 请参阅以下文档来获取更多帮助: 851 | 852 | ```vim 853 | :h Session 854 | :h 'sessionoptions' 855 | :h v:this_session 856 | ``` 857 | 858 | ## 局部化 859 | 860 | 以上提到的很多概念,都有一个局部化(非全局)的版本: 861 | 862 | | 全局 | 局部 | 作用域 | 帮助文档 | 863 | | ----------- | --------------------- | ------------ | --------------------- | 864 | | `:set` | `:setlocal` | 缓冲区或窗口 | `:h local-options` | 865 | | `:map` | `:map ` | 缓冲区 | `:h :map-local` | 866 | | `:autocmd` | `:autocmd * ` | 缓冲区 | `:h autocmd-buflocal` | 867 | | `:cd` | `:lcd` | 窗口 | `:h :lcd` | 868 | | `:` | `:` | 缓冲区 | `:h maploacalleader` | 869 | 870 | 变量也有不同的作用域,详细内容请参考 [Vim scripting 的文档](http://vimdoc.sourceforge.net/htmldoc/usr_41.html)。 871 | 872 | # 用法 873 | 874 | ## 获取离线帮助 875 | 876 | Vim 自带了一套很完善的帮助文档,它们是一个个有固定排版格式的文本文件,通过标签可以访问这些文件的特定位置。 877 | 878 | 在开始之前先读一下这个章节:`:help :help`。执行这个命令以后会在新窗口打开 `$VIMRUNTIME/doc/helphelp.txt` 文件并跳转到这个文件中 `:help` 标签的位置。 879 | 880 | 一些关于帮助主题的简单规则: 881 | 882 | - 用单引号把文本包起来表示选项,如:`:h 'textwidth'` 883 | - 以小括号结尾表示 VimL 函数,如:`:h reverse()` 884 | - 以英文冒号开头表示命令,如:`:h :echo` 885 | 886 | 使用快捷键 `` (这是 ctrl+d)来列出所有包含你当前输入的内容的帮助主题。如:`:h tab` 会列出所有包含 `tab` 主题,从 `softtabstop` 到 `setting-guitablabel` (译者注:根据安装的插件不同列出的选项也会不同)。 887 | 888 | 你想查看所有的 VimL 方法吗?很简单,只要输入:`:h ()` 就可以了。你想查看所有与窗口相关的函数吗?输入 `:h win*()`。 889 | 890 | 相信你很快就能掌握这些技巧,但是在刚开始的时候,你可能对于该通过什么进行查找一点线索都没有。这时你可以想象一些与要查找的内容相关的关键字,再让 `:helpgrep` 来帮忙。 891 | 892 | ```vim 893 | :helpgrep backwards 894 | ``` 895 | 896 | 上面的命令会在所有的帮助文件中搜索“backwards”,然后跳转到第一个匹配的位置。所有的匹配位置都会被添加到全局位置信息表,用 `:cp / :cn` 可以在匹配位置之间进行切换。或者用 `:copen` 命令来打开全局位置信息表,将光标定位到你想要的位置,再按 回车就可以跳转到该匹配项。详细说明请参考 `:h quickfix`。 897 | 898 | ## 获取离线帮助(补充) 899 | 900 | 这个列表最初发表在 [vim_dev](https://groups.google.com/forum/#!forum/vim_dev),由 @chrisbra 编辑的,他是 Vim 开发人员中最活跃的一个。 901 | 902 | 经过一些微小的改动后,重新发布到了这里。 903 | 904 | --- 905 | 906 | 如果你知道你想要找什么,使用帮助系统的搜索会更简单一些,因为搜索出的主题都带有固定的格式。 907 | 908 | 而且帮助系统中的主题包含了你当前使用的 Vim 版本的所特有特性,而网上那些已经过时或者是早期发布的话题是不会包含这些的。 909 | 910 | 因此学习使用帮助系统以及它所用的语言是很有必要的。这里是一些例子(不一定全,我有可能忘了一些什么)。 911 | 912 | (译者注:下面列表中提及的都是如何指定搜索主题以便快速准确的找到你想要的帮助) 913 | 914 | 1. 选项要用单引号引起来。用 `:h 'list'` 来查看列表选项帮助。只有你明确的知道你要找这么一个选项的时候才可以这么做,不然的话你可以用 `:h options.txt` 来打开所有选项的帮助页面,再用正则表达式进行搜索,如:`/width`。某些选项有它们自己的命名空间,如:`:h cpo-a`,`:h cpo-A`, `:h cpo-b` 等等。 915 | 916 | 2. 普通模式的命令不能用冒号作为前缀。使用 `:h gt` 来转到“gt”命令的帮助页面。 917 | 918 | 3. 正则表达式以“/”开头,所以 `:h /\+` 会带你到正则表达式中量词“+”的帮助页面。 919 | 920 | 4. 组合键经常以一个字母开头表示它们可以在哪些模式中使用。如:`:h i_CTRL-X` 会带你到插入模式下的 CTRL-X 命令的用法帮助页面,这是一个自动完成类的组合键。需要注意的是某些键是有固定写法的,如 Control 键写成 CTRL。还有,查找普通模式下的组合键帮助时,可以省略开头的字母“n”,如:`:h CTRL-A`。而 `:h c_CTRL-A`(译者注:原文为 `:h c_CRTL-R`,感觉改为 A 更符合上下文语境)会解释 CTRL-A 在命令模式下输入命令时的作用;`:h v_CTRL-A` 说的是在可见模式下把光标所在处的数字加 1;`:h g_CTRL-A` 则说的是 g 命令(你需要先按 "g" 的命令)。这里的 "g" 代表一个普通的命令,这个命令总是与其它的按键组合使用才生效,与 "z" 开始的命令相似。 921 | 922 | 5. 寄存器是以 "quote" 开头的。如:`:h quote:` (译者注:原文为`:h quote`,感觉作者想以":"来举例)来查看关于":"寄存器的说明。 923 | 924 | 6. 关于 Vim 脚本(VimL)的帮助都在 `:h eval.txt` 里。而某些方面的语言可以使用 `:h expr-X` 获取帮助,其中的 'X' 是一个特定的字符,如:`:h expr-!` 会跳转到描述 VimL 中'!'(非)的章节。另外一个重要提示,可以使用 `:h function-list` 来查看所有函数的简要描述,列表中包括函数名和一句话描述。 925 | 926 | 7. 关于映射都可以在 `:h map.txt` 中找到。通过 `:h mapmode-i` 来查找 `:imap` 命令的相关信息;通过 `:h map-topic` 来查找专门针对映射的帮助(译者注:topic 为一个占位符,正如上面的字符 'X' 一样,在实际使用中需要替换成相应的单词)(如:`:h :map-local` 查询本地 buffer 的映射,`:h map-bar` 查询如何在映射中处理'|')。 927 | 928 | 8. 命令定义用 "command-" 开头,如用 `:h command-bar` 来查看自定义命令中'!'的作用。 929 | 930 | 9. 窗口管理类的命令是以 "CTRL-W" 开头的,所以你可以用 `:h CTRL-W_*` 来查找相应的帮助(译者注:'\*'同样为占位符)(如:`:h CTRL-W_p` 查看切换到之前访问的窗口命令的解释)。如果你想找窗口处理的命令,还可以通过访问 `:h windows.txt` 并逐行向下浏览,所有窗口管理的命令都在这里了。 931 | 932 | 10. 执行类的命令以":"开头,即:`:h :s` 讲的是 ":s" 命令。 933 | 934 | 11. 在输入某个话题时按 CTRL-D,让 Vim 列出所有的近似项辅助你输入。 935 | 936 | 12. 用 `:helpgrep` 在所有的帮助页面(通常还包括了已安装的插件的帮助页面)中进行搜索。参考 `:h :helpgrep` 来了解如何使用。当你搜索了一个话题之后,所有的匹配结果都被保存到了全局位置信息表(或局部位置信息表)当中,可以通过 `:copen` 或 `:lopen` 打开。在打开的窗口中可能通过 `/` 对搜索结果进行进一步的过滤。 937 | 938 | 13. `:h helphelp` 里介绍了如何使用帮助系统。 939 | 940 | 14. 用户手册。它采用了一种对初学者更加友好的方式来展示帮助话题。用 `:h usr_toc.txt` 打开目录(你可能已经猜到这个命令的用处了)。浏览用户手册能帮助你找出某些你想了解的话题,如你可以在第 24 章看到关于“复合字符”以及“输入特殊字符”的讲解(用 `:h usr_24.txt` 可以快速打开相关章节)。 941 | 942 | 15. 高亮分组的帮助以 `hl-` 开头。如:`:h hl-WarningMsg` 说的是警告信息分组的高亮。 943 | 944 | 16. 语法高亮以`:syc-` 开头,如:`:h :syn-conceal` 讲的是 `:syn` 命令的对于隐藏字符是如何显示的。 945 | 946 | 17. 快速修复命令以 `:c` 开头,而位置列表命令以 `:l` 开头。 947 | 948 | 18. `:h BufWinLeave` 讲的是 BufWinLeave 自动命令。还有,`:h autocommand-events` (译者注:原文是 `:h autocommands-events`,但是没有该帮助)讲的是所有可用的事件。 949 | 950 | 19. 启动参数都以“-”开头,如:`:h -f` 会告诉你 Vim 中 “-f” 参数的作用。 951 | 952 | 20. 额外的特性都以“+”开头,如:`:h +conceal` 讲的是关于隐藏字符的支持。 953 | 954 | 21. 错误代码可以在帮助系统中直接查到。`:h E297` 会带你到关于这一错误的详细解释。但是有时并没有转到错误描述,而是列出了经常导出这一错误的 Vim 命令,如 `:h E128` (译者注:原文为`:h hE128`,但是并没有该帮助)会直接跳转到 `:function` 命令。 955 | 956 | 22. 关于包含的语法文件的文档的帮助话题格式是 `:h ft-*-syntax`。如:`:h ft-c-syntax` 说的就是 C 语言语法文件以及它所提供的选项。有的语法文件还会带有自动完成(`:h ft-php-omni`)或文件类型插件(`:h ft-tex-plugin`)相关的章节可以查看。 957 | 958 | 另外在每个帮助页的顶端通常会包含一个用户文档链接(更多的从从用户的角度出发来主角命令的功能和用法,不涉及那么多细节)。如:`:h pattern.txt` 里包含了 `:h 03.9` 和 `:h usr_27` 两个章节的链接。 959 | 960 | ## 获取在线帮助 961 | 962 | 如果你遇到了无法解决的问题,或者需要指引的话,可以参考 [Vim 使用](https://groups.google.com/forum/#!forum/vim_use)邮件列表。 [IRC](https://de.wikipedia.org/wiki/Internet_Relay_Chat) 也是一个很不错的资源。 [Freenode](https://freenode.net/) 上的 `#vim` 频道很庞大,并且里面有许多乐于助人的人。 963 | 964 | 如果你想给 Vim 提交 Bug 的话,可以使用 [vim_dev](https://groups.google.com/forum/#!forum/vim_dev) 邮件列表。 965 | 966 | ## 执行自动命令 967 | 968 | 你可以触发任何事件,如:`:doautocmd BufRead`。 969 | 970 | ### 用户自定义事件 971 | 972 | 对于插件而言,创建你自己的自定义事件有时非常有用。 973 | 974 | ```vim 975 | function! Chibby() 976 | " A lot of stuff is happening here. 977 | " And at last.. 978 | doautocmd User ChibbyExit 979 | endfunction 980 | ``` 981 | 982 | 现在你插件的用户可以在 Chibby 执行完成之后做任何他想做的事情: 983 | 984 | ```vim 985 | autocmd User ChibbyExit call ChibbyCleanup() 986 | ``` 987 | 988 | 顺便提一句,如果在使用 `:autocmd` 或 `:doautocmd` 时没有捕捉异常,那么会输出 "No matching autocommands" 信息。这也是为什么许多插件用 `silent doautocmd ...` 的原因。但是这也会有不足,那就是你不能再在 :autocmd 中使用 `echo "foo"` 了,取而代之的是你要使用 `unsilent echo "foo"` 来输出。 989 | 990 | 这就是为什么要在触发事件之前先判断事件是否存在的原因, 991 | 992 | ```vim 993 | if exists('#User#ChibbyExit') 994 | doautocmd User ChibbyExit 995 | endif 996 | ``` 997 | 998 | 帮助文档:`:h User` 999 | 1000 | ### 事件嵌套 1001 | 1002 | 默认情况下,自动命令不能嵌套!如果某个自动命令执行了一个命令,这个命令再依次触发其它的事件,这是不可能的。 1003 | 1004 | 例如你想在每次启动 Vim 的时候自动打开你的 vimrc 文件: 1005 | 1006 | ```vim 1007 | autocmd VimEnter * edit $MYVIMRC 1008 | ``` 1009 | 1010 | 当你启动 Vim 的时候,它会帮你打开你的 vimrc 文件,但是你很快会注意到这个文件没有任何的高亮,尽管平时它是正常可以高亮的。 1011 | 1012 | 问题在于你的非嵌套自动命令 `:edit` 不会触发“BufRead”事件,所以并不会把文件类型设置成“vim”,进而 `$VIMRUNTIME/syntax/vim.vim` 永远不会被引入。详细信息请参考:`:au BufRead *.vim`。要想完成上面所说的需求,使用下面这个命令: 1013 | 1014 | ```vim 1015 | autocmd VimEnter * nested edit $MYVIMRC 1016 | ``` 1017 | 1018 | 帮助文档:`:h autocmd-nested` 1019 | 1020 | ## 剪切板 1021 | 1022 | 如果你想在没有 GUI 支持的 Unix 系统中使用 Vim 的 `'clipboard'` 选项,则需要 `+clipboard` 以及可选的 `+xterm_clipboard` 两个[特性](#what-kind-of-vim-am-i-running)支持。 1023 | 1024 | 帮助文档: 1025 | 1026 | ```vim 1027 | :h 'clipboard' 1028 | :h gui-clipboard 1029 | :h gui-selections 1030 | ``` 1031 | 1032 | 另外请参考:[持续粘贴(为什么我每次都要设置 'paste' 模式](#持续粘贴为什么我每次都要设置-paste-模式) 1033 | 1034 | ### 剪贴板的使用(Windows, OSX) 1035 | 1036 | Windows 自带了[剪贴板](),OSX 则带了一个[粘贴板](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PasteboardGuide106/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008100-SW1) 1037 | 1038 | 在这两个系统中都可以用大家习惯用的 `ctrl+c / cmd+c` 复制选择的文本,然后在另外一个应用中用 `ctrl+v / cmd+v` 进行粘贴。 1039 | 1040 | 需要注意的是复制的文本已经被发送到了剪贴板,所以你在粘贴复制的内容之前关闭这个应用是没有任何问题的。 1041 | 1042 | 每次复制的时候,都会向剪贴板寄存器 `*` 中写入数据。 而在 Vim 中分别使用 `"*y` 和 `"*p` 来进行复制(yank) 和 粘贴(paste)。 1043 | 1044 | 如果你不想每次操作都要指定 `*` 寄存器,可以在你的 vimrc 中添加如下配置: 1045 | 1046 | ```vim 1047 | set clipboard=unnamed 1048 | ``` 1049 | 1050 | 通常情况下复制/删除/放入操作会往 `"` 寄存器中写入数据,而加上了上面的配置之后 `*` 寄存器也会被写入同样数据,因此简单的使用 `y` 和 `p` 就可以复制粘贴了。 1051 | 1052 | 我再说一遍:使用上面的选项意味着每一次的复制/粘贴,即使在同一个 Vim 窗口里,都会修改剪贴板的内容。你自己决定上面的选项是否适合。 1053 | 1054 | 如果你觉得输入 `y` 还是太麻烦的话,可以使用下面的设置把在可视模式下选择的内容发送到剪贴板: 1055 | 1056 | ```vim 1057 | set clipboard=unnamed,autoselect 1058 | set guioptions+=a 1059 | ``` 1060 | 1061 | 帮助文档: 1062 | 1063 | ```vim 1064 | :h clipboard-unnamed 1065 | :h autoselect 1066 | :h 'go_a' 1067 | ``` 1068 | 1069 | ### 剪贴板的使用(Linux, BSD, ...) 1070 | 1071 | 如果你的系统使用了 [X 图形界面](http://www.x.org/wiki),事情会变得有一点不同。X 图形界面实现了 [X 窗口系统协议](http://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html), 这个协议在 1987 年发布的主版本 11,因此 X 也通常被称为 X11。 1072 | 1073 | 在 X10 版本中,[剪贴缓冲区](http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html#Peer_to_Peer_Communication_by_Means_of_Cut_Buffers)被用来实现像 _clipboard_ 一样由 X 来复制文本,并且可以被所有的程序访问。现在这个机制在 X 中还存在,但是已经过时了,很多程序都不再使用这一机制。 1074 | 1075 | 近年来数据在程序之间是通过[选择](http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html#Peer_to_Peer_Communication_by_Means_of_Selections)进行传递的。一共有三种选择,经常用到的有两种:PRIMARY 和 CLIPBOARD。 1076 | 1077 | 选择的工作工模大致是这样的: 1078 | 1079 | Program A: 1080 | Program A:声称对 CLIPBOARD 的所有权 1081 | Program B: 1082 | Program B:发现CLIPBOARD的所有权被Program A持有 1083 | Program B:从Program A请求数据 1084 | Program A:响应这个请求并发送数据给Program B 1085 | Program B:从Program A接收数据并插入到窗口中 1086 | 1087 | | 选择 | 何时使用 | 如何粘贴 | 如何在 Vim 中访问 | 1088 | | --------- | --------------------- | ---------------------- | ----------------- | 1089 | | PRIMARY | 选择文本 | 鼠标中键, shift+insert | `*` 寄存器 | 1090 | | CLIPBOARD | 选择文本并按 `ctrl+c` | `ctrl+v` | `+`寄存器 | 1091 | 1092 | **注意**:X 服务器并不会保存选择(不仅仅是 CLIPBOARD 选择)!因此在关闭了相应的程序后,你用 `ctrl+c` 复制的内容将丢失。 1093 | 1094 | 使用 `"*p` 来贴粘 PRIMARY 选择中的内容,或者使用 `"+y1G` 来将整个文件的内容复制到 CLIPBOARD 选择。 1095 | 1096 | 如果你需要经常访问这两个寄存器,可以考虑使用如下配置: 1097 | 1098 | ```vim 1099 | set clipboard^=unnamed " * 寄存器 1100 | " 或者 1101 | set clipboard^=unnamedplus " + 寄存器 1102 | ``` 1103 | 1104 | (`^=` 用来将设置的值加到默认值之前,详见:`:h :set^=`) 1105 | 1106 | 这会使得所有复制/删除/放入操作使用 `*` 或 `+` 寄存器代替默认的未命令寄存器 `"`。之后你就可以直接使用 `y` 或 `p` 访问你的 X 选择了。 1107 | 1108 | 帮助文档: 1109 | 1110 | ```vim 1111 | :h clipboard-unnamed 1112 | :h clipboard-unnamedplus 1113 | ``` 1114 | 1115 | ## 打开文件时恢复光标位置 1116 | 1117 | 如果没有这个设置,每次打开文件时光标都将定位在第一行。而加入了这个设置以后,你就可以恢复到上次关闭文件时光标所在的位置了。 1118 | 1119 | 将下面的配置添加到你的 vimrc 文件: 1120 | 1121 | ```vim 1122 | autocmd BufReadPost * 1123 | \ if line("'\"") > 1 && line("'\"") <= line("$") | 1124 | \ exe "normal! g`\"" | 1125 | \ endif 1126 | ``` 1127 | 1128 | 这是通过判断之前的光标位置是否存在(文件可能被其它程序修改而导致所记录的位置已经不存在了),如果存在的话就执行 `` g`" `` (转到你离开时的光标位置但是不更改跳转列表)。 1129 | 1130 | 这需要使用 viminfo 文件:`:h viminfo-`。 1131 | 1132 | ## 临时文件 1133 | 1134 | 根据选项的不同, Vim 最多会创建 4 种工作文件。 1135 | 1136 | ### 备份文件 1137 | 1138 | 你可以让 Vim 在将修改写入到文件之前先备份原文件。默认情况下, Vim 会保存一个备份文件但是当修改成功写入后会立即删除它(`:set writebackup`)。如果你想一直保留这个备份文件的话,可以使用 `:set backup`。而如果你想禁用备份功能的话,可以使用 `:set nobackup nowritebackup`。 1139 | 1140 | 咱们来看一下上次我在 vimrc 中改了什么: 1141 | 1142 | ```sh 1143 | $ diff ~/.vim/vimrc ~/.vim/files/backup/vimrc-vimbackup 1144 | 390d389 1145 | < command! -bar -nargs=* -complete=help H helpgrep 1146 | ``` 1147 | 1148 | 帮助文档:`:h backup` 1149 | 1150 | ### 交换文件 1151 | 1152 | 假设你有一个非常棒的科幻小说的构思。在按照故事情节已经写了好几个小时几十万字的时候..忽然停电了!而那时你才想起来你上次保存 `~/来自外太空的邪恶入侵者.txt` 是在.. 好吧,你从来没有保存过。 1153 | 1154 | 但是并非没有希望了!在编辑某个文件的时候, Vim 会创建一个交换文件,里面保存的是对当前文件所有未保存的修改。自己试一下,打开任意的文件,并使用 `:swapname` 获得当前的交换文件的保存路径。你也可以将 `:set noswapfile` 加入到 vimrc 中来禁用交换文件。 1155 | 1156 | 默认情况下,交换文件会自动保存在被编辑文件所在的目录下,文件名以 `.file.swp` 后缀结尾,每当你修改了超过 200 个字符或是在之前 4 秒内没有任何动作时更新它的内容,在你不再编辑这个文件的时候会被删除。你可以自己修改这些数字,详见:`:h 'updatecount'` 和 `:h 'updatetime'`。 1157 | 1158 | 而在断电时,交换文件并不会被删除。当你再次打开 `vim ~/来自外太空的邪恶入侵者.txt` 时, Vim 会提示你恢复这个文件。 1159 | 1160 | 帮助文档:`:h swap-file` 和 `:h usr_11` 1161 | 1162 | ### 撤销文件 1163 | 1164 | [内容变更历史记录](#%E5%86%85%E5%AE%B9%E5%8F%98%E6%9B%B4%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95)是保存在内存中的,并且会在 Vim 退出时清空。如果你想让它持久化到磁盘中,可以设置 `:set undofile`。这会把文件 `~/foo.c` 的撤销文件保存在 `~/foo.c.un~`。 1165 | 1166 | 帮助文档:`:h 'undofile'` 和 `:h undo-persistence` 1167 | 1168 | ### viminfo 文件 1169 | 1170 | 备份文件、交换文件和撤销文件都是与文本状态相关的,而 viminfo 文件是用来保存在 Vim 退出时可能会丢失的其它的信息的。包括历史记录(命令历史、搜索历史、输入历史)、寄存器内容、标注、缓冲区列表、全局变量等等。 1171 | 1172 | 默认情况下,viminfo 被保存在 `~/.viminfo`。 1173 | 1174 | 帮助文档:`:h viminfo` 和 `:h 'viminfo'` 1175 | 1176 | ### 临时文件管理设置示例 1177 | 1178 | 如果你跟我一样,也喜欢把这些文件放到一个位置(如:`~/.vim/files`)的话,可以使用下面的配置: 1179 | 1180 | ```vim 1181 | " 如果文件夹不存在,则新建文件夹 1182 | if !isdirectory($HOME.'/.vim/files') && exists('*mkdir') 1183 | call mkdir($HOME.'/.vim/files') 1184 | endif 1185 | 1186 | " 备份文件 1187 | set backup 1188 | set backupdir =$HOME/.vim/files/backup/ 1189 | set backupext =-vimbackup 1190 | set backupskip = 1191 | " 交换文件 1192 | set directory =$HOME/.vim/files/swap// 1193 | set updatecount =100 1194 | " 撤销文件 1195 | set undofile 1196 | set undodir =$HOME/.vim/files/undo/ 1197 | " viminfo 文件 1198 | set viminfo ='100,n$HOME/.vim/files/info/viminfo 1199 | ``` 1200 | 1201 | 注意:如果你在一个多用户系统中编辑某个文件时, Vim 提示你交换文件已经存在的话,可能是因为有其他的用户此时正在编辑这个文件。而如果将交换文件放到自己的 home 目录的话,这个功能就失效了。因此服务器非常不建议将这些文件修改到 HOME 目录,避免多人同时编辑一个文件,却没有任何警告。 1202 | 1203 | ## 编辑远程文件 1204 | 1205 | Vim 自带的 netrw 插件支持对远程文件的编辑。实际上它将远程的文件通过 scp 复制到本地的临时文件中,再用那个文件打开一个缓冲区,然后在保存时把文件再复制回远程位置。 1206 | 1207 | 下面的命令在你本地的 VIM 配置与 SSH 远程服务器上管理员想让你使用的配置有冲突时尤其有用: 1208 | 1209 | ```vim 1210 | :e scp://bram@awesome.site.com/.vimrc 1211 | ``` 1212 | 1213 | 如果你已经设置了 `~/.ssh/config`,SSH 会自动读取这里的配置: 1214 | 1215 | Host awesome 1216 | HostName awesome.site.com 1217 | Port 1234 1218 | User bram 1219 | 1220 | 如果你的 `~/.ssh/config` 中有以上的内容,那么下面的命令就可以正常执行了: 1221 | 1222 | ```vim 1223 | :e scp://awesome/.vimrc 1224 | ``` 1225 | 1226 | 可以用同样的方法编辑 `~/.netrc`, 详见:`:h netrc-netrc`。 1227 | 1228 | 确保你已经看过了 `:h netrw-ssh-hack` 和 `:h g:netrw_ssh_cmd`。 1229 | 1230 | 另外一种编辑远程文件的方法是使用 [sshfs](https://wiki.archlinux.org/index.php/Sshfs),它会用 [FUSE](https://en.wikipedia.org/wiki/Filesystem_in_Userspace) 来挂载远程的文件系统到你本地的系统当中。 1231 | 1232 | ## 插件管理 1233 | 1234 | [Pathogen](https://github.com/tpope/vim-pathogen)是第一个比较流行的插件管理工具。实际上它只是修改了 _runtimepath_ (`:h 'rtp'`) 来引入所有放到该目录下的文件。你需要自己克隆插件的代码仓库到那个目录。 1235 | 1236 | 真正的插件管理工具会在 Vim 中提供帮助你安装或更新插件的命令。以下是一些常用的插件管理工具: 1237 | 1238 | - [dein](https://github.com/Shougo/dein.vim) 1239 | - [plug](https://github.com/junegunn/vim-plug) 1240 | - [vim-addon-manager](https://github.com/MarcWeber/vim-addon-manager) 1241 | - [vundle](https://github.com/VundleVim/Vundle.vim) 1242 | 1243 | ## 多行编辑 1244 | 1245 | 这是一种可以同时输入多行连续文本的技术。参考这个[示例](https://raw.githubusercontent.com/mhinz/vim-galore/master/contents/images/content-block_insert.gif)。 1246 | 1247 | 用 `` 切换到可视块模式。然后向下选中几行,按 `I` 或 `A` (译者注:大写字母,即 shift+i 或 shift+a)然后开始输入你想要输入的文本。 1248 | 1249 | 在刚开始的时候可能会有些迷惑,因为文本只出现在了当前编辑的行,只有在当前的插入动作结束后,之前选中的其它行才会出现插入的文本。 1250 | 1251 | 举一个简单的例子:`3jItext`。 1252 | 1253 | 如果你要编辑的行长度不同,但是你想在他们后面追加相同的内容的话,可以试一下这个:`3j$Atext`。 1254 | 1255 | 有时你可能需要把光标放到当前行末尾之后,默认情况下你是不可能做到的,但是可能通过设置 `virtualedit` 选项达到目的: 1256 | 1257 | ```vim 1258 | set virtualedit=all 1259 | ``` 1260 | 1261 | 设置之后 `$10l` 或 `90|` 都会生效,即使超过了行尾的长度。 1262 | 1263 | 详见 `:h blockwise-examples`。在开始的时候可能会觉得有些复杂,但是它很快就会成为你的第二天性的。 1264 | 1265 | 如果你想探索更有趣的事情,可以看看[多光标](https://github.com/terryma/vim-multiple-cursors) 1266 | 1267 | ## 使用外部程序和过滤器 1268 | 1269 | 免责声明:Vim 是单线程的,因此在 Vim 中以前端进程执行其它的程序时会阻止其它的一切。当然你可以使用 Vim 程序接口,如 Lua,并且使用它的多线程支持,但是在那期间, Vim 的处理还是被阻止了。Neovim 添加了任务 API 解决了此问题。 1270 | 1271 | (据说 Bram 正在考虑在 Vim 中也添加任务控制。如果你使用了较新版本的的 Vim ,可以看一下 `:helpgrep startjob`。) 1272 | 1273 | 使用 `:!` 启动一个新任务。如果你想列出当前工作目录下的所有文件,可以使用 `:!ls`。 用 `|` 来将结果通过管道重定向,如:`:!ls -l | sort | tail -n5`。 1274 | 1275 | 没有使用范围时(译者注:范围就是 `:` 和 `!` 之间的内容,`.` 表示当前行,`+4` 表示向下偏移 4 行,`$` 表示最末行等,多行时用 `,` 将它们分开,如 `.,$` 表示从当前行到末行),`:!` 会显示在一个可滚动的窗口中(译者注:在 GVim 和在终端里运行的结果稍有不同)。相反的,如果指定了范围,这些行会被[过滤]()。这意味着它们会通过管道被重定向到过滤程序的 [stdin](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29),在处理后再通过过滤程序的 [stdout](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29) 输出,用输出结果替换范围内的文本。例如:为接下来的 5 行文本添加行号,可以使用: 1276 | 1277 | ```vim 1278 | :.,+4!nl -ba -w1 -s' ' 1279 | ``` 1280 | 1281 | 由于手动添加范围很麻烦, Vim 提供了一些辅助方法以方便的添加范围。如果需要经常带着范围的话,你可以在可见模式中先选择,然后再按 `:` (译者注:选中后再按 `!` 更方便)。还可以使用 `!` 来取用一个 motion 的范围,如 `!ipsort` (译者注:原文为 `!ip!sort` ,但经过实验发现该命令执行报错,可能是因为 Vim 版本的原因造成的,新版本使用 `ip` 选择当前段落后自动在命令后添加了 `!` ,按照作者的写法来看,可能之前的版本没有自动添加 `!` )可以将当前段落的所有行按字母表顺序进行排序。 1282 | 1283 | 一个使用过滤器比较好的案例是[Go 语言](https://golang.org/)。它的缩进语法非常个性,甚至还专门提供了一个名为 `gofmt` 的过滤器来对 Go 语言的源文件进行正确的缩进。Go 语言的插件通常会提供一个名为 `:Fmt` 的函数,这个函数就是执行了 `:%!gofmt` 来对整个文件进行缩进。 1284 | 1285 | 人们常用 `:r !prog` 将 prog 程序的插入放到当前行的下面,这对于脚本来说是很不错的选择,但是在使用的过程中我发现 `!!ls` 更加方便,它会用输出结果替换当前行的内容。(译者注:前面命令中的 `prog` 只是个占位符,在实际使用中需要替换成其它的程序,如 `:r !ls`,这就与后面的 `!!ls` 相对应了,两者唯一的不同是第一个命令不会覆盖当前行内容,但是第二个命令会) 1286 | 1287 | 帮助文档: 1288 | 1289 | ```vim 1290 | :h filter 1291 | :h :read! 1292 | ``` 1293 | 1294 | ## Cscope 1295 | 1296 | [Cscope](http://cscope.sourceforge.net/) 的功能比 [ctags](http://ctags.sourceforge.net/) 要完善,但是只支持 C(通过设置 cscope.files 后同样支持 C++以及 Java)。 1297 | 1298 | 鉴于 Tag 文件只是知道某个符号是在哪里定义的,cscope 的数据库里的数据信息就多的多了: 1299 | 1300 | - 符号是在哪里定义的? 1301 | - 符号是在哪里被使用的? 1302 | - 这个全局符号定义了什么? 1303 | - 这个变量是在哪里被赋值的? 1304 | - 这个函数在源文件的哪个位置? 1305 | - 哪些函数调用了这个函数? 1306 | - 这个函数调用了哪些函数? 1307 | - "out of space"消息是从哪来的? 1308 | - 在目录结构中当前的源文件在哪个位置? 1309 | - 哪些文件引用了这个头文件? 1310 | 1311 | ### 1. 构建数据库 1312 | 1313 | 在你项目的根目录执行下面的命令: 1314 | 1315 | ```sh 1316 | $ cscope -bqR 1317 | ``` 1318 | 1319 | 这条命令会在当前目录下创建三个文件:`cscope{,.in,.po}.out` 。把它们想象成你的数据库。 1320 | 1321 | 不幸的时 `cscope` 默认只分析 `*.[c|h|y|l]` 文件。如果你想在 Java 项目中使用 cscope ,需要这样做: 1322 | 1323 | ```sh 1324 | $ find . -name "*.java" > cscope.files 1325 | $ cscope -bq 1326 | ``` 1327 | 1328 | ### 2. 添加数据库 1329 | 1330 | 打开你新创建的数据库连接: 1331 | 1332 | ```vim 1333 | :cs add cscope.out 1334 | ``` 1335 | 1336 | 检查连接已经创建成功: 1337 | 1338 | ```vim 1339 | :cs show 1340 | ``` 1341 | 1342 | (当然你可以添加多个连接。) 1343 | 1344 | ### 3. 查询数据库 1345 | 1346 | ```vim 1347 | :cs find 1348 | ``` 1349 | 1350 | 如:`:cs find d foo` 会列出 `foo(...)` 调用的所有函数。 1351 | 1352 | | Kind | 说明 | 1353 | | ---- | -------------------------------------- | 1354 | | s | **s**ymbol:查找使用该符号的引用 | 1355 | | g | **g**lobal:查找该全局符号的定义 | 1356 | | c | **c**alls:查找调用当前方法的位置 | 1357 | | t | **t**ext:查找出现该文本的位置 | 1358 | | e | **e**grep:使用 egrep 搜索当前单词 | 1359 | | f | **f**ile:打开文件名 | 1360 | | i | **i**ncludes:查询引入了当前文件的文件 | 1361 | | d | **d**epends:查找当前方法调用的方法 | 1362 | 1363 | 推荐一些比较方便的映射,如: 1364 | 1365 | ```vim 1366 | nnoremap cs :cscope find s =expand('') 1367 | nnoremap cg :cscope find g =expand('') 1368 | nnoremap cc :cscope find c =expand('') 1369 | nnoremap ct :cscope find t =expand('') 1370 | nnoremap ce :cscope find e =expand('') 1371 | nnoremap cf :cscope find f =expand('') 1372 | nnoremap ci :cscope find i ^=expand('')$ 1373 | nnoremap cd :cscope find d =expand('') 1374 | ``` 1375 | 1376 | 所以 `:tag` (或 ``)跳转到标签定义的文件,而 `:cstag` 可以达到同样的目的,同时还会打开 cscope 的数据库连接。`'cscopetag'` 选项使得 `:tag` 命令自动的像 `:cstag` 一样工作。这在你已经使用了基于标签的映射时会非常方便。 1377 | 1378 | 帮助文档:`:h cscope` 1379 | 1380 | ## MatchIt 1381 | 1382 | 由于 Vim 是用 C 语言编写的,因此许多功能都假设使用类似 C 语言的语法。默认情况下,如果你的光标在 `{` 或 `#endif` , 就可以使用 `%` 跳转到与之匹配的 `}` 或 `#ifdef`。 1383 | 1384 | Vim 自带了一个名为 matchit.vim 的插件,但是默认没有启用。启用后可以用 `%` 在 HTML 相匹配的标签或 VimL 的 if/else/endif 块之间进行跳转,它还带来了一些新的命令。 1385 | 1386 | ### 在 Vim 8 中安装 1387 | 1388 | ```vim 1389 | " vimrc 1390 | packadd! matchit 1391 | ``` 1392 | 1393 | ### 在 Vim 7 或者更早的版本中安装 1394 | 1395 | ```vim 1396 | "vimrc 1397 | runtime macros/matchit.vim 1398 | ``` 1399 | 1400 | 由于 matchit 的文档很全面,我建议安装以后执行一次下面的命令: 1401 | 1402 | ```vim 1403 | :!mkdir -p ~/.vim/doc 1404 | :!cp $VIMRUNTIME/macros/matchit.vim ~/.vim/doc 1405 | :helptags ~/.vim/doc 1406 | ``` 1407 | 1408 | ### 简短的介绍 1409 | 1410 | 至此这个插件已经可以使用了。 参考 `:h matchit-intro` 来获得支持的命令以及 `:h matchit-languages` 来获得支持的语言。 1411 | 1412 | 你可以很方便的定义自己的匹配对,如: 1413 | 1414 | ```vim 1415 | autocmd FileType python let b:match_words = '\:\:\' 1416 | ``` 1417 | 1418 | 之后你就可以在任何的 Python 文件中使用 `%` (向前)或 `g%` (向后)在这三个片断之间跳转了。 1419 | 1420 | 帮助文档: 1421 | 1422 | ```vim 1423 | :h matchit-install 1424 | :h matchit 1425 | :h b:match_words 1426 | ``` 1427 | 1428 | # 技巧 1429 | 1430 | ## 聪明地使用 n 和 N 1431 | 1432 | nN 的实际跳转方向取决于使用 `/` 还是 `?` 来执行搜索,其中 `/` 是向后搜索,`?` 是向前搜索。一开始我(原作者)觉得这里很难理解。 1433 | 1434 | 如果你希望 n 始终为向后搜索,N 始终为向前搜索,那么只需要这样设置: 1435 | 1436 | ```vim 1437 | nnoremap n 'Nn'[v:searchforward] 1438 | nnoremap N 'nN'[v:searchforward] 1439 | ``` 1440 | 1441 | ## 聪明地使用命令行历史 1442 | 1443 | 我(原作者)习惯用 Ctrl + pCtrl + n 来跳转到上一个/下一个条目。其实这个操作也可以用在命令行中,快速调出之前执行过的命令。 1444 | 1445 | 不仅如此,你会发现 其实更智能。如果命令行中已经存在了一些文字,我们可以通过按方向键来匹配已经存在的内容。比如,命令行中现在是 `:echo`,这时候我们按 ,就会帮我们补全成 `:echo "Vim rocks!"`(前提是,之前输入过这段命令)。 1446 | 1447 | 当然,Vim 用户都不愿意去按方向键,事实上我们也不需要去按,只需要设置这样的映射: 1448 | 1449 | ```vim 1450 | cnoremap 1451 | cnoremap 1452 | ``` 1453 | 1454 | 这个功能,我(原作者)每天都要用很多次。 1455 | 1456 | ## 智能 Ctrl-l 1457 | 1458 | Ctrl + l 的默认功能是清空并「重新绘制」当前的屏幕,就和 `:redraw!` 的功能一样。下面的这个映射就是执行重新绘制,并且取消通过 `/` 和 `?` 匹配字符的高亮,而且还可以修复代码高亮问题(有时候,由于多个代码高亮的脚本重叠,或者规则过于复杂,Vim 的代码高亮显示会出现问题)。不仅如此,还可以刷新「比较模式」(请参阅 `:help diff-mode`)的代码高亮: 1459 | 1460 | ```vim 1461 | nnoremap l :nohlsearch:diffupdate:syntax sync fromstart 1462 | ``` 1463 | 1464 | ## 禁用错误报警声音和图标 1465 | 1466 | ```vim 1467 | set noerrorbells 1468 | set novisualbell 1469 | set t_vb= 1470 | ``` 1471 | 1472 | 请参阅 [Vim Wiki: Disable beeping](http://vim.wikia.com/wiki/Disable_beeping)。 1473 | 1474 | ## 快速移动当前行 1475 | 1476 | 有时,我(原作者)想要快速把当前行上移或下移一行,只需要这样设置映射: 1477 | 1478 | ```vim 1479 | nnoremap [e :execute 'move -1-'. v:count1 1480 | nnoremap ]e :execute 'move +'. v:count1 1481 | ``` 1482 | 1483 | 这个映射,同样可以搭配数字使用,比如连续按下 2 ] e 就可以把当前行向下移动两行。 1484 | 1485 | ## 快速添加空行 1486 | 1487 | ```vim 1488 | nnoremap [ :put! =repeat(nr2char(10), v:count1)'[ 1489 | nnoremap ] :put =repeat(nr2char(10), v:count1) 1490 | ``` 1491 | 1492 | 设置之后,连续按下 5 \[ 空格 在当前行上方插入 5 个空行。 1493 | 1494 | ### 运行时检测 1495 | 1496 | 需要的特性:+profile 1497 | 1498 | Vim 提供了一个内置的运行时检查功能,能够找出运行慢的代码。 1499 | 1500 | `:profile` 命令后面跟着子命令来确定要查看什么。 1501 | 1502 | 如果你想查看所有的: 1503 | 1504 | ```Vim 1505 | :profile start /tmp/profile.log 1506 | :profile file * 1507 | :profile func * 1508 | 1509 | 1510 | ``` 1511 | 1512 | Vim 不断地在内存中检查信息,只在退出的时候输出出来。(Neovim 已经解决了这个问题用 `:profile dump` 命令) 1513 | 1514 | 看一下 `/tmp/profile.log` 文件,检查时运行的所有代码都会被显示出来,包括每一行代码运行的频率和时间。 1515 | 1516 | 大多数代码都是用户不熟悉的插件代码,如果你是在解决一个确切的问题, 1517 | 直接跳到这个日志文件的末尾,那里有 `FUNCTIONS SORTED ON TOTAL TIME` 和 `FUNCTIONS SORTED ON SELF TIME` 两个部分,如果某个 function 运行时间过长一眼就可以看到。 1518 | 1519 | ### 查看启动时间 1520 | 1521 | 感觉 Vim 启动的慢?到了研究几个数字的时候了: 1522 | 1523 | ```vim 1524 | vim --startuptime /tmp/startup.log +q && vim /tmp/startup.log 1525 | ``` 1526 | 1527 | 第一栏是最重要的因为它显示了**绝对运行时间**,如果在前后两行之间时间差有很大的跳跃,那么是第二个文件太大或者含有需要检查的错误的 VimL 代码。 1528 | 1529 | ## NUL 符用新行表示 1530 | 1531 | 文件中的 NUL 符 (`\0`),在内存中被以新行(`\n`)保存,在缓存空间中显示为 `^@`。 1532 | 1533 | 更多信息请参看 `man 7 ascii` 和 `:h NL-used-for-Nul` 。 1534 | 1535 | ## 快速编辑自定义宏 1536 | 1537 | 这个功能真的很实用!下面的映射,就是在一个新的命令行窗口中读取某一个寄存器(默认为 `*`)。当你设置完成后,只需要按下 回车 即可让它生效。 1538 | 1539 | 在录制宏的时候,我经常用这个来更改拼写错误。 1540 | 1541 | ```vim 1542 | nnoremap m :='let @'. v:register .' = '. string(getreg(v:register)) 1543 | ``` 1544 | 1545 | 只需要连续按下 leader m 或者 " leader m 就可以调用了。 1546 | 1547 | 请注意,这里之所以要写成 `` 是为了确保 `` 执行了。请参阅 `:h c_^R^R` 1548 | 1549 | ## 快速跳转到源(头)文件 1550 | 1551 | 这个技巧可以用在多种文件类型中。当你从源文件或者头文件中切换到其他文件的时候,这个技巧可以设置「文件标记」(请参阅 `:h marks`),然后你就可以通过连续按下 ' C 或者 ' H 快速跳转回去(请参阅 `:h 'A`)。 1552 | 1553 | ```vim 1554 | autocmd BufLeave *.{c,cpp} mark C 1555 | autocmd BufLeave *.h mark H 1556 | ``` 1557 | 1558 | **注意**:由于这个标记是设置在 viminfo 文件中,因此请先确认 `:set viminfo?` 中包含了 `:h viminfo-'`。 1559 | 1560 | ## 在 GUI 中快速改变字体大小 1561 | 1562 | 印象中,我(原作者)记得一下代码是来自 tpope's 的配置文件: 1563 | 1564 | ```vim 1565 | command! Bigger :let &guifont = substitute(&guifont, '\d\+$', '\=submatch(0)+1', '') 1566 | command! Smaller :let &guifont = substitute(&guifont, '\d\+$', '\=submatch(0)-1', '') 1567 | ``` 1568 | 1569 | ## 根据模式改变光标类型 1570 | 1571 | 我(原作者)习惯在普通模式下用块状光标,在插入模式下用条状光标(形状类似英文 "I" 的样子),然后在替换模式中使用下划线形状的光标。 1572 | 1573 | ```vim 1574 | if empty($TMUX) 1575 | let &t_SI = "\]50;CursorShape=1\x7" 1576 | let &t_EI = "\]50;CursorShape=0\x7" 1577 | let &t_SR = "\]50;CursorShape=2\x7" 1578 | else 1579 | let &t_SI = "\Ptmux;\\]50;CursorShape=1\x7\\\" 1580 | let &t_EI = "\Ptmux;\\]50;CursorShape=0\x7\\\" 1581 | let &t_SR = "\Ptmux;\\]50;CursorShape=2\x7\\\" 1582 | endif 1583 | ``` 1584 | 1585 | 原理很简单,就是让 Vim 在进入和离开插入模式的时候,输出一些序列,请参考 [escape sequence](https://en.wikipedia.org/wiki/Escape_sequence)。Vim 与终端之间的中间层,比如 [tmux](https://tmux.github.io) 会处理并执行上面的代码。 1586 | 1587 | 但上面这个还是有一个缺点的。终端环境的内部原理不尽相同,对于序列的处理方式也稍有不同。因此,上面的代码可能无法在你的环境中运行。甚至,你的运行环境也有可能不支持其他光标形状,请参阅你的 Vim 运行环境的文档。 1588 | 1589 | 好消息是,上面这个代码,可以在 iTerm2 中完美运行。 1590 | 1591 | ## 防止水平滑动的时候失去选择 1592 | 1593 | 如果你选中了一行或多行,那么你可以用 <> 来调整他们的缩进。但在调整之后就不会保持选中状态了。 1594 | 1595 | 你可以连续按下 g v 来重新选中他们,请参考 `:h gv`。因此,你可以这样来配置映射: 1596 | 1597 | ```vim 1598 | xnoremap < >gv 1600 | ``` 1601 | 1602 | 设置好之后,在可视模式中使用 `>>>>>` 就不会再出现上面提到的问题了。 1603 | 1604 | ## 重新载入保存文件 1605 | 1606 | 通过[自动命令](#自动命令),你可以在保存文件的同时触发一些其他功能。比如,如果这个文件是一个配置文件,那么就重新载入;或者你还可以对这个文件进行代码风格检查。 1607 | 1608 | ```vim 1609 | autocmd BufWritePost $MYVIMRC source $MYVIMRC 1610 | autocmd BufWritePost ~/.Xdefaults call system('xrdb ~/.Xdefaults') 1611 | ``` 1612 | 1613 | ## 更加智能的当前行高亮 1614 | 1615 | 我(原作者)很喜欢「当前行高亮」(请参阅 `:h cursorline`)这个功能,但我只想让这个效果出现在当前窗口,而且在插入模式中关闭这个效果: 1616 | 1617 | ```vim 1618 | autocmd InsertLeave,WinEnter * set cursorline 1619 | autocmd InsertEnter,WinLeave * set nocursorline 1620 | ``` 1621 | 1622 | ## 更快的关键字补全 1623 | 1624 | 关键字补全(`` 或 ``)功能的工作方式是,无论 `'complete'` 设置中有什么,它都会尝试着去补全。这样,一些我们用不到的标签也会出现在补全列表中。而且,它会扫描很多文件,有时候运行起来非常慢。如果你不需要这些,那么完全可以像这样把它们禁用掉: 1625 | 1626 | ```vim 1627 | set complete-=i " disable scanning included files 1628 | set complete-=t " disable searching tags 1629 | ``` 1630 | 1631 | ## 改变颜色主题的默认外观 1632 | 1633 | 如果你想让状态栏在颜色主题更改后依然保持灰色,那么只需要这样设置: 1634 | 1635 | ```vim 1636 | autocmd ColorScheme * highlight StatusLine ctermbg=darkgray cterm=NONE guibg=darkgray gui=NONE 1637 | ``` 1638 | 1639 | 同理,如果你想让某一个颜色主题(比如 "lucius")的状态栏为灰色(请使用 `:echo color_name` 来查看当前可用的所有颜色主题): 1640 | 1641 | ```vim 1642 | autocmd ColorScheme lucius highlight StatusLine ctermbg=darkgray cterm=NONE guibg=darkgray gui=NONE 1643 | ``` 1644 | 1645 | ## 命令 1646 | 1647 | 下面的命令都比较有用,最好了解一下。用 `:h :` 来了解更多关于它们的信息,如:`:h :global`。 1648 | 1649 | ### :global 和 :vglobal - 在所有匹配行执行命令 1650 | 1651 | 在所有符合条件的行上执行某个命令。如: `:global /regexp/ print` 会在所有包含 "regexp" 的行上执行 `print` 命令(译者注:regexp 有正则表达式的意思,该命令同样支持正则表达式,在所有符合正则表达式的行上执行指定的命令)。 1652 | 1653 | 趣闻:你们可能都知道老牌的 grep 命令,一个由 Ken Thompson 编写的过滤程序。它是干什么用的呢?它会输出所有匹配指定正则表达式的行!现在猜一下 `:global /regexp/ print` 的简写形式是什么?没错!就是 `:g/re/p` 。 Ken Thompsom 在编写 grep 程序的时候是受了 vi `:global` 的启发。(译者注: 1654 | 1655 | 既然它的名字是 `:global`,理应仅作用在所有行上,但是它也是可以带范围限制的。假设你想使用 `:delete` 命令删除从当前行到下一个空行(由正则表达式 `^$` 匹配)范围内所有包含 "foo" 的行: 1656 | 1657 | ```vim 1658 | :,/^$/g/foo/d 1659 | ``` 1660 | 1661 | 如果要在所有 _不_ 匹配的行上执行命令的话,可以使用 `:global!` 或是它的别名 `:vglobal` ( V 代表的是 inVerse )。 1662 | 1663 | ### :normal 和 :execute - 脚本梦之队 1664 | 1665 | 这两个命令经常在 Vim 的脚本里使用。 1666 | 1667 | 借助于 `:normal` 可以在命令行里进行普通模式的映射。如:`:normal! 4j` 会令光标下移 4 行(由于加了"!",所以不会使用自定义的映射 "j")。 1668 | 1669 | 需要注意的是 `:normal` 同样可以使用范围数(译者注:参考 `:h range` 和 `:h :normal-range` 了解更多),故 `:%norm! Iabc` 会在所有行前加上 "abc"。 1670 | 1671 | 借助于 `:execute` 可以将命令和表达式混合在一起使用。假设你正在编辑一个 C 语言的文件,想切换到它的头文件: 1672 | 1673 | ```vim 1674 | :execute 'edit' fnamemodify(expand('%'), ':r') . '.h' 1675 | ``` 1676 | 1677 | (译者注:头文件为与与源文件同名但是扩展名为 `.h` 的文件。上面的命令中 expand 获得当前文件的名称,fnamemodify 获取不带扩展名的文件名,再连上 '.h' 就是头文件的文件名了,最后在使用 edit 命令打开这个头文件。) 1678 | 1679 | 这两个命令经常一起使用。假设你想让光标下移 n 行: 1680 | 1681 | ```vim 1682 | :let n = 4 1683 | :execute 'normal!' n . 'j' 1684 | ``` 1685 | 1686 | ### 重定向消息 1687 | 1688 | 许多命令都会输出消息,`:redir` 用来重定向这些消息。它可以将消息输出到文件、[寄存器](#寄存器)或是某个变量中。 1689 | 1690 | ```vim 1691 | " 将消息重定向到变量 `neatvar` 中 1692 | :redir => neatvar 1693 | " 打印所有寄存器的内容 1694 | :reg 1695 | " 结束重定向 1696 | :redir END 1697 | " 输出变量 1698 | :echo neatvar 1699 | " 恶搞一下,我们把它输出到当前缓冲区 1700 | :put =neatvar 1701 | ``` 1702 | 1703 | 再 Vim 8 中,可以更简单的方式即位: 1704 | 1705 | :put =execute('reg') 1706 | 1707 | (译者注:原文最后一条命令是 `:put =nicevar` 但是实际会报变量未定义的错误) 1708 | (实测 neovim/vim8 下没问题) 1709 | 1710 | 帮助文档:`:h :redir` 1711 | 1712 | # 调试 1713 | 1714 | ## 常规建议 1715 | 1716 | 如果你遇到了奇怪的行为,尝试用这个命令重现它: 1717 | 1718 | vim -u NONE -N 1719 | 1720 | 这样会在不引用 vimrc(默认设置)的情况下重启 vim,并且在 **nocompatible** 模式下(使用 vim 默认设置而不是 vi 的)。(搜索 `:h --noplugin` 命令了解更多启动加载方式) 1721 | 1722 | 如果仍旧能够出现该错误,那么这极有可能是 vim 本身的 bug,请给 [vim_dev]("https://groups.google.com/forum/#!forum/vim_dev") 发送邮件反馈错误,多数情况下问题不会立刻解决,你还需要进一步研究 1723 | 1724 | 许多插件经常会提供新的(默认的/自动的)操作。如果在保存的时候发生了,那么请用 `:verb au BufWritePost` 命令检查潜在的问题 1725 | 1726 | 如果你在使用一个插件管理工具,将插件行注释调,再进行调试。 1727 | 1728 | 问题还没有解决?如果不是插件的问题,那么肯定是你的自定义的设置的问题,可能是你的 options 或 autocmd 等等。 1729 | 1730 | 到了一行行代码检查的时候了,不断地排除缩小检查范围知道你找出错误,根据二分法的原理你不会花费太多时间的。 1731 | 1732 | 在实践过程中,可能就是这样,把 `:finish` 放在你的 **vimrc** 文件中间,Vim 会跳过它之后的设置。如果问题还在,那么问题就出在`:finish`之前的设置中,再把`:finish`放到前一部分设置的中间位置。否则问题就出现在它后面的半部分设置,那么就把`:finish`放到后半部分的中间位置。不断的重复即可找到。 1733 | 1734 | ## 调整日志等级 1735 | 1736 | Vim 现在正在使用的另一个比较有用的方法是增加 debug 信息输出详细等级。现在 Vim 支持 9 个等级,可以用`:h 'verbose'`命令查看。 1737 | 1738 | ```vim 1739 | :e /tmp/foo 1740 | :set verbose=2 1741 | :w 1742 | :set verbose=0 1743 | ``` 1744 | 1745 | 这可以显示出所有引用的文件、没有变化的文件或者各种各样的作用于保存的插件。 1746 | 1747 | 如果你只是想用简单的命令来提高等级,也是用 `:verbose` ,放在其他命令之前,通过计数来指明等级,默认是 1. 1748 | 1749 | ```vim 1750 | :verb set verbose 1751 | " verbose=1 1752 | :10verb set verbose 1753 | " verbose=10 1754 | ``` 1755 | 1756 | 通常用等级 1 来显示上次从哪里设置的选项 1757 | 1758 | ```vim 1759 | :verb set ai? 1760 | " Last set from ~/.vim/vimrc 1761 | ``` 1762 | 1763 | 一般等级越高输出信息月详细。但是不要害怕,亦可以把输出导入到文件中: 1764 | 1765 | ```vim 1766 | :set verbosefile=/tmp/foo | 15verbose echo "foo" | vsplit /tmp/foo 1767 | ``` 1768 | 1769 | 你可以一开始的时候就打开 verbosity,用 `-V` 选项,它默认设置调试等级为 10。 例如:`vim -V5` 1770 | 1771 | ## 查看启动日志 1772 | 1773 | ## 查看运行时日志 1774 | 1775 | ## Vim 脚本调试 1776 | 1777 | 如果你以前使用过命令行调试器的话,对于`:debug`命令你很快就会感到熟悉。 1778 | 1779 | 只需要在任何其他命令之前加上`:debug`就会让你进入调试模式。也就是,被调试的 Vim 脚本会在第一行停止运行,同时该行会被显示出来。 1780 | 1781 | 想了解可用的 6 个调试命令,可以查阅`:h >cont`和阅读下面内容。需要指出的是,类似 gdb 和其他相似调试器,调试命令可以使用它们的简短形式:`c`、 `q`、`n`、`s`、 `i`和 `f`。 1782 | 1783 | 除了上面的之外,你还可以自由地使用任何 Vim 的命令。比如,`:echo myvar`,该命令会在当前的脚本代码位置和上下文上被执行。 1784 | 1785 | 只需要简单使用`:debug 1`,你就获得了[REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)调试特性。 1786 | 1787 | 当然,调试模式下是可以定义断点的,不然的话每一行都去单步调试就会十分痛苦。(断点之所以被叫做断点,是因为运行到它们的时候,运行就会停止下来。因此,你可以利用断点跳过自己不感兴趣的代码区域)。请查阅`:h :breakadd`、 `:h :breakdel`和 `:h :breaklist`获取更多细节。 1788 | 1789 | 假设你需要知道你每次在保存一个文件的时候有哪些代码在运行: 1790 | 1791 | ```vim 1792 | :au BufWritePost 1793 | " signify BufWritePost 1794 | " * call sy#start() 1795 | :breakadd func *start 1796 | :w 1797 | " Breakpoint in "sy#start" line 1 1798 | " Entering Debug mode. Type "cont" to continue. 1799 | " function sy#start 1800 | " line 1: if g:signify_locked 1801 | >s 1802 | " function sy#start 1803 | " line 3: endif 1804 | > 1805 | " function sy#start 1806 | " line 5: let sy_path = resolve(expand('%:p')) 1807 | >q 1808 | :breakdel * 1809 | ``` 1810 | 1811 | 正如你所见,使用``命令会重复之前的调试命令,也就是在该例子中的`s`命令。 1812 | 1813 | `:debug`命令可以和[verbose](#verbosity)选项一起使用。 1814 | 1815 | ## 语法文件调试 1816 | 1817 | 语法文件由于包含错误的或者复制的正则表达式,常常会使得 Vim 的运行较慢。如果 Vim 在编译的时候包含了`+profile` [feature](#what-kind-of-vim-am-i-running)特性,就可以给用户提供一个超级好用的`:syntime`命令。 1818 | 1819 | ```vim 1820 | :syntime on 1821 | " 多次敲击来重绘窗口,这样的话就会使得相应的语法规则被重新应用一次 1822 | :syntime off 1823 | :syntime report 1824 | ``` 1825 | 1826 | 输出结果包含了很多的度量维度。比如,你可以通过结果知道哪些正则表达式耗时太久需要被优化;哪些正则表达式一直在别使用但重来没有一次成功匹配。 1827 | 1828 | 请查阅`:h :syntime`。 1829 | 1830 | # 杂项 1831 | 1832 | ## 附加资源 1833 | 1834 | | 资源名称 | 简介 | 1835 | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | 1836 | | [七个高效的文本编辑习惯](http://www.moolenaar.net/habits.html) | 作者:Bram Moolenaar(即 Vim 的作者) | 1837 | | [七个高效的文本编辑习惯 2.0(PDF 版)](http://www.moolenaar.net/habits_2007.pdf) | 同上 | 1838 | | [IBM DeveloperWorks: 使用脚本编写 Vim 编辑器](http://www.ibm.com/developerworks/views/linux/libraryview.jsp?sort_order=asc&sort_by=Title&search_by=scripting+the+vim+editor) | Vim 脚本编写五辑 | 1839 | | [《漫漫 Vim 路》](http://learnvimscriptthehardway.stevelosh.com) | 使用魔抓定制 Vim 插件 | 1840 | | [《 Vim 实践 (第 2 版)》](http://www.amazon.com/Practical-Vim-Edit-Speed-Thought/dp/1680501275/) | 轻取 Vim 最佳书籍 | 1841 | | [Vimcasts.org](http://vimcasts.org/episodes/archive) | Vim 录屏演示 | 1842 | | [为什么是个脚本都用 vi?](http://www.viemu.com/a-why-vi-vim.html) | 常见误区释疑 | 1843 | | [你不爱 vi,所以你不懂 Vim ](http://stackoverflow.com/a/1220118) | 简明,扼要,准确的干货 | 1844 | 1845 | ## Vim 配置集合 1846 | 1847 | 目前,网上有很多流行 Vim 配置集合,对于 Vim 配置集合,个人认为有利有弊。 1848 | 对于维护的比较好的配置,比如 [SpaceVim](http://spacevim.org/cn/) 还是值得尝试的,可以节省很多自行配置的时间。 1849 | 当然,网上还有很多其他很流行的配置,比如: 1850 | 1851 | - [k-vim](https://github.com/wklken/k-vim) 1852 | - [amix's vimrc](https://github.com/amix/vimrc) 1853 | - [janus](https://github.com/carlhuda/janus) 1854 | 1855 | ## 常见问题 1856 | 1857 | ### 编辑小文件时很慢 1858 | 1859 | 有两个因素对性能影响非常大: 1860 | 1861 | 1. 过于复杂的 **正则表达式** 。尤其是 Ruby 的语法文件,以前会造成性能下降。(见[调试语法文件](#debugging-syntax-files)) 1862 | 1863 | 2. **屏幕重绘** 。有一些功能会强制重绘所有行。 1864 | 1865 | | 典型肇事者 | 原因 | 解决方案 | 1866 | | ------------------------ | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | 1867 | | `:set cursorline` | 会导致所有行重绘 | `:set nocursorline` | 1868 | | `:set cursorcolumn` | 会导致所有行重绘 | `:set nocursorcolumn` | 1869 | | `:set relativenumber` | 会导致所有行重绘 | `:set norelativenumber` | 1870 | | `:set foldmethod=syntax` | 如果语法文件已经很慢了,这只会变得更慢 | `:set foldmethod=manual`,`:set foldmethod=marker` 或者使用[快速折叠](https://github.com/Konfekt/FastFold)插件 | 1871 | | `:set synmaxcol=3000` | 由于内部表示法,Vim 处理比较长的行时会有问题。让它高亮到 3000 列…… | `:set synmaxcol=200` | 1872 | | matchparen.vim | Vim 默认加载的插件,用正则表达式查找配对的括号 | 禁用插件:`:h matchparen` | 1873 | 1874 | **注意**:只有在你真正遇到性能问题的时候才需要做上面的调整。在大多数情况下使用上面提到的选项是完全没有问题的。 1875 | 1876 | ### 编辑大文件的时候很慢 1877 | 1878 | Vim 处理大文件最大的问题就是它会一次性读取整个文件。这么做是由于缓冲区的内部机理导致的(在 [vim_dev](https://groups.google.com/forum/#!topic/vim_dev/oY3i8rqYGD4/discussion) 中讨论)。 1879 | 1880 | 如果只是想查看的话,`tail hugefile | vim -` 是一个不错的选择。 1881 | 1882 | 如果你能接受没有语法高亮,并且禁用所有插件和设置的话,使用: 1883 | 1884 | ```sh 1885 | $ vim -u NONE -N 1886 | ``` 1887 | 1888 | 这将会使得跳转变快很多,尤其是省去了基于很耗费资源的正则表达式的语法高亮。你还可以告诉 Vim 不要使用交换文件和 viminfo 文件,以避免由于写这些文件而造成的延时: 1889 | 1890 | ```sh 1891 | $ vim -n -u NONE -i NONE -N 1892 | ``` 1893 | 1894 | 简而言之,尽量避免使用 Vim 写过大的文件。 1895 | 1896 | ### 持续粘贴(为什么我每次都要设置 'paste' 模式) 1897 | 1898 | 持续粘贴模式让终端模拟器可以区分输入内容与粘贴内容。 1899 | 1900 | 你有没有遇到过往 Vim 里粘贴代码之后被搞的一团糟? 1901 | 1902 | 这在你使用 `cmd+v`、`shirt-insert`、`middle-click` 等进行粘贴的时候才会发生。 1903 | 因为那样的话你只是向终端模拟器扔了一大堆的文本。 1904 | Vim 并不知道你刚刚是粘贴的文本,它以为你在飞速的输入。 1905 | 于是它想缩进这些行但是失败了。 1906 | 1907 | 这明显不是个问题,如果你用 Vim 的寄存器粘贴,如:`"+p` ,这时 Vim 就知道了你在粘贴,就不会导致格式错乱了。 1908 | 1909 | 使用 `:set paste` 就可以解决这个问题正常进行粘贴。见 `:h 'paste'` 和 `:h 'pastetoggle'` 获取更多信息。 1910 | 1911 | 如果你受够了每次都要设置 `'paste'` 的话,看看这个能帮你自动设置的插件:[bracketed-paste](https://github.com/ConradIrwin/vim-bracketed-paste)。 1912 | 1913 | [点此](http://cirw.in/blog/bracketed-paste)查看该作者对于这个插件的更多描述。 1914 | 1915 | Neovim 尝试把这些变得更顺畅,如果终端支持的话,它会自动开启持续粘贴模式,无须再手动进行切换。 1916 | 1917 | ### 在终端中按 ESC 后有延时 1918 | 1919 | 如果你经常使用命令行,那么肯定要接触 _终端模拟器_ ,如 xterm、gnome-terminal、iTerm2 等等(与实际的[终端](https://en.wikipedia.org/wiki/Computer_terminal)不同)。 1920 | 1921 | 终端模拟器与他们的祖辈一样,使用 [转义序列](https://zh.wikipedia.org/wiki/%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97) (也叫 _控制序列_ )来控制光标移动、改变文本颜色等。转义序列就是以转义字符开头的 ASCII 字符串(用[脱字符表示法](https://zh.wikipedia.org/wiki/%E8%84%B1%E5%AD%97%E7%AC%A6%E8%A1%A8%E7%A4%BA%E6%B3%95)表示成 `^[` )。当遇到这样的字符串后,终端模拟器会从[终端信息](https://en.wikipedia.org/wiki/Terminfo)数据库中查找对应的动作。 1922 | 1923 | 为了使用问题更加清晰,我会先来解释一下什么是映射超时。在映射存在歧义的时候就会产生映射超时: 1924 | 1925 | ```vim 1926 | :nnoremap ,a :echo 'foo' 1927 | :nnoremap ,ab :echo 'bar' 1928 | ``` 1929 | 1930 | 上面的例子中两个映射都能正常工作,但是当输入 `,a` 之后,Vim 会延时 1 秒,因为它要确认用户是否还要输入那个 `b`。 1931 | 1932 | 转义序列会产生同样的问题: 1933 | 1934 | - `` 作为返回普通模式或取消某个动作的按键而被大量使用 1935 | - 光标键使用转义序列进行的编码 1936 | - Vim 期望 Alt (也叫作 _Mate Key_ )会发送一个正确的 8-bit 编码的高位,但是许多终端模拟器并不支持这个(也可能默认没有启用),而只是发送一个转义序列作为代替。 1937 | 1938 | 你可以这样测试上面所提到的事情: `vim -u NONE -N` 然后输入 `i` ,你会看到一个以 `^[` 开头的字符串,表明这是一个转义序列,`^[` 就是转义字符。 1939 | 1940 | 简而言之,Vim 在区分录入的 `` 和转义序列的时候需要一定的时间。 1941 | 1942 | 默认情况下,Vim 用 `:set timeout timeoutlen=1000`,就是说它会用 1 秒的时间来区分有歧义的映射 _以及_ 按键编码。这对于映射来说是一个比较合理的值,但是你可以自行定义按键延时的长短,这是解决该问题最根本的办法: 1943 | 1944 | ```vim 1945 | set timeout " for mappings 1946 | set timeoutlen=1000 " default value 1947 | set ttimeout " for key codes 1948 | set ttimeoutlen=10 " unnoticeable small value 1949 | ``` 1950 | 1951 | 在 `:h ttimeout` 里你可以找到一个关于这些选项之间关系的小表格。 1952 | 1953 | 而如果你在 tmux 中使用 Vim 的话,别忘了把下面的配置加入到你的 `~/.tmux.conf`文件中: 1954 | 1955 | set -sg escape-time 0 1956 | 1957 | ### 无法重复函数中执行的搜索 1958 | 1959 | - 在命令中的搜索(`/`、`:substitute` 等)内容会改变“上次使用的搜索内容”。(它保存在`/`寄存器中,用 `:echo @/` 可以输出它里面的内容) 1960 | - 简单的文本变化可以通过 `.` 重做。(它保存在 `.` 寄存器,用 `:echo @.` 可以输出它的内容) 1961 | 1962 | 而在你在函数中进行这些操作的时候,一切就会变得不同。因此你不能用 N/n 查找某个函数刚刚查找的内容,也不能重做函数中对文本的修改。 1963 | 1964 | 帮助文档:`:h function-search-undo`。 1965 | 1966 | ## 进阶阅读 1967 | 1968 | - [Vim 插件开发指南](https://github.com/wsdjeg/vim-plugin-dev-guide) 1969 | - [常用插件列表](PLUGINS.md) 1970 | 1971 | ## 加入我们 1972 | 1973 | 可以协助我们核对翻译,或者从[章节列表](CONTRIBUTING.md)中认领章节进行翻译。 1974 | --------------------------------------------------------------------------------