Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim find last occurrence of

Tags:

vim

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?

like image 336
Robottinosino Avatar asked Mar 29 '13 16:03

Robottinosino


People also ask

How do I search last of file?

File Explorer has a convenient way to search recently modified files built right into the “Search” tab on the Ribbon. Switch to the “Search” tab, click the “Date Modified” button, and then select a range. If you don't see the “Search” tab, click once in the search box and it should appear.

How do you find the last occurrence of a character in a string in Linux?

The command is relatively simple. We start by finding the string we require using grep. Next, Grep will list all the string occurrences, and finally, we pipe the output to the tail and locate the last line of the output.

How do I get to next occurrence in vi?

vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return. Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.


2 Answers

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

like image 95
Scott Olson Avatar answered Nov 07 '22 16:11

Scott Olson


A potentially longer solution:

:vim foo % | clast 
like image 32
romainl Avatar answered Nov 07 '22 16:11

romainl