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
#def my_fun(x, y): # return x + y
I am okay with using either
""" 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.
one way manually
:set number :10,12s/^/#
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
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 usually sweep out a visual block (
<C-V>), then search and replace the first character with:
(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:
I have the following lines in my
" 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*)#([^ ])/12/ge<CR>:s/^#$//ge<CR>:noh<CR>`n au BufEnter *.py inoremap <C-N>n <C-O>mn<C-O>:s/^(s*)#([^ ])/12/ge<CR><C-O>:s/^#$//ge<CR><C-O>:noh<CR><C-O>`n au BufEnter *.py vnoremap <C-N>n mn:s/^(s*)#([^ ])/12/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
# variable x #x = 0
# variable x x = 0
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:
# to comment your lines from the first column.
-# to uncomment the same way.
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.
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 command performs an action for every selected line. Commenting will insert a
# at the start of every line, and uncommenting will delete that
<leader>cc. Uncomment with
<leader>cu. And toggle comments with
<leader> key in vim is
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.:
And you will get a window of results. Alternatively you can just search vim-scripts for comment plugins.
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>
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.
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
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