本文参考MIT-Missing-Semester课程

Vim的理念

当我们编程的时候,会花费大量时间阅读和修改代码,Vim是一个模块化编辑器,整个Vim使用一种可编程化按键操作(包括界面和命令),这种设计可以让人们不使用鼠标和键盘方向键来节省时间

模块化编辑

  • 标准模式(Normal):移动和编辑文件
  • 插入模式(Insert):插入文本
  • 替换模式(Replace):替换文本
  • 可视化模式(Visual):选择文本块
  • 命令行模式(Command-line):运行命令

Vim在左下角显示当前模式,默认模式为标准模式,大部分时间我们会在标准模式和插入模式中切换(因为经常使用<ESC>,所以很多人重新映射该按键为CapsLock
切换模式:

  • 标准模式:<ESC>
  • 插入模式:i
  • 替换模式:R
  • 可视化模式:v
  • 可视化行模式:V
  • 可视化块模式:<Ctrl-v>(Ctrl+V,也写作 ^V)
  • 命令行模式::

基础

插入文本

在标准模式下按i进入插入模式,此时可以像使用其他编辑器一样输入文本,知道按下<ESC>返回标准模式

缓冲区

Vim会生成一系列缓冲文件,一个会话有许多页面和窗口,共享一个缓冲区,这允许我们同时观看一个文件的不同部分,默认情况下,Vim仅开启一个页面和窗口

命令行

在标准模式下按:进入命令行模式,光标会跳到屏幕底部的命令行区域。命令行模式有很多功能,包括打开,保存,关闭文件以及退出Vim

  • :q退出(关闭窗口)
  • :w保存(写入文件)
  • :wq保存并退出
  • :e {file name}打开文件并编辑
  • :ls展示打开的缓存
  • :help {topic}打开帮助

Vim界面操作

移动

  • 基础移动:hjkl(左,下,上,右)
  • 单词:w(下一个单词),b(单词的开头),e(单词的结尾)
  • 行:0(一行的开头),^(第一个非空格字符),$(一行的结尾)
  • 屏幕:H(屏幕顶部),M(屏幕中央),L(屏幕底部)
  • 滚动:Ctrl-u(上升),Ctrl-d(下降)
  • 文件:gg(文件开头),G(文件结尾)
  • 行号::{number}<CR>或者{number}G(行号)
  • 杂项:%对应项目
  • 查找:f{character},t{character},F{character},T{character}
    • 查找本行前面或后面的字符
    • ,/:导航匹配
  • 搜索:/{regex}.n/N导航匹配

选择

可视化模式:

  • 可视化:v
  • 可视化行:V
  • 可视化文本块:Ctrl-v
    也可以使用移动按键选择

编辑

  • i进入插入模式(删除和修改操作使用不止退格键)
  • o/O在上面或下面插入一行
  • d{motion}删除{motion},(dw删除单词,d$删除到行末,d0删除到行首)
  • c{motion}修改{motion},(cw修改单词,作用相当于插入模式的d{motion}
  • x删除字符(等效于dl
  • s替换字符(等效于cl
  • 可视化模式+操作(选择文本,d删除,c修改)
  • u撤回,<Ctrl-r>`取消撤回
  • y复制(d也是复制)
  • p粘贴
  • ~翻转字符大小写

计数

  • 3w向前移动3个单词
  • 5j向下移动5行
  • 7dw删除7个单词

修改

i:内部
a:周围

  • ci(改变当前小括号内部的内容
  • ci[改变当前方括号内部的内容
  • da'删除一个单引号字符串,包括单引号

个性化Vim

Vim通过一个纯文本配置文件进行个性化(包括命令行命令),在~/.vimrc

推荐按照以下设置修改默认配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
" Comments in Vimscript start with a `"`.

" If you open this file in Vim, it'll be syntax highlighted for you.

" Vim is based on Vi. Setting `nocompatible` switches from the default
" Vi-compatibility mode and enables useful Vim functionality. This
" configuration option turns out not to be necessary for the file named
" '~/.vimrc', because Vim automatically enters nocompatible mode if that file
" is present. But we're including it here just in case this config file is
" loaded some other way (e.g. saved as `foo`, and then Vim started with
" `vim -u foo`).
set nocompatible

" Turn on syntax highlighting.
syntax on

" Disable the default Vim startup message.
set shortmess+=I

" Show line numbers.
set number

" This enables relative line numbering mode. With both number and
" relativenumber enabled, the current line shows the true line number, while
" all other lines (above and below) are numbered relative to the current line.
" This is useful because you can tell, at a glance, what count is needed to
" jump up or down to a particular line, by {count}k to go up or {count}j to go
" down.
set relativenumber

" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2

" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can't backspace before the insertion point set with 'i'.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start

" By default, Vim doesn't let you hide a buffer (i.e. have a buffer that isn't
" shown in any window) that has unsaved changes. This is to prevent you from "
" forgetting about unsaved changes and then quitting e.g. via `:qa!`. We find
" hidden buffers helpful enough to disable this protection. See `:help hidden`
" for more information on this.
set hidden

" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase

" Enable searching as you type, rather than waiting till you press enter.
set incsearch

" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " 'Q' in normal mode enters Ex mode. You almost never want this.

" Disable audible bell because it's annoying.
set noerrorbells visualbell t_vb=

" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a

" Try to prevent bad habits like using the arrow keys for movement. This is
" not the only possible bad habit. For example, holding down the h/j/k/l keys
" for movement, rather than using more efficient movement commands, is also a
" bad habit. The former is enforceable through a .vimrc, while we don't know
" how to prevent the latter.
" Do this in normal mode...
nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>
" ...and in insert mode
inoremap <Left> <ESC>:echoe "Use h"<CR>
inoremap <Right> <ESC>:echoe "Use l"<CR>
inoremap <Up> <ESC>:echoe "Use k"<CR>
inoremap <Down> <ESC>:echoe "Use j"<CR>

随着使用Vim,你会更加熟悉Vim的设置内容,建议根据自己的习惯进行更改,可以参考其他人的设置,但不建议复制别人的全部配置

拓展Vim

Vim有很多实用的插件,不需要下载额外的插件器,可以用内置的包管理器系统,只需要创建文件夹~/.vim/pack/vendor/start/然后把插件放到里面即可

推荐插件:

其他程序中的Vim模式

许多工具支持Vim仿真模式,支持Vim的大部分基础操作

Shell

Bash:set -o vi
Zsh:bindkey -v
Fish:fish_vi_key_bindings
其他:export EDITOR=vim(git生效)

GNU Readline

添加set editing-mode vi到文件~/.inputrc
(适用于Python REPL)

高级Vim

搜索和替换

s替换命令

  • %s/foo/bar/g(将文件里的全部foo替换为bar)
  • %s/\[.*\](\(.*\))/\1/g(将带有命名的md链接替换为纯文本URL)

多窗口

  • :sp /:vsp分割窗口(对同一个缓存区有多个视口)

  • q{character}开始记录一个宏
  • q停止记录宏
  • @{character}重播这个宏
  • 如果有错误,宏会停止
  • {number}@{character}多次执行同一个宏
  • 宏可以被递推
    • q{character}q清理宏
    • 记录宏,并用@{character} 去递归调用宏