tpope / vim-commentary

commentary.vim: comment stuff out
http://www.vim.org/scripts/script.php?script_id=3695
5.9k stars 214 forks source link

One nmap to rule them all #104

Closed soerenwolfers closed 5 years ago

soerenwolfers commented 5 years ago

Can I use vim-commentary to create a normal mode map, let's say to ', that does:

tpope commented 5 years ago

If you care about that "immediately" constraint, you're going to have to implement your own custom map that uses reltime() or something to figure out if the correct amount of time has elapsed.

soerenwolfers commented 5 years ago

@tpope Could you give me a hint at how to figure out, using the code you already wrote, the size of the current comment block (0 for current line is not commented, 1 for current line is commented but has no commented neighbors, >1 current line is commented and has commented neighbors)?

I think I have the rest ready (though it is kind of ugly, since it requires sacrificing another key for a new binding, here +)

nmap ' :call MyUncomment()
nmap + :call MyUncomment2()
namp +' :call MyUncomment3()
function! MyUncomment()
  let n = sizeofcommentblock()
  if n<2 
    call <Plug>CommentaryLine
  else 
    call feedkeys("+") 
  endif
endfunction
function! MyUncomment2() " No more ' was pressed -> uncomment line
  call <Plug>CommentaryLine
endfunction
function ! MyUncomment3() " Another press of ' -> uncomment block
 call <Plug>Commentary<Plug>Commentary
endfunction