[visual-studio-code] Duplicate line in Visual Studio Code

It's possible to create keybindings that are only active when Vim for VSCode is on and in a certain mode (i.e., "Normal", "Insert", or "Visual").

To do so, use Ctrl + Shift + P to open up VSCode's Command Palette, then search for "Preferences: Open Keyboard Shortcuts (JSON)"--selecting this option will open up keybindings.json. Here, custom bindings can be added.

For example, here are the classic VSCode commands to move/duplicate lines tweaked for ease of use in Vim..

    [
      {
        "key": "alt+j",
        "command": "editor.action.moveLinesDownAction",
        "when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
      },
      {
        "key": "alt+shift+j",
        "command": "editor.action.copyLinesDownAction",
        "when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
      },
      {
        "key": "alt+k",
        "command": "editor.action.moveLinesUpAction",
        "when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
      },
      {
        "key": "alt+shift+k",
        "command": "editor.action.copyLinesUpAction",
        "when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
      },
    ]

Now we can use these Vim-friendly commands in VSCode!

  • Alt + J to move a line down
  • Alt + K to move a line up
  • Shift + Alt + J to duplicate a line down
  • Shift + Alt + K to duplicate a line up