Phil Gyford
Phil Gyford

Reputation: 14664

How to toggle multi-line comments with vim mode in Visual Studio Code

I'm using VS Code, on a Mac, using the Vim emulation.

If I have a .py file like this:

test1 = 1

test2 = 2

And I select all three lines, then press <cmd> and / I get this:

# test1 = 1

# test2 = 2

I would like the same functionality using a vim command sequence of <leader>, then c, then <space>.

In my settings.json I have this:

{
  "vim.leader": ",",
  "vim.normalModeKeyBindings": [
    {
      "before": ["<leader>", "c", "<space>"],
      "commands": ["editor.action.commentLine"],
      "when": "editorTextFocus && !editorReadonly"
    }
  ]
}

But, using that sequence, while I can select a single line to toggle commenting on/off, it does nothing when I have multiple lines selected. Why?

Upvotes: 4

Views: 9889

Answers (1)

Ken
Ken

Reputation: 71

Please try adding the following to your settings.json and let me know if it works for you. It worked for me.

"vim.visualModeKeyBindings": [
  {
    "before": ["<leader>", "c"],
    "commands": ["editor.action.commentLine"],
    "when": "editorTextFocus && !editorReadonly"
  }
],

Upvotes: 7

Related Questions