How to comment out a block of Python code in Vim

I was wondering if there was any key mapping in Vim to allow me to indent certain lines of code (whether those lines have been selected in visual mode, or n lines above/below current cursor position).

So basically something that converts the following

def my_fun(x, y):
    return x + y

to

#def my_fun(x, y):
#    return x + y

I am okay with using either # or """ for commenting out the relevant lines. Ideally, I would also like the same keymapping to uncomment the lines if the given lines have been commented out.


Step 1: Go to the the first column of the first line you want to comment.

初始状态

Step 2: Press: Ctrl+v and select the lines you want to comment:

选择线

Step 3: Shift- I #space (Enter Insert-at-left mode, type chars to insert. The selection will disappear, but all lines within it will be modified after Step 4.)

评论

Step 4: Esc

<Esc>键


单向手动

:set number
:10,12s/^/#

You could add the following mapping to your .vimrc

vnoremap <silent> # :s/^/#/<cr>:noh<cr>
vnoremap <silent> -# :s/^#//<cr>:noh<cr>

Highlight your block with:

Shift+v

# to comment your lines from the first column.

-# to uncomment the same way.

链接地址: http://www.djcxy.com/p/49458.html

上一篇: Vim:以可视方式选择文本块的更快方式

下一篇: 如何在Vim中注释掉一段Python代码