How to comment 20 lines of code at a time from a python file in vi/vim.
What command can I use instead of using # or """ ??
4 Answers
In vi:
- navigate to the line where you wish to start commenting out
- enter command mode (Esc then :)
- type
.,+20s/^/#/or.,+20s/^/"""/and hit Enter
What I typically do is this:
Leave editing mode Esc, may need to hit couple times
Press Shift+v to enter "Visual" mode
Highlight the desired lines via arrow keys, or use 3j to select 3 lines down or 3k 3 lines up
Enter command mode via :, and when you see
:'<,'>displayed type ins/^/#/, and then hit Enter
This is visual approach, sort of like using a mouse in GUI to highlight lines, except without a mouse in vim
If you are using a Python IDE (As PyCharm) you can select these lines and use Ctrl+/ (or the default "comment" shortcut) to comment them.
If you are using a common text editor either you have to comment line per line or use the multi-line string (triple quotes) as a multi-line comment.
3Vim can comment and uncomment multiple lines at once for specific lines range. Kindly check below example. here we will comment and uncomment from line numbers 7 to 13. Open file in vim
#vim /etc/hostsrun below two commands one by one
:se nu
:7,13s/^/#done. for uncomment, run below one command
:7,13s/^#//