Robottinosino
Robottinosino

Reputation: 10882

Vim find last occurrence of

In a large buffer, I'd like to scroll down to the last occurrence of pattern pattern.

If I am at the first occurrence, it is easy enough to search for the pattern /, reverse the move to next occurrence n with N and get to the last..

If I am in the middle of a sequence of occurrences.. is there a better way to jump?

Upvotes: 47

Views: 62146

Answers (4)

Wolfack
Wolfack

Reputation: 2769

You can try searching backward via ?

:1?SEARCH_PHRASE

Upvotes: 6

Reman
Reman

Reputation: 8109

Another solution is:

G/\(.\{-}\zs\(foo\)\)\{1,}<CR>

Upvotes: 0

romainl
romainl

Reputation: 196496

A potentially longer solution:

:vim foo % | clast

Upvotes: 5

Scott Olson
Scott Olson

Reputation: 3542

An easy way to find the last occurrence is to jump to the end and search backwards:

G?foo<CR>

In fact, there's an even easier way if you were already searching for something. Just jump to the end and search backwards for the thing you were just searching for:

GN

Simple as that.

Edit: if your search occurred on the very last line, then GN would skip over it to the second last occurrence. Using ggN would solve this problem. (And gg?foo<CR> for similar reasons.)

Upvotes: 95

Related Questions