J
J
JZorkiy2019-03-23 16:07:51
vim
JZorkiy, 2019-03-23 16:07:51

Vim connoisseurs: feature for commenting?

I just can't find how to write a function that, using the keyboard shortcut (ctrl-/), will do:
i//
but depending on the file extension.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentine, 2019-03-23
@JZorkiy

The logic is something like this, you need to finish it for multi-line comments

function RetFileType()
    let file_name = buffer_name("%")
    if file_name =~ '\.vim'
        return ["\"", ""]
    elseif __is_django_template() == 1
        return ['{% comment %}' , '{% endcomment %}']
    elseif file_name =~ '\.html$' || file_name =~ '\.xhtml$' || file_name =~ '\.xml'
        return ["<!--", "-->"]
    endif
    return ["#", ""]
endfunction
au BufEnter * let b:comment = RetFileType()
function! CommentLine()
    let stsymbol = b:comment[0]
    let endsymbol = b:comment[1]
    execute ":silent! normal 0i" . stsymbol . "\<ESC>A" . endsymbol . "\<ESC>"
endfunction
vmap  <C-c> :call CommentLine()<cr>
vmap  <C-u> :call UnCommentLine()<cr>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question