Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 471
user2760404 Avatar asked Dec 02 '22 18:12

user2760404


2 Answers

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.

like image 191
Birei Avatar answered Dec 18 '22 15:12

Birei


: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.

like image 33
Amadan Avatar answered Dec 18 '22 15:12

Amadan