4

In VSCode I can comment my code with Ctrl kCtrl c and uncomment with Ctrl kCtrl u

Is there any way to do this using LunarVim or Neovim?

0

2 Answers 2

11

Because "commenting out" is context-dependent, the behavior is left to plugins rather than the core Vim editor.

Many plugins reuse the sequence gc to comment.

2
  • 2
    Man, why does tcomment never get any love?
    – Rich
    Sep 5 at 12:47
  • Maybe converting the readme into markdown will make it more appealing? Sep 5 at 12:54
2

The standard and advised approach is to use a plugin, like Vivian wrote.

If you feel like not using a plugin, you can do it in a few lines of code using autocmd like the snippet below. Select several lines in visual mode with V and press Ctrl-Slash or Shift-Ctrl-Slash to comment/uncomment.

The idea is simple. The buffer-local variable b:comment_symbol is defined corresponding to the file type (you can see with echo &ft). If you substitute b:comment_symbol for start-of-line, you comment a line. If you substitute nothing for b:comment_symbol at start-of-line, you uncomment a line.

(This is standard vim, sorry idk if it works in neovim or lunarvim)

augroup visual_commenting
    autocmd!
    autocmd FileType c,cpp,java,rust  let b:comment_symbol = '//'
    autocmd FileType vim              let b:comment_symbol = '"'
    autocmd FileType sh,vim,python    let b:comment_symbol = '#'
    autocmd FileType tex              let b:comment_symbol = '%'
    autocmd BufEnter * silent! vnoremap <silent> <C-_> :<C-u>keepp '<,'>s@^@\=b:comment_symbol<CR>
    autocmd BufEnter * silent! exec 'vnoremap <silent> <C-?> :<C-u>keepp ''<,''>s@^' . b:comment_symbol . '@<CR>'
augroup END
1
  • If there is no linewise comment e.g. like in coq, just substitute both start-of-line and end-of-line.
    – Hoblovski
    Apr 14 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.