F A Fernández
F A Fernández

Reputation: 134

Selected lines with pattern: Vim Visual Mode

I would like to select (in Vim) the lines that start with the same pattern. For example, if I have this:

if (...) {
    print "Accepted\n"
    o.attr_out = "Accepted";
}
else if (...) {
    print "Partially accepted\n"
    o.attr_out = "Partially Accepted";
}
else if (...) {
    print " NOT Accepted\n"
    o.attr_out = "Not Accepted";
}

Skip to this in a quick way in Vim.

if (...) {
    if (debug == true) print "Accepted\n"
    o.attr_out = "Accepted";
}
else if (...) {
    if (debug == true) print "Partially accepted\n"
    o.attr_out = "Partially Accepted";
}
else if (...) {
    if (debug == true) print " NOT Accepted\n"
    o.attr_out = "Not Accepted";
}

Upvotes: 0

Views: 453

Answers (1)

cguk70
cguk70

Reputation: 626

You can use the vim command:

:%s/print "/if ( debug == true ) &/g

here's a quick breakdown of the command:

% - include all lines in the search

s - substiture the 1st pattern with the 2nd.

/print "/ - the pattern you're searching for.

if ( debug == true ) &/ - the text you want to replace the pattern with (note & will put back the print " text that it found in the search).

g - replace all occurrences on the same line. (Technically you don't need that here - since there's only one occurrence of print " on each line).

Refer to the :help :s command for more information.

Upvotes: 3

Related Questions