Go to first line that doesn't start with a specific line in vim

Suppose I have the following buffer:

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

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

I expect the cursor to go to line 4. But this is not the case, it goes to the end of the file.

Why?

+4
source share
3 answers

If your cursor is on the first line of the file and you want to jump to the first that does not start with asdf , you can use the following search expression:

 /\v^(asdf)@! 

He makes a negative forecast ahead and stops in the first match.

+8
source

:v not used to move the cursor, but to perform an operation on all inconsistent lines. Thus, it scans every line of the file and executes your norm 0 for each of them that does not start with asdf. So he jumps with the first qwertyuiop and then does the same in zxcvmnm,. .

Easier to find the last matching line using gg? and then go down one line.

+6
source

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 the throw command, a nonexistent command, or an incomplete / incorrect command.

 :v/^asdf/^ 
+4
source

Source: https://habr.com/ru/post/1501271/


All Articles