question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to toggle comment visual line selection

See original GitHub issue

I am trying to add a keybinding for <C-/> that allows me to toggle comments for any number of lines in visual line mode. Here’s what I tried:

function! g:toggleComment()
	normal! gv
	let startline = line("v")
	let endline = line(".")
	call VSCodeNotifyRange('editor.action.commentLine', startline, endline, 1)
endfunction
vnoremap <C-/> :call g:toggleComment()<cr>

But nothing happens when I press <C-/>. I have added && neovim.mode != ‘visual’ to the when expression of the existing vscode keybinding of <C-/>.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:34 (14 by maintainers)

github_iconTop GitHub Comments

28reactions
ian-h-chamberlaincommented, Jun 8, 2020

@claudiodsf I was able to create a keybind like this for macOS in my VSCode keybindings.json:

    {
        "key": "cmd+/",
        "command": "vscode-neovim.send",
        "when": "editorTextFocus && neovim.init",
        "args": "<C-/>"
    },

For the benefit of anyone else coming to this issue, I finally figured the solution to another problem that was bothering me – the de-selection of the commented lines after toggling. The following restores the visual selection after <C-/>, which allows you to toggle on and off the selected lines without re-selecting them. This matches the behavior I was used to coming from VscodeVim.

.vimrc / init.vim:

if exists('g:vscode')
    xmap <C-/> <Plug>VSCodeCommentarygv
    nmap <C-/> <Plug>VSCodeCommentaryLinegv
endif

Based on this Stackoverflow it seems like there is a common practice to name <Plug> bindings with parentheses, which would make these binds slightly more clear to read, e.g.

    xmap <C-/> <Plug>(VSCodeCommentary)gv
    nmap <C-/> <Plug>(VSCodeCommentaryLine)gv

This would be a trivial change to vscode-code-actions.vim. if the maintainers are interested in supporting configuration like this I’d be happy to open a PR adding the parenthesized names while keeping the old ones for backwards compatibility.

Edit: actually I really only want this behavior in visual mode so I ended up with:

    xmap <C-/> <Plug>VSCodeCommentarygv
    nmap <C-/> <Plug>VSCodeCommentaryLine
9reactions
jukrb0xcommented, Nov 7, 2021

@claudiodsf I was able to create a keybind like this for macOS in my VSCode keybindings.json:

    {
        "key": "cmd+/",
        "command": "vscode-neovim.send",
        "when": "editorTextFocus && neovim.init",
        "args": "<C-/>"
    },

For the benefit of anyone else coming to this issue, I finally figured the solution to another problem that was bothering me – the de-selection of the commented lines after toggling. The following restores the visual selection after <C-/>, which allows you to toggle on and off the selected lines without re-selecting them. This matches the behavior I was used to coming from VscodeVim.

.vimrc / init.vim:

if exists('g:vscode')
    xmap <C-/> <Plug>VSCodeCommentarygv
    nmap <C-/> <Plug>VSCodeCommentaryLinegv
endif

Based on this Stackoverflow it seems like there is a common practice to name <Plug> bindings with parentheses, which would make these binds slightly more clear to read, e.g.

    xmap <C-/> <Plug>(VSCodeCommentary)gv
    nmap <C-/> <Plug>(VSCodeCommentaryLine)gv

This would be a trivial change to vscode-code-actions.vim. if the maintainers are interested in supporting configuration like this I’d be happy to open a PR adding the parenthesized names while keeping the old ones for backwards compatibility.

Edit: actually I really only want this behavior in visual mode so I ended up with:

    xmap <C-/> <Plug>VSCodeCommentarygv
    nmap <C-/> <Plug>VSCodeCommentaryLine

This works great, I would suggest to add one more condition neovim.mode == visual

  {
    "key": "cmd+/",
    "command": "vscode-neovim.send",
    "when": "editorTextFocus && neovim.init && neovim.mode == visual",
    "args": "<C-/>"
  }

so that it would also perfectly work in insert mode with vscode.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toggle Comment - Visual Studio Marketplace
Toggle Comment is a simple visual studio extension to comment out/uncomment the selected lines. This is the same feature as "Ctrl + /"...
Read more >
visual studio C++ toggle comment ? comment while not whole ...
As for question one, it's the same shortcut pair: Ctrl+K, Ctrl+C to toggle any comments on, Ctrl+K, Ctrl+U to toggle any comments off....
Read more >
How to make CTRL + / toggle a comment in Visual Studio
You can customize the keyboard shortcuts by going int the "Tools" menu and selecting "Options". Then select "Keyboard" from the "Environment" ...
Read more >
How to toggle comment visual line selection #199 - GitHub
You put your cursor on line 3, press shift-v to enter visual line mode and press k twice. Now all three lines are...
Read more >
Ctrl + / to TOGGLE comment? : r/VisualStudio - Reddit
Ctrl + / to TOGGLE comment? · Goto Tools > Options > Keyboard · Select Edit.ToggleLineComment or Edit.ToggleBlockComment (whichever you want) ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found