VS Code VIM 插件高效使用

VSCode 化

由于 VSCode 本身存在一些功能可能与 Vim 冲突,以尽可能提高代码效率需要根据实际需要来决定是使用 VSCode 特性或是 Vim 特性。

模式

  • 普通模式
  • 插入模式
  • VISUAL 模式

光标移动

光标相关移动

  • h
  • j
  • k
  • l
  • ctrl + o 移动到上一光标位置
  • ctrl + i 移动到下一光标位置

行相关移动

  • 0 行开头
  • ^ 行开头(非空格)
  • $ 行尾
  • g_ 行尾(非空格)

词句相关移动

  • w 下一个词开始
  • W 下一个词开始(无视标点)
  • b 上一个词开始
  • B 上一个词开始(无视标点)
  • e 上一个词结束
  • E 上一个词结束(无视标点)
  • ( 句首
  • ) 句尾

块相关移动

  • { 移动到块开始
  • } 移动到块结束

屏幕相关移动

  • ctrl + b 向上一屏幕
  • ctrl + f 向下一屏幕
  • ctrl + u 向上半屏幕
  • ctrl + d 向下半屏幕
  • H 当前屏幕第一行
  • M 当前屏幕中建行
  • L 当前屏幕最后一行

语言相关一种

  • gd 跳转到定义

文件相关移动

  • gf 跳转到文件
  • gg 文件头
  • G 文件尾
  • <N>gg 第 N 行
  • <N>G 第 N 行
  • : + <N> 第 N 行

历史移动

  • g; 上一个修改位置
  • g, 下一个修改位置
  • ctrl + o 上一个跳转位置
  • ctrl + i 下一个跳转位置

窗口移动

  • ctrl + w, h 左边的窗口
  • ctrl + w, j 下边的窗口
  • ctrl + w, k 上边的窗口
  • ctrl + w, l 右边的窗口
  • ctrl + w, w 切换窗口
  • :bp 上一个标签
  • :bn 下一个标签

剪切/粘贴

  • d 剪切
  • y 复制
  • p 粘贴
  • P 粘贴到上一行
  • "_d 删除(不放置到剪切板)
  • "0p 粘贴(忽略剪切的内容)

折叠

  • zc 折叠代码块
  • zo 展开代码块
  • za 切换折叠
  • zR 展开所有
  • zM 折叠所有

常用功能

  • gu 小写转换
  • gU 大写转换

书签

  • m{a-zA-Z} 保存书签
    小写的是文件书签,可以用(a-z)中的任何字母标记。
    大写的是全局书签,用大写的(A-Z)中任意字母标记。
  • `{a-zA-Z} 跳转到某个书签。
    如果是全局书签,则会开启被书签标记的文件跳转至标记的行
  • `0 跳转入现在编辑的文件中上次退出的位置 (go to last exit in file)
  • `` 跳转如最后一次跳转的位置
  • ``` 跳转至最后一次编辑的位置
  • g`{mark} 跳转到书签
  • :delm {marks} 删除一个书签
  • :delm! 删除全部书签
  • :marks 显示系统全部书签

插件

easy-motion

<LEADER> <LEADER> s 进入跳转到字符模式

<LEADER> <LEADER> j/k 进入跳转到行模式

<LEADER> <LEADER> h/l 进入跳转到列模式

VSCode 相关配置文件

为了兼容 VSCode 和 Vim 的部分操作,配置如(使用 alt 在部分情况下可以替代 VIM 的 g),在 VIM 下使用 <LEADER> 可以在部分情况下替代 VIM 的 ctrl

setting.json

