Learn-Vim随笔
本文最后更新于 2025年7月18日 凌晨
一、前言
学习vim的过程中发现了很多很好的资源,其中不乏bilibili上up主的精品教程。也在YouTube上看过很多教程。但Learn-Vim这个Github仓库实在让我受益良多。
本笔记便是出于此仓库: > 仓库地址
附上个人.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
47syntax on " 开启语法高亮
set number " 设置行号
set relativenumber " 设置相对行号
set wrap " 开启代码包裹,防止溢出屏幕
set showcmd " 显示命令
set wildmenu " 开启补全菜单
set hlsearch " 开启搜索高亮
set incsearch " 开启实时搜索高亮
set ignorecase " 搜索忽略大小写
set cursorline " 开启鼠标地平线
" 按下冒号重制高亮
exec "nohlsearch"
" 将大写JK映射为5倍jk
noremap J 5j
noremap K 5k
nnoremap <esc><esc> :noh<return><esc>
" 映射自动保存
map S :w<CR>
map Q :q<CR>
map s :<nop>
map R :source<CR>
call plug#begin()
Plug 'vim-airline/vim-airline'
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && npx --yes yarn install' }
Plug 'preservim/nerdtree'
Plug 'jiangmiao/auto-pairs'
Plug 'preservim/nerdcommenter'
Plug 'connorholyday/vim-snazzy'
call plug#end()
color molokai
map sr :set splitright<CR>:vsplit<CR>
map sl :set nosplitright<CR>:vsplit<CR>
map st :set nosplitbelow<CR>:split<CR>
map sb :set splitbelow<CR>:split<CR>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
二、vim语法
vim的语法只有一句,verb+noun
!
2.1 动词
这里简要罗列出vim的动词列表: 1
2
3y 复制
d 删除
c 修改(删除并开始编辑)
2.2 名词
以下皆是名词,但分为了两种。 ### 2.2.1 简单名词
1 |
|
2.2.2 补充的名词
1 |
|
三、移动
3.1 字符导航
1 |
|
禁用箭头的设置: 1
2
3
4noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
相对行号: 1
set relative number
3.2 计算编号
1 |
|
3.3 词语导航
1 |
|
3.4 行导航
1 |
|
3.5 搜索动词
1 |
|
3.6 句子导航和段落导航
1 |
|
下面是一个有两个段落的句子:
1 |
|
个人感觉句子、段落在代码里可能就不是那么实用。但用于写文章博客还是很屌的。
3.7 匹配导航
程序员专用
光标在成对的括号中其中一对上时按下%
来跳到对应的括号上。
使用场景: 1
2
3
4
5
6(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else
(+ (fib (- n 1)) (fib (- n 2)))
)))
3.8 行号导航
1 |
|
使用ctrl+g
来显示行数。
gg和GG绝对是实用中的实用。
3.9 窗口导航
1 |
|
3.10 滚动
1 |
|
3.11 搜索导航
这里的搜索是整个文档的。
1 |
|
搜索结束后关闭高亮(在~/.vimrc中添加配置): 1
nnoremap <esc><esc> :noh<return><esc>
1
2
3
4* 向后搜索光标位置的单词
# 向前搜索光标位置的单词
g* 在*的基础上增加了模糊匹配
g# 在#的基础上增加了模糊匹配
3.12 标记位置
标记当前位置,类似书签。 1
2
3ma 标记a的位置
`a 精确跳转到标记的a的位置
'a 跳转到标记a的行首
局部标记每个文件(缓冲区)只有一个,全局标记所有文件共享。
使用marks来查看所有的标记。
更多的标记用的不多,这里直接饮用(不是错别字❌)原文:
1
2
3
4
5
6
7'' Jump back to the last line in current buffer before jump
`` Jump back to the last position in current buffer before jump
`[ Jump to beginning of previously changed / yanked text
`] Jump to the ending of previously changed / yanked text
`< Jump to the beginning of last visual selection
`> Jump to the ending of last visual selection
`0 Jump back to the last edited file when exiting vim
3.13 所有的跳转
1 |
|
具体说明见Lear-Vim作者文档✍️。