-
-
Save voyeg3r/223f148a115c17a02a15660cc7335f4c 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> |
@backermanbd I think I understand your first point. If I have this code:
static int do_something() {
struct dir* d = malloc(sizeof(*d));
if (!d) {
return -ENOMEM;
}
And I put my cursor on line 3 and do gc}
, this is the result:
static int do_something() {
struct dir* d = malloc(sizeof(*d));
// if (!d) {
// return -ENOMEM;
// }
For comparison, if I put my cursor on on line 4:
static int do_something() {
struct dir* d = malloc(sizeof(*d));
// if (!d) {
// return -ENOMEM;
// }
You want it to behave like the second result regardless of whether you had your cursor on line 3 or 4.
If I were you I'd create an issue here: https://github.com/tpope/vim-commentary/issues. Chances are good you're not the only person who finds this behavior irksome. You'll probably get more traction there. Maybe I'll even take a poke at it. Or you can fix it yourself and help everybody. This is the strength of open source software.
As for your second point, try putting this in your .vimrc:
autocmd BufEnter *.c,*.cpp setlocal commentstring=//\ %s
Or create a file under ~/.vim/after/ftplugin/c.vim
with these contents:
setlocal commentstring=//\ %s
Make sure it's exactly like that, including the %s
at the end.
@backermanbd have you fixed the issue?
I have same problem when I used in CPP files.
To @mizlan The fixing has some bugs
What are the bugs?
When uncomment the lines, the indent spaces is dismissed. For example,
static int do_something() {
struct dir* d = malloc(sizeof(*d));
// if (!d) {
// return -ENOMEM;
// }
After uncommented,
static int do_something() {
struct dir* d = malloc(sizeof(*d));
if (!d) {
return -ENOMEM;
}
Ah. I never addressed the uncomment portion of the original code in my diff.
Change the uncomment code to:
execute 'silent s/\v\s*\zs' . comment_leader . ' \ze//'
(i.e., change the match-any-whitespace to a match-space regex.
@dave-kennedy I tried..
vim-commentary puts comment out signs at the start of each line.
otherwise it doesn't
PS: i couldn't make the commentstring
//
of cpp filetried this
//\
from this issuestill no luck