Block comenting python code in vim

block commenting python tip vim

Recently, I find myself wanting to do block commenting for my python scripts more and more often. Sometimes just to hide a piece of code, or disable it while I'm debugging etc.

Of course, in python the best way to do this is by putting a `#` at the start of the line, but this process can be quite tedious if you have lots of code you wish to comment. I decided to let vim do the work for me.

I placed the script in my vim plugins folder, and then mapped F6 to the BlockComment() call, and F7 to call the NoBlockComment().

Now I can easilly comment out a single line, or multiple lines, and even uncomment them again.

Gotta love vim ^_^

Here is the script:

function! BlockComment()
python << EOF
import vim
for line in vim.current.range:
    vim.command("normal 0i#")
EOF
endfunction

function! NoBlockComment()
python << EOF
import vim
for line in vim.current.range:
    vim.command("normal 0x")
EOF
endfunction


There are currently no comments on this entry. Be the first!


You can use Markdown to format your comment.