Answers to: How do you comment out several lines in vi at once?http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once<p>Lets say I have a block of text:</p> <pre><code>def blarg_func: blarg = 1 return blarg </code></pre> <p>and I want to comment it all out in vi</p> <pre><code>#def blarg_func: # blarg = 1 # return blarg </code></pre> <p>What I usually do is go to the first line and hit "^i" and then enter "#". next I arrow down to the next line and hit "." and then repeat until all the lines are commented. </p> <p>If I hit "2" then "." it will give me.</p> <pre><code>#def blarg_func: ## blarg = 1 return blarg </code></pre> <p>I know I can do this with numbered lines, like </p> <pre><code>:1,3s/^/#/ </code></pre> <p>but I want to be able to do this without having to enter line numbers.</p> <p>Keep in mind I am using vi not vim, so "^v" will not work. </p>enWed, 16 Jul 2014 11:44:43 -0400Answer by Mike Renohttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/3372<p>Remember VI uses sed and awk commands.<br> </p> <p>Research how to do what you want to do with sek and awk commands and you will be way ahead of a VI expert. Visit ora.com to get the best technical books for linux users. O'Reilly permits you purchase directly from them and will sell you the next revision of the book at a discount.</p>Mike RenoWed, 16 Jul 2014 11:44:43 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/3372Answer by Anilhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/3358<p>For me with MAC running UBUNTU/Centos VMs only working options are vim, crtl+v, crtl+i,shift+i,#,esc </p>AnilMon, 16 Jun 2014 16:33:22 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/3358Answer by olejorgenbhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/349<p>Use repeat last substitute maybe. (unless that's vim specific)</p> <pre><code>:s/^/#/ &lt;return&gt; j3&amp; </code></pre> <p>will comment out 4 lines</p>olejorgenbThu, 06 May 2010 21:41:19 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/349Answer by codebunnyhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/348<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>codebunnyThu, 06 May 2010 21:40:50 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/348Answer by codebunnyhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/347<p>For plain standard vi, do this:</p> <ol> <li>Go to the starting line of the block</li> <li>ma (to mark the start of the block)</li> <li>Move to the end of the block.</li> <li>:'a,.s/^/#/</li> </ol> <p>Basically, you go to to the first line of the block and mark it and you may as well use register a. So that's ma.</p> <p>Then you move to the end of the block and you do a search and replace.</p> <p>: puts you into ex mode</p> <p>'a is the start of the search block and it refers to the mark you made earlier</p> <p>. is the current line</p> <p>so the whole block is described by 'a,. (dash a comma dot)</p> <p>then you replace the start of the line with a #, but only for that specified block (s/^/#/)</p> <p>This is standard vi, so it will work for you.</p>codebunnyThu, 06 May 2010 20:54:28 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/347Answer by Mike 1http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/345<p>Quicker, no custom macros needed:</p> <ol> <li>Press shift-V to go into block selection mode</li> <li>Select the block you want to comment out</li> <li>Press: shift-i # ESC</li> <li>Profit</li> </ol> <p>Shift - I will take you to the beginning of the line, then add your # character. Escape, and it's applied to the entire highlighted block.</p> <p>This may be vim specific.</p>Mike 1Thu, 06 May 2010 20:42:31 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/345Answer by SiegeXhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/343<p>You can use macro recording to do this:</p> <ol> <li>Put cursor on the 'd' in <code>def blarg_func:</code></li> <li>Press <code>Esc</code> to go into COMMAND MODE</li> <li>Type <code>qa</code> to start a recording macro and store it in register 'a'</li> <li>Press <code>i</code> to go into INSERT MODE</li> <li>Press <code>#</code> to enter a literal pound symbol</li> <li>Press <code>j</code> or the <code>down arrow</code> to go to the next line</li> <li>Press <code>q</code> to stop recording</li> <li>Press <code>&lt;#&gt;@a</code> to repeat the macro stored in register a. Example <code>10@a</code> to comment out the next 10 lines.</li> </ol> <p>So all together the key presses would be: <code>qai#&lt;ESC&gt;jq10@a</code></p>SiegeXThu, 06 May 2010 19:51:03 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/343Answer by Jasonhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/342<p>map something to it:</p> <p>:map ^K I#^[jI#^[</p> <p>that works for me on AIX, so should work in any vi.</p> <p>To get the esc working, use control-v, so the key sequence is:</p> <p>colon map control-k space shift-i shift-3 control-v esc j shift-i shift-3 control-v esc</p> <p>HTH.</p>JasonThu, 06 May 2010 19:50:00 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/342Answer by srinivasmiriyaluhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/341<p>why dont you use Emacs..its very simple in emacs which you have asked</p>srinivasmiriyaluThu, 06 May 2010 19:34:10 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/341Answer by peterhttp://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/339<p>Try the <a href="http://www.vim.org/scripts/script.php?script_id=23" rel="nofollow" title="Enhanced Commentify">Enhanced Commentify</a> plugin. Alternatively you can add some code to your vimrc for each filetype - see <a href="http://it.toolbox.com/wiki/index.php/Comment_Blocks_of_Text_with_vim" rel="nofollow">here</a>.</p>peterThu, 06 May 2010 19:20:11 -0400http://linuxexchange.org/questions/337/how-do-you-comment-out-several-lines-in-vi-at-once/339