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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
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.
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