Skip to content

Instantly share code, notes, and snippets.

@sooop
Created March 3, 2014 05:07

Revisions

  1. sooop revised this gist Mar 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ function! CheckIsComment(line_number)
    let c = 0
    while c < strlen(sl)
    let d = c + strlen(s:comment_prefix) - 1
    " sl[c] is space or tabe?
    "sl[c] is space or tabe?
    if " \t" =~ sl[c]
    " ignore indentation
    " pass
  2. sooop revised this gist Mar 3, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    " toggle comment line

    function! SetCommentPrefix()
    let b:comment_prefix = "#"
    let s:comment_prefix = "#"
    if &filetype == "vim"
    " for vim, inline comment start with \"
    let b:comment_prefix = "\""
    let s:comment_prefix = "\""
    elseif &filetype ==? "c" || &filetype ==? "objc" || &filetype ==? "cpp"
    let b:comment_prefix = "//"
    let s:comment_prefix = "//"
    endif
    endfunction

    @@ -18,7 +18,7 @@ function! CommentLine(line_number)
    " move to seletced line
    call setpos(".", [0, a:line_number, 0, 0])
    " just insert comment prefix character at the front of given line
    exec "normal! I".b:comment_prefix
    exec "normal! I".s:comment_prefix
    "restore cursor position
    call setpos(".", cpos)
    endfunction
    @@ -30,7 +30,7 @@ function! UncommentLine(line_number)
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    " remove comment prefix charactor
    exec ".s/".escape(b:comment_prefix, b:comment_prefix[0])."//"
    exec ".s/".escape(s:comment_prefix, s:comment_prefix[0])."//"
    " restore cursor position
    call setpos(".", cpos)
    endfunction
    @@ -42,12 +42,12 @@ function! CheckIsComment(line_number)
    let sl = getline(a:line_number)
    let c = 0
    while c < strlen(sl)
    let d = c + strlen(b:comment_prefix) - 1
    let d = c + strlen(s:comment_prefix) - 1
    " sl[c] is space or tabe?
    if " \t" =~ sl[c]
    " ignore indentation
    " pass
    elseif sl[(c):(d)] == b:comment_prefix
    elseif sl[(c):(d)] == s:comment_prefix
    return 1
    else
    return 0
  3. sooop revised this gist Mar 3, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,7 @@ endfunction


    function! CheckIsComment(line_number)
    call SetCommentPrefix()
    " check the line for given line number is comment
    let sl = getline(a:line_number)
    let c = 0
  4. sooop revised this gist Mar 3, 2014. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -30,11 +30,7 @@ function! UncommentLine(line_number)
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    " remove comment prefix charactor
    if &filetype ==? 'c' || &filetype ==? "objc" || &filetype ==? "cpp"
    exec ".s/".escape(b:comment_prefix, '/')."//"
    else
    exec ".s/".b:comment_prefix."//"
    endif
    exec ".s/".escape(b:comment_prefix, b:comment_prefix[0])."//"
    " restore cursor position
    call setpos(".", cpos)
    endfunction
  5. sooop revised this gist Mar 3, 2014. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@ function! SetCommentPrefix()
    if &filetype == "vim"
    " for vim, inline comment start with \"
    let b:comment_prefix = "\""
    elseif &filetype ==? "c" || &filetype ==? "objc" || &filetype ==? "cpp"
    let b:comment_prefix = "//"
    endif
    endfunction

    @@ -17,7 +19,7 @@ function! CommentLine(line_number)
    call setpos(".", [0, a:line_number, 0, 0])
    " just insert comment prefix character at the front of given line
    exec "normal! I".b:comment_prefix
    " restore cursor position
    "restore cursor position
    call setpos(".", cpos)
    endfunction

    @@ -28,7 +30,11 @@ function! UncommentLine(line_number)
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    " remove comment prefix charactor
    exec ".s/".b:comment_prefix."//"
    if &filetype ==? 'c' || &filetype ==? "objc" || &filetype ==? "cpp"
    exec ".s/".escape(b:comment_prefix, '/')."//"
    else
    exec ".s/".b:comment_prefix."//"
    endif
    " restore cursor position
    call setpos(".", cpos)
    endfunction
    @@ -39,11 +45,12 @@ function! CheckIsComment(line_number)
    let sl = getline(a:line_number)
    let c = 0
    while c < strlen(sl)
    let d = c + strlen(b:comment_prefix) - 1
    " sl[c] is space or tabe?
    if " \t" =~ sl[c]
    " ignore indentation
    " pass
    elseif sl[c] == b:comment_prefix
    elseif sl[(c):(d)] == b:comment_prefix
    return 1
    else
    return 0
  6. sooop revised this gist Mar 3, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -3,36 +3,46 @@
    function! SetCommentPrefix()
    let b:comment_prefix = "#"
    if &filetype == "vim"
    " for vim, inline comment start with \"
    let b:comment_prefix = "\""
    endif
    endfunction


    function! CommentLine(line_number)
    call SetCommentPrefix()
    " remember current cursor position
    let cpos = getpos(".")
    " move to seletced line
    call setpos(".", [0, a:line_number, 0, 0])
    " just insert comment prefix character at the front of given line
    exec "normal! I".b:comment_prefix
    " restore cursor position
    call setpos(".", cpos)
    endfunction

    function! UncommentLine(line_number)
    call SetCommentPrefix()
    " remember current cursor position
    let cpos = getpos(".")
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    " remove comment prefix charactor
    exec ".s/".b:comment_prefix."//"
    " restore cursor position
    call setpos(".", cpos)
    endfunction


    function! CheckIsComment(line_number)
    " check the line for given line number is comment
    let sl = getline(a:line_number)
    let c = 0
    while c < strlen(sl)
    " sl[c] is space or tabe?
    if " \t" =~ sl[c]
    "pass
    " ignore indentation
    " pass
    elseif sl[c] == b:comment_prefix
    return 1
    else
    @@ -55,6 +65,7 @@ endfunction
    function! ToggleCommentRange()
    let line_begin = line("'<")
    let line_end = line("'>")
    " decide mode with first line in selection
    let mode_ = CheckIsComment(line_begin)
    let cpos = getpos(".")
    let i = line_begin
  7. sooop revised this gist Mar 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ function! UncommentLine(line_number)
    let cpos = getpos(".")
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    exec ".s/".b:comment_prefix."\\+//"
    exec ".s/".b:comment_prefix."//"
    call setpos(".", cpos)
    endfunction

  8. sooop created this gist Mar 3, 2014.
    72 changes: 72 additions & 0 deletions toggleComment.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    " toggle comment line

    function! SetCommentPrefix()
    let b:comment_prefix = "#"
    if &filetype == "vim"
    let b:comment_prefix = "\""
    endif
    endfunction


    function! CommentLine(line_number)
    call SetCommentPrefix()
    let cpos = getpos(".")
    " move to seletced line
    call setpos(".", [0, a:line_number, 0, 0])
    exec "normal! I".b:comment_prefix
    call setpos(".", cpos)
    endfunction

    function! UncommentLine(line_number)
    call SetCommentPrefix()
    let cpos = getpos(".")
    "move to selected line
    call setpos(".", [0, a:line_number, 0, 0])
    exec ".s/".b:comment_prefix."\\+//"
    call setpos(".", cpos)
    endfunction


    function! CheckIsComment(line_number)
    let sl = getline(a:line_number)
    let c = 0
    while c < strlen(sl)
    if " \t" =~ sl[c]
    "pass
    elseif sl[c] == b:comment_prefix
    return 1
    else
    return 0
    endif
    let c += 1
    endwhile
    return 0
    endfunction

    function! ToggleCommentLine()
    let cl = line(".")
    if CheckIsComment(cl)
    call UncommentLine(cl)
    else
    call CommentLine(cl)
    endif
    endfunction

    function! ToggleCommentRange()
    let line_begin = line("'<")
    let line_end = line("'>")
    let mode_ = CheckIsComment(line_begin)
    let cpos = getpos(".")
    let i = line_begin
    while i < line_end + 1
    if mode_
    call UncommentLine(i)
    else
    call CommentLine(i)
    endif
    let i+=1
    endwhile
    endfunction

    nnoremap <leader>\ :call ToggleCommentLine()<cr>
    vnoremap <leader>\ <esc> :call ToggleCommentRange()<cr>