1、vim 三种基本的工作模式

vim有三种基本的工作模式,分别为:命令模式、末行模式、编辑模式。关于这三种工作模式的介绍,请见下文。

1.1、命令模式

使用vim打开文件之后,首先进入命令模式,它是vim编辑器的入口模式。在命令模式下,通过使用命令对文件进行常规的编辑操作,例如:复制、粘贴、删除、定位、翻页等等。

1.2、末行模式

末行模式是vim编辑器的出口,要退出vim,必须要在末行模式下。在末行模式下,执行退出文件、保存并退出文件等操作。

1.3、编辑模式

正常的编辑文字模式。

2、三种模式的切换操作

(1)vim打开一个文件进入的就是命令模式,另外两种模式需要回到命令模式才能进入彼此。

(2)在命令模式中按下i,I,o,O,a,A,r,R任何一个字母后进入编辑模式,进入编辑模式后,下方会出现INSERT或者REPLACE字样,按Esc键退出编辑模式,回到命令模式。

(3)在命令模式中按下:,/,?中任意一个即可进入末行模式,末行模式可以对文档或者vim环境进行操作,例如查找、读取、存盘、替换、离开、显示行号等。

3、各种模式切换

普通模式 - - NORMAL - - ———> 插入模式 - - INSERT - -

  • insert

    • i 在光标前插入

    • I 在行首插入

  • open a new line

    • o 在下一行插入

    • O 在上一行插入

  • append

    • a 在光标后插入
    • A 在行尾插入

普通模式 - - NORMAL - - <——— 插入模式 - - INSERT - -

  • ESC
  • jj / CapsLock

普通模式 - - NORMAL - - ———> 可视模式 - - VISUAL - -

  • v

普通模式 - - NORMAL - - <——— 可视模式 - - VISUAL - -

  • v
  • Esc

普通模式 - - NORMAL - - ———> 命令模式

  • :

普通模式 - - NORMAL - - <——— 命令模式

  • Esc

总结

Vim01

Vim02

Vim03

Vim04

Vim05

4、光标移动

Vim06

Vim07

解释:

  • w : word
  • b : begin
  • e : end
  • ge : 不太常用

Vim08

Vim09

字符 单词 跳转 句和段
h 左 w下一个单词开头 0 行首 f char 跳到char位置 ( 到句首
j 下 b本单词或上一个单词开头 ^ 行首非空字符 F char 反向 ) 到句尾
k 上 e本单词或下一个单词结尾 $ 行尾 t char char前一位置 { 到段首
l 右 ge上一个单词结尾 gg 首行 T char char后一位置 } 到段尾
W长单词 E长单词 B长单词 G 尾行 ; 重复上次的跳转
ngg 第n行 , 反向查找上次的查找

5、动作 motion

Vim10

Vim11

  • it : inner tag
  • is : inner sentence
  • ip : inner paragraph

6、操作符

Vim12

操作符 说明
d(delete) 删除
c(change) 修改(删除并且进入插入模式)
y(yank) 复制
v(visual) 选中并且进入 VISUAL 模式
p(paste) 粘贴
b(bracket) 小括号的简写
B(Bracket) 大括号的简写
e(entire) 全选

7、大小写

Vim13

  • ‘g’ means goto
  • ‘U’ means Uppercase
  • ‘u’ 与上一个相反

Tips

  • gd : goto definition 跳转到定义
  • ctrl + o : 跳回
  • gh : goto (mouse) hover 鼠标悬浮
  • gt : goto tab 跳转下一个标签页
  • gT : 与上一个相反 跳转上一个标签页
  • ngt / ngT : 跳转到第n个标签页

VSCode中的Vim

  • vscode中默认为space

  • S{Charactor} : 寻找某个字符

  • xxx : 详见配置

Motion Command Description
<leader><leader> s <char> Search character
<leader><leader> f <char> Find character forwards
<leader><leader> F <char> Find character backwards
<leader><leader> t <char> Til character forwards
<leader><leader> T <char> Til character backwards
<leader><leader> w Start of word forwards
<leader><leader> b Start of word backwards
<leader><leader> l Matches beginning & ending of word, camelCase, after _, and after # forwards
<leader><leader> h Matches beginning & ending of word, camelCase, after _, and after # backwards
<leader><leader> e End of word forwards
<leader><leader> ge End of word backwards
<leader><leader> j Start of line forwards
<leader><leader> k Start of line backwards
<leader><leader> / <char>... <CR> Search n-character
<leader><leader><leader> bdt Til character
<leader><leader><leader> bdw Start of word
<leader><leader><leader> bde End of word
<leader><leader><leader> bdjk Start of line
<leader><leader><leader> j JumpToAnywhere motion; default behavior matches beginning & ending of word, camelCase, after _ and after #

<leader><leader> (2s|2f|2F|2t|2T) <char><char> and <leader><leader><leader> bd2t <char>char> are also available. The difference is character count required for search. For example, <leader><leader> 2s <char><char> requires two characters, and search by two characters. This mapping is not a standard mapping, so it is recommended to use your custom mapping.

  • surround
Surround Command Description
y s <motion> <desired> Add desired surround around text defined by <motion>
d s <existing> Delete existing surround
c s <existing> <desired> Change existing surround to desired
S <desired> Surround when in visual modes (surrounds full selection)

Some examples:

  • "test" with cursor inside quotes type cs"' to end up with 'test'
  • "test" with cursor inside quotes type ds" to end up with test
  • "test" with cursor inside quotes type cs"t and enter 123> to end up with <123>test</123>

多光标功能

🖱️ Multi-Cursor Mode

:warning: Multi-Cursor mode is experimental. Please report issues in our feedback thread.

Enter multi-cursor mode by:

  • On OSX, cmd-d. On Windows, ctrl-d.
  • gb, a new shortcut we added which is equivalent to cmd-d (OSX) or ctrl-d (Windows). It adds another cursor at the next word that matches the word the cursor is currently on.
  • Running “Add Cursor Above/Below” or the shortcut on any platform.

Once you have multiple cursors, you should be able to use Vim commands as you see fit. Most should work; some are unsupported (ref PR#587).

  • Each cursor has its own clipboard.
  • Pressing Escape in Multi-Cursor Visual Mode will bring you to Multi-Cursor Normal mode. Pressing it again will return you to Normal mode.

vscode中可以使用 cmd + d 批量修改

vim中可以使用 gb 批量膝盖

将光标定位到终端

在settings.json添加如下快捷键

//由editor 转到 terminal
{
"before": ["leader", "t"],
"commands": ["workbench.action.terminal.focus"]
}

space + t 即可跳转到终端中

跳转Tab

进入命令模式 :

  • 输入 tabn : tab next 即进入下一个标签页
  • 输入 tabp : tab prev 即进入下一个标签页