<p>An alternative for plain vi, and if you have a small number of lines to change, is to do this</p>
<ol>
<li>put your cursor on the first line</li>
<li>make a note of how many lines you want to change (example 4)</li>
<li>:.,.+3s/^/#/</li>
</ol>
<p>What you are doing here is telling it to do a search and replace on the block marked out by the current line (. - dot) to the current line plus 3 (dot plus 3). If you have four lines to comment out, it will be current line plus 3 = .,.+3. If you have 10 lines it will be current line plus 9 = .,.+9.</p>
<p>When you do a search and replace, before the s you can specify the region that the search and replace is to apply to. One method of specifying the region is to use line numbers, like m,n = from line m to line n. Example 45,55.</p>
<p>There are a number of abbreviations that fit in there. $ = last line in the file. So if you want to apply it to the whole file, you use 1,$ = from line 1 to last line. Another abbreviation is . meaning the current line. Example .,$ meaning from the current line to the last line in the file.</p>
<p>You can also do relative numbers, like $ - 5 meaning up to the fifth last line. Or .+4 meaning current line plus 4. Or .-2 meaning current line minus 2.</p>