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 the w 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 mode
  • o / O insert line below / above
  • d{motion} delete {motion}
    • e.g. dw is delete word, d$ is delete to end of line, d0 is delete to beginning of line
  • c{motion} change {motion}
    • e.g. cw is change word. like d{motion} followed by i
  • x delete character (equal do dl)
  • s substitute character (equal to cl)
  • Visual mode + manipulation
    • select text, d to delete it or c to change it
  • u to undo, <Ctrl+r> to redo
  • y to copy / “yank” (some other commands like d 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 forward
  • 5j move 5 lines down
  • 7dw 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(连带括号一起删除。

这里的ia不是edit中的含义,而是inneraround的意思。

  • ci( change the contents inside the current pair of parentheses
  • ci[ change the contents inside the current pair of square brackets
  • da' 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

my vim config

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
},

easymotion

<leader>+<leader>+w <leader>+<leader>+b: 快速跳转 <leader>+<leader>+f+<char>: 查找跳转

Vimtutor

Lesson 1

hjkl 移动
x 删除一个字符
i 输入
a append输入

Lesson 2

dw 删除单词
de 删除单词,光标留在单词末尾。
d$ 输出到行尾
2w 3b 2e 移动单词
d2w 删除两个单词
dd 2dd 删除两行
u 撤销 U 返回一行的原始状态 Ctrl r 复原

Lesson 3

dd 之后的一行可以 p 粘贴
rx 替换某个字符为x
ce 删除光标后单词部分,并进入insert mode。
cc 删除整行,进入insert mode。
c$ 输出光标到行尾,并进入insert mode。

Lesson 4

Ctrl g 显示当前行状态 G 文件末尾 gg 文件开头
/ 向后搜索 ?向前搜索 ctrl o 返回 ctrl i 前进
% 跳转到对应匹配的) ] }
s/thee/the 替换一个theethe
s/thee/the/g 一行中thee替换成the
:%s/thee/the/g 整个文件的thee替换成the
:%s/thee/the/gc 整个文件的thee替换成the,每个替换会有命令提示
:#,#s/thee/the/g # 是替换的行范围

Lesson 5

! + command 执行外部命令
:w FILENAME 文件另存为
v 进入visual模式选中再 :w FILENAME 保存部分内容
:r FILENAME 将文件内容追加到光标下
:r !ls 将ls内容加到光标下

Lesson 6

o 光标下插入新行 O光标上插入新行
R replace mode
2y 复制两行
:set ic 接下来搜索大小写都会包括

Lesson 7

:ctrl d可以自动显示命令

NERDTree

Ctrl p: 模糊搜索。

文件操作 o: 打开文件并跳转。
go: 打开文件不跳转,仍然保留在nerdtree。
t: 新建标签页打开该文件。
T: 新建标签页打开该文件, 不跳转。
i: 上下分屏。
gi: 上下分屏,不跳转。
s: 左右分屏。
gs: 左右分屏,不跳转。
目录操作 o: 打开目录。
O: 递归打开目录。
t: 在新标签页中打开目录。
T: 在新标签页中打开目录,不跳转。
x: 关闭父目录。
X:关闭子目录。

Ctags

Ctags使用方法

Ubuntu: $ sudo apt-get update && sudo apt-get install -y exuberant-ctags

创建~/.ctags,描述要忽略的文件/文件夹。
进入source code根目录,运行ctags, 生成文件tags

vim -t <tags> 从shell命令行跳转到tag的定义位置。
Ctrl ] 跳转。
Ctrl T 返回。
:tn 跳转到下一个定义(如果有多个定义的话)。
:tp 跳转到上一个定义(如果有多个定义的话)。
:tags 列出tag stack。
:tags main 跳转到指定tag,main。
:tags /^get jumps to the tag that starts with “get”
:tag /Final$ jumps to the tag that ends with “Final”
:tag /norm lists all the tags that contain “norm”, including “id_norm”

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 size
C-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>

Reference

https://learnvim.irian.to/