01
入门
模式与基本移动
Vim 是模式编辑器——不同模式服务于不同目的。Esc 可从任何其他模式返回普通模式。hjkl 键让双手保持在主键盘行。w/b 按单词移动,0/$ 移动到行首/行尾,gg/G 跳到文件开头/末尾,:N 跳到第 N 行。
vim
# Vim Modes
# Normal mode (Esc) - default, for navigation
# Insert mode (i) - for typing text
# Visual mode (v) - for selecting text
# Command mode (:) - for Ex commands
# Basic movement (Normal mode)
h # left
j # down
k # up
l # right
w # next word start
b # previous word start
0 # beginning of line
$ # end of line
gg # first line
G # last line
:42 # go to line 42退出与保存
ZZ 和 ZQ 是最快的退出方式——无需按冒号。:x 仅在文件已修改时写入(保留 mtime),而 :wq 总是写入。:w! 在文件系统权限允许时覆盖只读标记。使用 :qa! 退出多个已修改的缓冲区。
vim
:w # write (save) file
:w! # force write (override read-only)
:q # quit
:q! # force quit (discard changes)
:wq # write and quit
:x # write only if changed, then quit
ZZ # same as :x (write if changed, quit)
ZQ # quit without saving (like :q!)
:w newfile.txt # save as new file
:sav newfile.txt # save as new file and switch to it
:wa # write all buffers
:xa # write all changed buffers and exit
:qa! # force quit all (discard everything)帮助系统
Vim 的帮助全面且带超链接。Ctrl-] 跟随标签(链接),Ctrl-T 返回。帮助记法:i_CTRL-N 表示插入模式下的 Ctrl-N,'number'(带引号)表示选项。:helpgrep 一次搜索所有帮助文件——用 :cn/:cp 在匹配间导航。K 调用光标下关键词的 man 手册(可通过 'keywordprg' 配置)。
vim
:help # main help
:help subject # help on subject (e.g. :help insert)
:help i_CTRL-N # help on Ctrl-N in insert mode
:help 'number' # help on the 'number' option (quotes)
:helpgrep pattern # search all help files for pattern
:cn / :cp # next/prev helpgrep match
K # man page for word under cursor (Normal mode)
Ctrl-] # jump to tag under cursor (follow link)
Ctrl-T # jump back from tag (pop tag stack)
:helptags ~/.vim/doc # regenerate help tags for a doc directory单词与字符移动
小写移动(w/b/e)将标点视为单词分隔符——适合代码。大写移动(W/B/E)仅将空白视为分隔符——适合散文。e/E 移动到下一个单词的结尾;ge/gE 移动到上一个单词的结尾(少用但很方便)。
vim
# Word motions (punctuation-aware)
w # next word start
b # previous word start
e # end of next word
ge # end of previous word
# WORD motions (whitespace-only delimiters, uppercase)
W # next WORD start
B # previous WORD start
E # end of next WORD
gE # end of previous WORD
# Word boundaries:
# word: sequence of word-characters OR sequence of punctuation
# WORD: anything separated by whitespace
# Examples (cursor on 'a' in "foo.bar baz"):
# w -> 'bar' (punctuation splits words)
# W -> 'baz' (only whitespace splits WORDS)行移动
0 和 $ 跳到绝对行首/行尾(包括空白)。^ 和 g_ 跳过行首/行尾的空白——通常正是你想要的。当 'wrap' 开启时,g0/g$ 在屏幕行上操作,而 0/$ 在逻辑行上操作。| 跳转到指定列号。
vim