提升生产力:用Vim快捷键玩转VSCode

809 阅读3分钟

最近写代码的时间比较多主要是写TypeScript,就配置了一套vscode+vim的快捷键,再加上IDEA Keybindings插件已经完全脱离鼠标了,写代码飞起,今天分享给大家。

安装插件

在vscode扩展中安装vim插件 image.png 我习惯idea的快捷键了所以装了一个idea的快捷键 image.png

修改快捷键

修改快捷键整体分为两部分1. 代码区热键修改setting.json文件。2. 非代码区热键修改keybinding.json文件。

代码区热键

点开设置然后转到json文件 image.png 先整体配置一下

    // 绑定vim前导键
    "vim.leader": " ",
    // 开启easymotion插件
    "vim.easymotion": true,
    // 启用系统粘贴板作为vim寄存器,我同时用ctrl+c和y复制ctrl+v和p粘贴,不开启会复制的不互通
    "vim.useSystemClipboard": true,
    // 高亮展示search的结果
    "vim.hlsearch": true,
    // 交由vscode处理的按键,而不是vim
    "vim.handleKeys": {
        "<C-a>": false,
        "<C-f>": false,
        "<C-d>": true,// 习惯ctrl+d向下复制当前行了
        "<C-s>": false,
        "<C-z>": false,
        "<C-c>": false,
        "<C-v>": false,
        "<C-y>": false,
        "<C-x>": false
    }
  • jj:退出insert模式
  • 空格 + t 跳转到控制台
  • hh:跳转到行首,取代^
  • ll:跳转到行尾,取代$
  • zz:代码块折叠/打开
  • g[:跳转到上一个问题
  • g]:跳转到下一个问题
{ // 插入模式下的非递归按键绑定
  "vim.insertModeKeyBindings": [
    {
      "before": [
        "j",
        "j"
      ],
      "after": [
        "<Esc>"
      ]
    },
  ],
  //    普通模式下的非递归按键绑定
  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before": [
        "<leader>",
        "t"
      ],
      "commands": [
        "workbench.action.terminal.focus"
      ]
    },
    {
      "before": [
        "h",
        "h"
      ],
      "after": [
        "^"
      ]
    },
    {
      "before": [
        "l",
        "l"
      ],
      "after": [
        "$"
      ]
    },
    {
      "before": [
        "z",
        "z"
      ],
      "commands": [
        "editor.toggleFold"
      ]
    },
    {
      "before": [
        "g",
        "[",
      ],
      "commands": [
        "editor.action.marker.prevInFiles"
      ]
    },
    {
      "before": [
        "g",
        "]",
      ],
      "commands": [
        "editor.action.marker.nextInFiles"
      ]
    },
  ],
}

全局快捷键

image.png 在keybinding.json中设置

  • ctrl+h:代码帮助提示
  • ctrl+j:弹出/关闭参数提示
  • ctrl+k:弹出/关闭 代码建议
  • ctrl+n:移动到下一个建议:ctrl+k或者自动弹出代码提示之后,使用ctrl+n来替代↓下选择建议
  • ctrl+N:移动到上一个建议
  • i:在资源管理器中新建文件
  • shift + i:在资源管理器中新建目录
  • r:刷新资源管理器
  • m:重命名/目录
  • d:删除文件/目录
  • x:剪切/目录
  • y:复制/目录
  • p:粘贴文件/目录
  • ctrl+l:拆分左右分栏
  • ctrl+w:关闭当前标签页或者分栏
// 将键绑定放在此文件中以覆盖默认值
[
    // --- 代码编辑命令
     // 触发帮助提示
     {
        "key": "ctrl+h",
        "command": "editor.action.showHover",
        "when": "editorTextFocus"
    },
    // 触发参数提示
    {
        "key": "ctrl+j",
        "command": "editor.action.triggerParameterHints",
        "when": "editorHasSignatureHelpProvider && editorTextFocus"
    },
    {
        "key": "ctrl+j",
        "command": "closeParameterHints",
        "when": "editorFocus && parameterHintsVisible"
    },
    // 触发建议提示
    {
        "key": "ctrl+k",
        "command": "editor.action.triggerSuggest",
        "when": "editorHasCompletionItemProvider && textInputFocus && !editorReadonly"
    },
    {
        "key": "ctrl+k",
        "command": "hideSuggestWidget",
        "when": "suggestWidgetVisible && textInputFocus"
    },
    // 移动到下一个建议
    {
        "key": "ctrl+n",
        "command": "selectNextSuggestion",
        "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
    },
    // 移动到上一个建议
    {
        "key": "ctrl+shift+n",
        "command": "selectPrevSuggestion",
        "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
    },
    // --- 资源管理器中对文件或目录的操作
     // 新建文件
     {
        "key": "i",
        "command": "explorer.newFile",
        "when": " explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus "
    },
    // 新建目录
    {
        "key": "shift+i",
        "command": "explorer.newFolder",
        "when": " explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus "
    },
    // 刷新资源管理器
    {
        "key": "r",
        "command": "workbench.files.action.refreshFilesExplorer",
        "when": " explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus "
    },
    // 重命名文件或目录
    {
        "key": "m",
        "command": "renameFile",
        "when": " explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus "
    },
    // 删除文件或目录
    {
        "key": "d",
        "command": "deleteFile",
        "when": " explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus "
    },
    // 剪切文件或目录
    {
        "key": "x",
        "command": "filesExplorer.cut",
        "when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus"
    },
    // 复制文件或目录
    {
        "key": "y",
        "command": "filesExplorer.copy",
        "when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !inputFocus"
    },
    // 粘贴文件或目录
    {
        "key": "p",
        "command": "filesExplorer.paste",
        "when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceReadonly && !inputFocus"
    },
      // 拆分一个左右分屏
    {
        "key": "ctrl+l",
        "command": "workbench.action.splitEditor"
    },
      // 关闭当前选项卡或分屏
    {
        "key": "ctrl+w",
        "command": "workbench.action.closeActiveEditor"
    },
]

其他

vscode-vim或者IDEA Keybindings插件提供的快捷键。

  • gd:(go to definition)查看接口定义
  • gi:(go to implementations) 查看实现
  • gh:查看提示
  • gb:开启多光标模式,选中和当前单词相同的单词
  • gt:向后切换标签页。或者ctrl+tab
  • gcc:注释当前行
  • alt+1:打开/关闭资源管理器,使用hjkl来移动,使用空格或者回车键打开文件
  • ctrl+1/2/n:在分栏中切换光标

easymotion

开启vscode 的vim的easymotion,可以将leader替换为空格键,这样就可以简化我们的操作了image.png

vim-surround

开启surround。来快速操作环绕操作 image.png

这套快捷键操作用了有一段时间了,写代码已经不用鼠标了,写代码飞起