{
    "vim.easymotion": true,
    "vim.useSystemClipboard": true, 
    "vim.leader": "<space>",
    "vim.useCtrlKeys": false, // 优先触发 VSCode 快捷键
	"vim.mouseSelectionGoesIntoVisualMode": false, // 使用鼠标时不进入 Vim
    "vim.camelCaseMotion.enable": true,
    "vim.highlightedyank.enable": true, // 高亮复制内容
    "vim.showMarksInGutter": true,
    "vim.statusBarColorControl": true,
    "vim.statusBarColors.commandlineinprogress": "#000000", // 命令模式黑色背景,保证可见性
    "vim.statusBarColors.searchinprogressmode": "#000000",
    "vim.normalModeKeyBindings": [ // normal 模式按键绑定
        { // 使用 VSCode 内置的撤销
            "before": [
                "u",
            ],
            "commands": [
                "undo"
            ]
        },
        { // 绑定 VSCode 的重做
            "before": [
                "U"
            ],
            "commands": [
                "redo"
            ]
        },
        { // gs 跳转到符号
            "before": [
                "g",
                "s"
            ],
            "commands": [
                "workbench.action.gotoSymbol",
            ]
        },
        { // gf 跳转到编辑器
            "before": [
                "g",
                "f"
            ],
            "commands": [
                "workbench.action.showAllEditors"
            ]
        },
        { // gh 跳转到左面的编辑器
            "before": [
                "g",
                "h"
            ],
            "commands": [
                "workbench.action.previousEditor"
            ]
        },
        { // gl 跳转到右面的编辑器
            "before": [
                "g",
                "l"
            ],
            "commands": [
                "workbench.action.nextEditor"
            ]
        },
        { // <leader> + j 跳转到上一位置
            "before": [
                "<leader>",
                "j"
            ],
            "commands": [
                "extension.vim_ctrl+o"
            ]
        },
        { // <leader> + k 跳转到下一位置
            "before": [
                "<leader>",
                "k"
            ],
            "commands": [
                "extension.vim_ctrl+i"
            ]
        },
        { // gj 快速下移
            "before": [
                "g",
                "j"
            ],
            "commands": [
                "extension.vim_ctrl+d"
            ]
        },
        { // gk 快速上移
            "before": [
                "g",
                "k"
            ],
            "commands": [
                "extension.vim_ctrl+u"
            ]
        }
    ],
    "terminal.integrated.macOptionIsMeta": true
}

keybindings.json

在 终端和编辑器中来回切换比较频繁,为这个按键绑定了 alt + t

