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 """
f开发者_如何学Pythonor 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
one way manually
: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.
Highlight your block with: ShiftV
Comment the selected block out with: :norm i#
(lower case i)
To uncomment, highlight your block again, and uncomment with: :norm ^x
The :norm
command performs an action for every selected line. Commenting will insert a #
at the start of every line, and uncommenting will delete that #
.
I usually sweep out a visual block (<C-V>
), then search and replace the first character with:
:'<,'>s/^/#
(Entering command mode with a visual block selected automatically places '<,'> on the command line) I can then uncomment the block by sweeping out the same visual block and:
:'<,'>s/^#//
There are some good plugins to help comment/uncomment lines. For example The NERD Commenter.
Sample shortcuts from the NERD Commenter:
[count]|<Leader>|cc |NERDCommenterComment|
Comment out the current line or text selected in visual mode.
[count]|<Leader>|cu |NERDCommenterUncomment|
Uncomments the selected line(s).
Full documentation is located here.
I have the following lines in my .vimrc
:
" comment line, selection with Ctrl-N,Ctrl-N
au BufEnter *.py nnoremap <C-N><C-N> mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>`n
au BufEnter *.py inoremap <C-N><C-N> <C-O>mn<C-O>:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap <C-N><C-N> mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>gv`n
" uncomment line, selection with Ctrl-N,N
au BufEnter *.py nnoremap <C-N>n mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>:s/^#$//ge<CR>:noh<CR>`n
au BufEnter *.py inoremap <C-N>n <C-O>mn<C-O>:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR><C-O>:s/^#$//ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap <C-N>n mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>gv:s/#\n/\r/ge<CR>:noh<CR>gv`n
The shortcuts preserve your cursor position and your comments as long as they start with #
(there is space after #). For example:
# variable x
x = 0
After commenting:
# variable x
#x = 0
After uncomennting:
# variable x
x = 0
No plugins or mappings required. Try the built-in "norm" command, which literally executes anything you want on every selected line.
Add # Comments
1. shift V to visually select lines
2. :norm i#
Remove # Comments
1. visually select region as before
2. :norm x
Or if your comments are indented you can do :norm ^x
Notice that these are just ordinary vim commands being preceded by ":norm" to execute them on each line.
More detailed answer for using "norm" command in one of the answers here
What's a quick way to comment/uncomment lines in Vim?
NERDcommenter is an excellent plugin for commenting which automatically detects a number of filetypes and their associated comment characters. Ridiculously easy to install using Pathogen.
Comment with <leader>cc
. Uncomment with <leader>cu
. And toggle comments with <leader>c<space>
.
(The default <leader>
key in vim is \
)
Frankly I use a tcomment plugin for that link. It can handle almost every syntax. It defines nice movements, using it with some text block matchers specific for python makes it a powerful tool.
There's a lot of comment plugins for vim - a number of which are multi-language - not just python. If you use a plugin manager like Vundle then you can search for them (once you've installed Vundle) using e.g.:
:PluginSearch comment
And you will get a window of results. Alternatively you can just search vim-scripts for comment plugins.
As mentioned in other answers NERDCommenter is good one - for more info on using it see this answer. Note: That the <leader>
key is usually \. E.g. so to comment out a line - type: \cc
A very minimal light weight plugin: vim-commentary.
gcc to comment a line
gcgc to uncomment. check out the plugin page for more.
v+k/j highlight the block then gcc to comment that block.
CtrlK for comment (Visual Mode):
vnoremap <silent> <C-k> :s#^#\##<cr>:noh<cr>
CtrlU for uncomment (Visual Mode):
vnoremap <silent> <C-u> :s#^\###<cr>:noh<cr>
精彩评论