A text editor like vi or vim can be used to comment multiple lines in a shell script (or any other script or configuration file) in a single go rather than adding # or whatever that goes for a comment for the script in question.
Open the file to be edited using vi/vim using the command vi filename (where filename can be replaced by the name of the file), now enter the command mode by pressing: .In order to comment multiple lines ( i.e. to add a # to the beginning of the line), say lines from 5 to 10 in the script, type the command
:5,10s/^/#/ # : in the command is part of entering the editing/ex mode in vi.
Note# The command : set nu can be used to view line numbers in vi/vim
Clarification
General Syntax
==============================================
:%s/r1/r2/ is the syntax used for string substitution in vi/vim
Syntax split
: Enter command mode
% Perform the command on all lines
s Short form for :substitute command
r1 Can be replaced with any string in the script/file that is to be replaced
r2 Can be replaced with any string which should replace the string r1 in the script/file
/ Used a separator
Changes from syntax in executed command
============================================
In the command that we used for substitution/adding comments to lines starting from 5 to 10
% has been replaced with 5,10 so that the command will be executed only for lines starting from 5 to 10
^ caret [^ ] matches the position before the first character in the string (hence in the command that we executed the substitution takes place for the position before the first character[=r1])
# Replacement for r2 in syntax (i.e. # is used as the character/string which substitutes r1[position before the first character])
Comment removal
In order to uncomment multiple lines (we will remove # from the lines that we have commented, i.e. from 5 to 10) we can use the command
:5,10s/^#//
Clarification
^# Matches all characters starting with # (since ^ means position before the first character, ^x [x can be replaced with any character which does not hold a special meaning in bash, or \x if x has got a special meaning] will match any line which has got x as the character at the first character)
// Two /s without any in between space :: hence substitution will completely remove # from the beginning without adding anything
Commenting all lines in a script/file
Open the file in vi/vim and use the command
:%s/^/#/g
g stands for global (so that the substitution will occur globally, i.e. for all lines in the script)
All the rest have already been explained in General Syntax
Note: For php files which treat ; as the commenting character instead of # , # can be replaced with ; i.e. for commenting all lines in a php file we can use the command %s/^/;/g
If you require help, contact SupportPRO Server Admin