To comment out one or more lines in Vim, you can use the :
command followed by the s
command and a search pattern. For example, to comment out all lines that contain the word “hello”, you can use the following command:
:s/hello/# hello/g
This will replace every occurrence of the word “hello” with # hello
.
To uncomment a line or lines, you can use a similar command but replace the #
with a space. For example, to uncomment all lines that contain the word “hello”, you can use the following command:
:s/# hello/hello/g
This will replace every occurrence of # hello
with just “hello”.
You can also use the :
command followed by the g
command to comment out or uncomment multiple lines at once. For example, to comment out the current line and the two lines below it, you can use the following command:
:3,5s/^/#/
This will add a #
character to the beginning of each line in the range 3,5
.
To uncomment the same lines, you can use the following command:
:3,5s/^#//
This will remove the #
character from the beginning of each line in the range 3,5
.
Note that these commands may not work if the comment character for your programming language is something other than #
. In that case, you will need to use the appropriate comment character in the search pattern.