Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.
The most straight-forward way to comment out a block of code in Python is to use the # character. Any Python statement that begins with a hashtag will be treated as a comment by the compiler. There's no end to how many block comments you can have, in a row or otherwise.
You start your editor, navigate down to the beginning row of the block you want to comment out. You hit i to go into insert mode, enter // to comment, hit ESC to go back to command mode, hit j to navigate down to the next row, and then repeat until all the rows are commented out.
The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With