https://missing.csail.mit.edu/2020/editors/
Modal editing
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block): for selecting blocks of text
- Command-line: for running a command
Normal mode
光标移动
- h j k l 左下上右
- w 光标向后移动一个单词(word)
- b 光标向前移动一个单词(before)
- e 跳转到单词的结尾(end)
- a 在单词的结尾进入insert mode。(append)
- A 在整行的结尾进入insert mode。
- ^ 跳转到行首第一个非空字符
- 0 跳转到行首
- $ 跳转到行尾
- % 再附近匹配的前后括号
()
[]
{}
间来回跳转 - ctrl+b(before)或PgUp 向上翻页
- ctrl+f(after)或PgDn 向下翻页
- ctrl+u 向上翻半屏(up)
- ctrl+d 向下翻半屏(down)
- gg 移动光标到首行
- G 移动光标到末行
- {数字}G 跳转到特定行。如:
500G
,跳转到第500行 - LMH 移动光标到当前屏幕的最下行、中间行、最上行(Lowest、Middle、Highest)
- f 在本行查找(find)字母,并跳转到第一个查找到的字母前。如:先按
f
再按w
,查找字母w - F 类似于
f
,向前查找 ,并跳转到第一个查找到的字母前 - t 类似于
f
,向后查找,并跳转到第一个查找到的字母前一个字母前(to) - T 类似于
F
,向前查找,并跳转到第一个查找到的字母后 - /{what you want to search} 查找,按回车跳转到搜索处。查找后,再按
n
搜索下一个,N
搜索上一个. - ?{what you want to search} 与
/
类似,向上查找,按回车跳转到搜索处。
编辑
- d 删除(delete),需要与光标移动键结合操作。如:dd 删除整行,dw 删除光标后的一个单词,d$ 从光标处删除到行尾
- c 改变(change),需要与光标移动键结合操作。先删除,再进入insert mode。如:cc 清空整行,进入insert mde
- x 删除一个字符
- s 删除一个字符(substitute),并进入insert mode
- r 替换一个字符(replace)。如:先按
r
再按a
,将光标处字符改为a - u 撤销(undo) ,U 撤销整行的操作
- ctrl+r 重做(redo),即撤销撤销
- y 复制(yank),需要与光标移动键结合操作
- p 粘贴(paste)
- . 重复输入之前一行输入的内容
- ~ 改变大小写(Shift+`)
- 数字 先按数字键再按其他键表示重复做,比如
5j
向下移动五行 - a与i范围修饰作用
a
表示around,i
表示inside。例如:ci[
删除[]内内容,再进入insert mode;da[
删除包括[]
在内的[]括号内内容
Command-line mode
- :W 保存write,如:
:w TEST.txt
保存为TEST.txt。可以先在visual mode选择字块,再:w TEST.txt
保存字块。 - :q 退出quit
- :q! 退出不保存
- :qa 退出所有层quit all
- :e {name of file} 打开文件open file for editing
- :sp 打开一个新窗口
- :!{command} 执行外部指令。如,
:!ls
暂时返回到命令行窗口,执行ls命令。注意:!ls
与:ls
的不同 - :ls show open buffers列出文件。显示当前在编辑器中打开的缓冲区列表,并显示与每个缓冲区相关的文件名。例如:
1 # "文件1.txt" 第 1 行
2 %a "文件2.txt" 第 0 行
3 "文件3.txt" 第 0 行
在每个文件前面的数字表示缓冲区编号,“#” 符号表示该缓冲区是当前缓冲区。“%” 符号表示该缓冲区是一个备用缓冲区,是用于快速在两个文件之间切换的特殊类型的缓冲区。“%” 符号后面的 “a” 表示该缓冲区是当前窗口的备用缓冲区。
- : s/original/new/ 查找字符串original,把它替换为new。
:s/original/new/g
查找整行所有的original,把他们替换为new - : s?original?new向查找字符串original,把它替换为new。
:s?original?new?g
查找整行所有的original,把他们替换为new - :#,#s/old/new/gwhere #,# are the line numbers of the range of lines where the substitution is to be done.
- :%s/old/new/g to change every occurrence in the whole file.
- :%s/old/new/gc to find every occurrence in the whole file,with a prompt whether to substitute or not.
- :r FILENAME 插入文件内容。
:r !ls
reads the output of thels
command and puts it below the cursor. - :help {topic} 打开帮助
- :help :w opens help for the :w command
- :help w opens help for the w movement
macros宏
- q{character} to start recording a macro in register {character}
- q to stop recording
- @{character} replays the macro
- Macro execution stops on error
- {number}@{character} executes a macro {number} times
- Macros can be recursive
- first clear the macro with q{character}q
- record the macro, with @{character} to invoke the macro recursively (will be a no-op until recording is complete)
配置vim
vim的配置文件是~/.vimrc
,你可以修改该文件以更改配置。一个示例配置文件~/.vimrc
内容如下:
" 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
"This option causes the keys being typed in normal mode to be displayed in the command line or bottom line of the Vim window.
set showcmd
" 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>
在命令行也可以做一些简单的设置:
:set ic 设置使用/
?
搜索时忽略大小写(ignore case)
:set noic 设置使用/
?
搜索时考虑大小写(no ignore case)
:set hls is设置’hlsearch’ and ‘incsearch’ options,用于开启高亮搜索功能。高亮搜索功能会在文本中高亮显示所有与当前搜索模式匹配的文本。
:nohlsearch关闭高亮搜索功能。
/whatyousearch\c\c
仅这次搜索忽略大小写
Typing
:set xxx
sets the option “xxx”. Some options are:
ic
‘ignorecase’ ignore upper/lower case when searching
is
‘incsearch’ show partial matches for a search phrase
hls
‘hlsearch’ highlight all matching phrases
You can either use the long or the short option name.
Prependno
to switch an option off::set noic
Resources
vimtutor
is a tutorial that comes installed withVim
- if Vim is installed, you should be able to runvimtutor
from your shell- Vim Adventures is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar has various Vim tips
- Vim Golf is code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)
Exercises
- Complete
vimtutor
. Note: it looks best in a 80x24 (80 columns by 24 lines) terminal window. - Download our basic vimrc and save it to ~/.vimrc. Read through the well-commented file (using Vim!), and observe how Vim looks and behaves slightly differently with the new config.
- Install and configure a plugin: ctrlp.vim.
- Create the plugins directory with mkdir -p ~/.vim/pack/vendor/start
- Download the plugin:
cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim
- Read the documentation for the plugin. Try using CtrlP to locate a file by navigating to a project directory, opening Vim, and using the Vim command-line to start :CtrlP.
- Customize CtrlP by adding configuration to your ~/.vimrc to open CtrlP by pressing Ctrl-P.