// Place your key bindings in this file to overwrite the defaults
[
    { // 查找
        "key": "cmd+f",
        "command": "actions.find",
        "when": "editorFocus || editorIsOpen"
    },
    { // 替换
        "key": "ctrl+h",
        "command": "editor.action.startFindReplaceAction",
        "when": "editorFocus || editorIsOpen"
    },
    {
        "key": "alt+m",
        "command": "bookmarks.toggle",
        "when": "editorTextFocus"
    },
    { // 热键冲突
        "key": "shift+alt+up",
        "command": "editor.action.insertCursorAbove",
        "when": "editorTextFocus"
    },
    {
        "key": "shift+alt+down",
        "command": "editor.action.insertCursorBelow",
        "when": "editorTextFocus"
    },
    { // 关闭窗口
        "key": "alt+e",
        "command": "workbench.action.closeActiveEditor"
    },
    { // 全屏
        "key": "f11",
        "command": "workbench.action.exitZenMode",
        "when": "inZenMode"
    },
    { // 格式化
        "key": "shift+alt+f",
        "command": "editor.action.formatDocument",
        "when": "editorTextFocus && !editorReadonly"
    },
    { // 切换语言
        "key": "alt+l",
        "command": "workbench.action.editor.changeLanguageMode"
    },
    { // 取消python插件选中行运行
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorFocus && !findInputFocussed && !python.datascience.hascodecells && !replaceInputFocussed && editorLangId == 'python'"
    },
    {
        "key": "shift+enter",
        "command": "-python.datascience.runcurrentcelladvance",
        "when": "editorFocus && python.datascience.featureenabled && python.datascience.hascodecells && !editorHasSelection"
    },
    {
        "key": "shift+enter",
        "command": "-python.datascience.execSelectionInteractive",
        "when": "editorFocus && python.datascience.featureenabled && python.datascience.ownsSelection && !findInputFocussed && !replaceInputFocussed && editorLangId == 'python'"
    },
    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
    },
    { // 大小写设置
        "key": "ctrl+shift+u",
        "command": "editor.action.transformToUppercase",
        "when": "editorHasSelection"
    },
    {
        "key": "ctrl+shift+l",
        "command": "editor.action.transformToLowercase",
        "when": "editorHasSelection"
    },
    { // 匹配项选择
        "key": "ctrl+a",
        "command": "editor.action.selectHighlights",
        "when": "editorFocus && editorHasSelection && editorHasMultipleSelections"
    },
    {
        "key": "cmd+a",
        "command": "editor.action.selectHighlights",
        "when": "editorFocus && editorHasSelection && editorHasMultipleSelections"
    },
    {
        "key": "ctrl+e",
        "command": "editor.action.moveSelectionToPreviousFindMatch",
        "when": "editorFocus && editorHasSelection && editorHasMultipleSelections"
    },
    {
        "key": "cmd+e",
        "command": "editor.action.moveSelectionToPreviousFindMatch",
        "when": "editorFocus && editorHasSelection && editorHasMultipleSelections"
    },
    {
        "key": "ctrl+shift+l",
        "command": "-editor.action.selectHighlights",
        "when": "editorFocus"
    },
    { // 删除一行
        "key": "shift+delete",
        "command": "editor.action.deleteLines",
        "when": "textInputFocus && !editorReadonly"
    },
    {
        "key": "shift+cmd+k",
        "command": "-editor.action.deleteLines",
        "when": "textInputFocus && !editorReadonly"
    },
    { // 注释
        "key": "ctrl+/",
        "command": "editor.action.commentLine",
        "when": "editorTextFocus && !editorReadonly"
    },
    {
        "key": "cmd+/",
        "command": "editor.action.commentLine",
        "when": "editorTextFocus && !editorReadonly"
    },
    { // 高亮选中
        "key": "ctrl+shift+h",
        "command": "highlightwords.addHighlight",
        "when": "editorFocus && editorHasSelection"
    },
    {
        "key": "ctrl+shift+h",
        "command": "highlightwords.addRegExpHighlight",
        "when": "editorFocus && !editorHasSelection"
    },
    {
        "key": "ctrl+shift+h",
        "command": "highlightwords.toggleSidebar",
    },
    // 跳转
    { // 打开文件
        "key": "alt+o",
        "command": "workbench.action.quickOpen",
    },
    { // 打开工作区符号
        "key": "alt+s",
        "command": "workbench.action.showAllSymbols",
        "when": "editorFocus"
    },
    { // 打开文件符号
        "key": "alt+f",
        "command": "workbench.action.gotoSymbol",
        "when": "editorFocus"
    },
    { // 跳转到行
        "key": "alt+g",
        "command": "workbench.action.gotoLine",
        "when": "editorFocus"
    },
    { // 切换上一个编辑器
        "key": "alt+h",
        "command": "workbench.action.previousEditor"
    },
    { // 切换下一个编辑器
        "key": "alt+l",
        "command": "workbench.action.nextEditor"
    },
    { // 跳转到定义
        "key": "alt+d",
        "command": "editor.action.revealDefinition",
        "when": "editorHasDefinitionProvider && editorTextFocus && !isInEmbeddedEditor"
    },
    { // 跳转到类型定义
        "key": "alt+shift+d",
        "command": "editor.action.goToTypeDefinition",
        "when": "editorHasReferenceProvider && editorTextFocus && !inReferenceSearchEditor && !isInEmbeddedEditor"
    },
    { // 跳转到实现
        "key": "alt+i",
        "command": "editor.action.goToImplementation",
        "when": "editorHasImplementationProvider && editorTextFocus && !isInEmbeddedEditor"
    },
    { // 跳转到引用
        "key": "alt+r",
        "command": "editor.action.goToReferences",
        "when": "editorHasReferenceProvider && editorTextFocus && !inReferenceSearchEditor && !isInEmbeddedEditor"
    },
    { // 跳转到引用面板
        "key": "shift+alt+r",
        "command": "references-view.findReferences",
        "when": "editorHasReferenceProvider"
    },
    { // 返回
        "key": "alt+j",
        "command": "workbench.action.navigateBack"
    },
    { // 前进
        "key": "alt+k",
        "command": "workbench.action.navigateForward"
    },
    { // 终端/工作区互跳
        "key": "alt+t",
        "command": "workbench.action.terminal.focus",
        "when": "editorFocus"
    },
    { // 终端/工作区互跳
        "key": "alt+t",
        "command": "workbench.action.focusFirstEditorGroup",
        "when": "terminalFocus",
    },
    { // 折叠切换
        "key": "alt+c",
        "command": "editor.toggleFold",
        "when": "editorTextFocus && foldingEnabled"
    },
    { // 全部折叠
        "key": "alt+a",
        "command": "editor.foldAll",
        "when": "editorTextFocus && foldingEnabled"
    },
    { // 全部展开
        "key": "alt+u",
        "command": "editor.unfoldAll",
        "when": "editorTextFocus && foldingEnabled"
    },
    { // 折叠到 1 
        "key": "alt+1",
        "command": "editor.foldLevel1",
        "when": "editorTextFocus && foldingEnabled"
    },
    { // 折叠到 2 
        "key": "alt+2",
        "command": "editor.foldLevel2",
        "when": "editorTextFocus && foldingEnabled"
    },
    { // 折叠到 3 
        "key": "alt+3",
        "command": "editor.foldLevel3",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "alt+w",
        "command": "workbench.action.showAllEditors"
    },
]

参考资料