2

How to make Visual Studio Code move my cursor one line down, after commenting current line with ctrl + / shortcut? I tried (in user settings):

"vim.normalModeKeyBindings": [
    {
        "before": ["<C+/>"],
        "after": ["<C+/>", "j"]
    }
]

but it doesn't work.

Using Linux Manjaro + vscode 1.27.2

Kossak
  • 1,273
  • 1
  • 16
  • 31

2 Answers2

5

The solution for current vscode (1.48.0):

  1. Install this extension: geddski.macros
  2. Add this line to settings.json:
"macros": { "commentLine": ["editor.action.commentLine","cursorDown"] },
  1. Add these lines to keybindings.json:
{
    "key": "ctrl+/",
    "command": "macros.commentLine",
    "when": "editorTextFocus && !editorReadonly"
},

How to quickly open these files:

  • For keybindings.json press Ctrl+Shift+p and write Preferences: Open Keyboard Shortcuts (JSON)
  • For settings.json press above shortcut and write Preferences: Open Settings (JSON)

Physical location of files:
Linux:

  • ~/.config/Code/User/keybindings.json
  • ~/.config/Code/User/settings.json

Windows:

  • %UserProfile%\AppData\Roaming\Code\User\keybindings.json
  • %UserProfile%\AppData\Roaming\Code\User\settings.json

MacOS:

  • ~/Library/Application\ Support/Code/User/keybindings.json
  • ~/Library/Application\ Support/Code/User/settings.json
Kossak
  • 1,273
  • 1
  • 16
  • 31
  • I'll edit the answer to add the `keybindings.json` location in the answer, but till then use these below: – Farhad Jan 19 '21 at 12:31
  • for linux: `~/.config/Code/User/keybindings.json` – Farhad Jan 19 '21 at 12:32
  • for windows: `%UserProfile%\AppData\Roaming\Code\User\keybindings.json` – Farhad Jan 19 '21 at 12:32
  • Did'nt Work For Me Vs Code Throw An Error Command `macros.commentLine` Not Found – Farhad Jan 19 '21 at 12:45
  • @Farhad I edited the answer, adding proper macro to `settings.json`. Try it now. – Kossak Feb 05 '21 at 19:05
  • For those who are lazy to deal with changing 2 config JSONs, there is a dedicated extension that does the requested job _only_ and requires _zero config_: https://marketplace.visualstudio.com/items?itemName=shawtung.vscode-commentlinedown I've just spend 15min spotting my error after copy-pasting the answer above: on MacOS `ctrl` is `cmd` :) – vladZams Feb 07 '22 at 19:21
0

If you check the keybindings.json (Click the gear icon) search comment
You will find there is a Toggle Line Comment Command there.
So you may need to disable it first.

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57