Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I comment all lines that match a search pattern in VIM

Tags:

vim

vi

I want to comment all lines that match a particular string. I am doing assembly programming, so lines are commented using ";" character.

The string (pattern) may be present anywhere within the line. The comment should be added to the beginning of the line (obviously).

like image 262
Divine Cosmos Avatar asked Oct 22 '13 22:10

Divine Cosmos


People also ask

How do I comment multiple lines in Vim?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

How do you comment multiple lines at once?

To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ )

How do you comment a line range in Vim?

For commenting a block of text is almost the same: First, go to the first line you want to comment, press Ctrl``V , and select until the last line. Second, press Shift``I``#``Esc (then give it a second), and it will insert a # character on all selected lines.

How comment multiple lines in Linux?

Method 1: Using <<comment: In Shell or Bash shell, we can comment on multiple lines using << and name of comment. we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.


3 Answers

Alternatively:

:g/pattern/s/^/;

like image 102
Christian Brabandt Avatar answered Oct 11 '22 13:10

Christian Brabandt


I'd say

:g/pattern/norm I;

(assuming ; is the comment character, and pattern is what you're looking for)

See also: |I| in insert.txt docs

like image 25
sehe Avatar answered Oct 11 '22 15:10

sehe


:s command add a ; to the beginning of each matched line:

:%s/.*pattern/;&
like image 36
Kent Avatar answered Oct 11 '22 15:10

Kent