0

I got this code from voyeg3r's gist
@d-ben-knoble helped me add a feature I wanted in this QA.

Now I want this code to support commenting out certain filetypes like conf, mail, tex and so on.

My edited vimrc code:

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/^' . comment_leader . '//'
        else
            " Comment the line
            execute 'silent s/^/\1' . comment_leader . ' /'
        endif
    else
        echo "No comment leader found for filetype"
    endif
endfunction

nnoremap <C-C> :call ToggleComment()<CR>
vnoremap <C-C> :call ToggleComment()<CR>

When I try to comment out this kitty.conf code, it doesn't work. Instead it shows "No comment leader found for filetype" as per my vimrc code

map ctrl+v        paste_from_clipboard
map shift+insert paste_from_clipboard

font_family      Hack Regular Nerd Font
bold_font        Hack Bold Nerd Font
italic_font      Hack Italic Nerd Font
bold_italic_font Hack Bold Italic Nerd Font
8
  • Please give details on what your problem is. You say you want it to work for those filetypes; they are configured in the dictionary. Is something not working? If so, please provide a recipe that demonstrates this. It might look like “1. Edit foo.conf 2. Insert hello world 3. Press C-c. It should comment out the line but instead X happens”
    – D. Ben Knoble
    Jul 4 at 13:43
  • thanks for the update though
    – Mega Bang
    Jul 4 at 13:49
  • i am using gvim on windows.. i tried :debug.. but it doesn't work.. vim returns it as error
    – Mega Bang
    Jul 4 at 14:06
  • For example, :debug call ToggleComment() ?
    – D. Ben Knoble
    Jul 4 at 14:17
  • it shows this i.imgur.com/66qTg40.jpg
    – Mega Bang
    Jul 4 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.