Skip to content

Instantly share code, notes, and snippets.

@dave-kennedy
Forked from voyeg3r/togglecomment.vim
Last active March 29, 2022 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave-kennedy/2188b3dd839ac4f73fe298799bb15f3b to your computer and use it in GitHub Desktop.
Save dave-kennedy/2188b3dd839ac4f73fe298799bb15f3b to your computer and use it in GitHub Desktop.
" source: https://stackoverflow.com/a/24046914/2571881
let s:comment_map = {
\ "c": '\/\/',
\ "cpp": '\/\/',
\ "go": '\/\/',
\ "java": '\/\/',
\ "javascript": '\/\/',
\ "lua": '--',
\ "scala": '\/\/',
\ "php": '\/\/',
\ "python": '#',
\ "ruby": '#',
\ "rust": '\/\/',
\ "sh": '#',
\ "desktop": '#',
\ "fstab": '#',
\ "conf": '#',
\ "profile": '#',
\ "bashrc": '#',
\ "bash_profile": '#',
\ "mail": '>',
\ "eml": '>',
\ "bat": 'REM',
\ "ahk": ';',
\ "vim": '"',
\ "tex": '%',
\ }
function! ToggleComment()
if has_key(s:comment_map, &filetype)
let comment_leader = s:comment_map[&filetype]
if getline('.') =~ '^\s*$'
" Skip empty line
return
endif
if getline('.') =~ '^\s*' . comment_leader
" Uncomment the line
execute 'silent s/\v\s*\zs' . comment_leader . '\s*\ze//'
else
" Comment the line
execute 'silent s/\v^(\s*)/\1' . comment_leader . ' /'
endif
else
echo "No comment leader found for filetype"
endif
endfunction
nnoremap <Leader>t :call ToggleComment()<CR>
vnoremap <Leader>t :call ToggleComment()<CR>
@OrionRandD
Copy link

you forgot the lisp family langs:
elisp, lisp, clojure...
Default comment is ";;" semicolon twice
Could you add them? Prolog and Cobol as well?
Thx....

@dave-kennedy
Copy link
Author

you forgot the lisp family langs:
elisp, lisp, clojure...
Default comment is ";;" semicolon twice
Could you add them? Prolog and Cobol as well?
Thx....

Actually @voyeg3r finally merged my changes so I'd redirect your suggestion there :)

Copy link

ghost commented Jun 30, 2021

I made a suggestion in voyeg3r gist
please check it out

@OrionRandD
Copy link

OrionRandD commented Jun 30, 2021 via email

Copy link

ghost commented Jul 2, 2021 via email

@dave-kennedy
Copy link
Author

@backermanbd @OrionRandD I just found vim-commentary. I will probably use that going forward instead of maintaining this. It has better community support, isn't much larger than this, and uses Vim's 'commentstring' option instead of a list of hard coded file types so it automatically works with a lot more languages.

Copy link

ghost commented Jul 4, 2021

@dave-kennedy I tried vim-commentary. But didn't like it though.
I just wanted voyeg3r & your code to put comment out signs at the first of each line

like this cpp code

// #include <iostream>
// using namespace std;

// int main(){
//     cout << "hello world";
// }

Copy link

ghost commented Jul 4, 2021

@dave-kennedy please help me add the feature..

@anqin
Copy link

anqin commented Mar 29, 2022

@backermanbd @dave-kennedy
+1, CPP developers need this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment