{"id":602,"date":"2012-03-25T02:03:30","date_gmt":"2012-03-25T08:03:30","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=602"},"modified":"2019-10-30T05:50:08","modified_gmt":"2019-10-30T11:50:08","slug":"commenting-multiple-lines-in-a-shell-script-using-vivim-in-a-single-step","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/commenting-multiple-lines-in-a-shell-script-using-vivim-in-a-single-step\/","title":{"rendered":"Commenting Multiple Lines in a Shell Script Using VI\/Vim in a Single Step"},"content":{"rendered":"<p>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.<\/p>\n<p>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<br \/>\n:5,10s\/^\/#\/ # : in the command is part of entering the editing\/ex mode in vi.<\/p>\n<p>Note# The command : set nu can be used to view line numbers in vi\/vim<br \/>\nClarification<br \/>\nGeneral Syntax<br \/>\n==============================================<br \/>\n:%s\/r1\/r2\/ is the syntax used for string substitution in vi\/vim<br \/>\nSyntax split<br \/>\n: Enter command mode<br \/>\n% Perform the command on all lines<br \/>\ns Short form for :substitute command<br \/>\nr1 Can be replaced with any string in the script\/file that is to be replaced<br \/>\nr2 Can be replaced with any string which should replace the string r1 in the script\/file<br \/>\n\/ Used a separator<br \/>\nChanges from syntax in executed command<br \/>\n============================================<\/p>\n<p>In the command that we used for substitution\/adding comments to lines starting from 5 to 10<br \/>\n% has been replaced with 5,10 so that the command will be executed only for lines starting from 5 to 10<br \/>\n^ 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])<br \/>\n# Replacement for r2 in syntax (i.e. # is used as the character\/string which substitutes r1[position before the first character])<\/p>\n<h4>Comment removal<\/h4>\n<p>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<br \/>\n:5,10s\/^#\/\/<br \/>\nClarification<br \/>\n^# 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)<br \/>\n\/\/ Two \/s without any in between space :: hence substitution will completely remove # from the beginning without adding anything<br \/>\nCommenting all lines in a script\/file<br \/>\nOpen the file in vi\/vim and use the command<br \/>\n:%s\/^\/#\/g<\/p>\n<p>g stands for global (so that the substitution will occur globally, i.e. for all lines in the script)<br \/>\nAll the rest have already been explained in General Syntax<br \/>\nNote: 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<\/p>\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n<p style=\"text-align: center;\"><!--HubSpot Call-to-Action Code --><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><!-- [if lte IE 8]><\/p>\n\n\n\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><a href=\"https:\/\/www.supportpro.com\/freecheckup.php\"><img decoding=\"async\" id=\"hs-cta-img-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b.png\" alt=\"Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value\" \/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '9d590242-d641-4383-94b4-8cfd62f0af6b', {}); <\/script><\/span><!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-602","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":11,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":4455,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions\/4455"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}