user2760404
user2760404

Reputation: 49

Go to first line that doesn't begin with a certain string in vim

Suppose I have the following buffer:

asdf
asdfotshne
asdfoensh
asdq
asdf
asdfothen
asdfghjkl;
qwertyuiop
zxcvbnm,.

Then I run :v/^asdf/norm 0.

I expect to have the cursor go to Line 4. But it doesn't, it goes to the end of the file.

Why?

Upvotes: 4

Views: 119

Answers (3)

Peter Rincker
Peter Rincker

Reputation: 45107

Just to show that you can do this with :v.

:v/^asdf/throw ""

:v and :g will stop whenever an exception is thrown. You can do this with a throw, non-existent command, or an incomplete/malformed command.

:v/^asdf/^

Upvotes: 4

Birei
Birei

Reputation: 36262

If your cursor is at first line of the file and you want to go to the first one that doesn't starts with asdf, you can use following search expression:

/\v^(asdf)@!

It does a negative look-ahead and stops in first match.

Upvotes: 9

Amadan
Amadan

Reputation: 198324

:v is not used to move the cursor, but rather to perform an operation on all non-matching lines. As such, it scans every line of the file, and executes your norm 0 on each one that does not start with asdf. It thus jumps at the first character of qwertyuiop, and then does the same thing at zxcvmnm,..

It is easier to find the last matching line using gg?, then going one line down.

Upvotes: 6

Related Questions