VIM
Basic
Command-line
:e {name of file}
open file for editing:ls
show open buffers:help {topic}
open help:help :w
opens help for the:w
command:help w
opens help for thew
movement
Movement
Movements in Vim are also called “nouns”.
- Basic movement:
hjkl
(left, down, up, right) - Words:
w
(next word),b
(beginning of word),e
(end of word) - Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) - Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Scroll:
Ctrl-u
(up),Ctrl-d
(down) - File:
gg
(beginning of file),G
(end of file) - Line numbers:
:{number}<CR>
or{number}G
(line {number}) - Misc:
%
(corresponding item) - Find:
f{character}
,t{character}
,F{character}
,T{character}
- find/to forward/backward {character} on the current line
,
/;
for navigating matches
- Search:
/{regex}
,n
/N
for navigating matches
Edits
Vim’s editing commands are also called “verbs”
i
enter Insert modeo
/O
insert line below / aboved{motion}
delete {motion}- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
c{motion}
change {motion}- e.g.
cw
is change word. liked{motion}
followed byi
- e.g.
x
delete character (equal dodl
)s
substitute character (equal tocl
)- Visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
u
to undo,<Ctrl+r>
to redoy
to copy / “yank” (some other commands liked
also copy)p
to paste- Lots more to learn: e.g.
~
flips the case of a character
Counts
You can combine nouns and verbs with a count, which will perform a given action a number of times.
3w
move 3 words forward5j
move 5 lines down7dw
delete 7 words
Modifiers
You can use modifiers to change the meaning of a noun. Some modifiers are i
, which means “inner” or “inside”, and a
, which means “around”.
光标在括号中,
ci(
可以把括号内的内容替换并进入 insert,类似di(
也一样,相当于删除。da(
连带括号一起删除。这里的
i
和a
不是 edit 中的含义,而是inner和around的意思。
ci(
change the contents inside the current pair of parenthesesci[
change the contents inside the current pair of square bracketsda'
delete a single-quoted string, including the surrounding single quotes
Tabs 标签页
gt
, gT
切换标签页。
Ctrl-w c
关闭当前标签页。
Windows 窗口
Ctrl-w h/j/k/l
移动到左/下/上/右窗口
Ctrl-w c
Ctrl w w
: 循环窗口切换。
Customizing Vim
Extending Vim
Vim 8.0 之后自带插件管理工具,只要 create the directory ~/.vim/pack/vendor/start/
, and put plugins in there (e.g. via git clone
). vendor
目录名好像可以替换。
Advanced Vim
Search and replace
:s
(substitute) command
%s/foo/bar/g
- replace foo with bar globally in file
Multiple windows
:sp
/:vsp
to split windows- Can have multiple views of the same buffer.
Macros
to do
Register
"ayiw
将内容保存进寄存器 a。Ctrl-r a
insert mode 下将寄存器内容粘贴出来。
Misc
gUw
转大写guw
转小写
Vscode Vim
gd
: 函数定义跳转。C-o
: 返回。gh
: 原地查看函数信息。
setting.json
: 配置 vim 快捷键。
C-q
: visual block.
visual 模式选中后,I
进入 muti-cursor 进行多行编辑。
C-shift-e
: 跳转资源管理器。C-shift-c
: 聚焦到大纲。
/***vscode vim***/
"vim.useSystemClipboard": true,
"vim.incsearch": true,
"vim.hlsearch": true,
"vim.easymotion": true,
"vim.leader": "<space>",
"vim.insertModeKeyBindings": [
{
"before": ["j", "j"],
"after": ["<Esc>"]
}
],
"vim.normalModeKeyBindings": [
{
"before": ["<Enter>"],
"after": ["o", "<Esc>"]
},
{
"before": ["<C-v>"],
"after": ["i", "<C-v>","<Esc>"]
}
],
"vim.handleKeys": {
"<C-a>": false,
"<C-x>": false,
"<C-f>": false,
"<C-h>": false,
"<C-n>": false,
"<C-p>": false,
"<C-y>": false,
"<C-g>": false,
"<Shift+Tab>": false
},
Tmux
~/.tmux.conf 可修改 tmux 配置。
tmux
: open a new session.C-b
-> C-a
C-b %
: 左右分屏。改为-> C-a |
C-b "
: 上下分屏。-> C-a -
C-b <arrow key>
:在 panes 间移动。-> alt + arrow
exit
or hit Ctrl-d
:退出当前 pane。C-b c
: new window.C-b p
: previous window.C-b n
: next window.C-b <number>
: move to window n.tmux ls
: list sessions.tmux attach -t 0
: attach to 0 session.C-b ?
: help message.C-b z
: make a pane go full screen. Hit C-b z
again to shrink it back to its previous sizeC-b C-<arrow key>
: 调整当前 window 的大小。C-b ,
: 重命令当前 window。<C-b> [
Start scrollback. You can then press <space>
to start a selection and <enter>
to copy that selection.
my configs
C-b
-> C-a
C-b %
-> C-a |
C-b "
-> C-a -
C-b <arrow key>
-> alt <arrow key>