How to toggle comment visual line selection
See original GitHub issueI 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:
- Created 4 years ago
- Comments:34 (14 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@claudiodsf I was able to create a keybind like this for macOS in my VSCode
keybindings.json: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: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.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:
This works great, I would suggest to add one more condition
neovim.mode == visualso that it would also perfectly work in insert mode with vscode.