tags:

views:

62

answers:

2

how do i preserve sentences that start with X criteria and delete sentences that match Y criteria. using vim, I would like to delete all sentences that do not start with void.

void set_first_name(string in_first_name);
string first_name();
void set_last_name(string in_last_name);
string last_name();
void set_composer_yob(int in_composer_yob);
int composer_yob();
void set_composer_genre(string in_composer_genre);
string composer_genre();
void set_ranking(int in_ranking);
int ranking();
void set_fact(string in_fact);

A: 

Usually, a regex for this would look like this:

^(?!\s*void).*\n

I.e., match the start of the line (^), then assert that you can't match void here, optionally preceded by whitespace, then match all characters until the following linebreak.

It appears (thanks @Al) that vim supports lookahead via a different syntax (\@! for negative, \@= for positive lookahead), so this pattern could be transformed into

^(\@!\s*void).*\n
Tim Pietzcker
If lookaheads are not supported one could use something like `^([^v]|v[^o]|vo[^i]|voi[^d]).*` instead.
Jens
Vim does support lookahead: positive is `\@=` and negative is `\@!`. So your pattern would be `^\(\s*void\)\@!.*\n` (Vim will deal with the carriage return automatically).
Al
+3  A: 

The vim commands :g and :v help here.

:g/regex/action

'g' globally selects all lines matching regex and applies the action to them. 'v' inverts the regex (like grep -v), so applies the action to all non-matching lines.

So to delete all lines not starting with void:

:v/^void/d

where 'd' is the action to delete the line.

Derek Baum