0

When I open a log file in vi or view and I need to search for a word, the first thing I do is to press G to get to the bottom. Then I'll start a search using a / followed by whatever word I'm looking for. This usually works for me.

Today someone mentioned I should use ? to start my search since I'm starting at the bottom of the file and this will cause it to search from the bottom up.

My question is should I have been using the ? all along, since these are log files and I will usually want to find the most recent occurrence of the "word"?

Also where does the search start when using the /, from the top or the current line the cursor is on?

3
  • 1
    If the cursor is at the beginning of the file don't need to even use G (if you have wrapping enabled). Just hit ?. Otherwise, yeah, G? works.
    – B Layer
    Commented Dec 16, 2020 at 21:00
  • 1
    @filbranden that was the info I was looking for, thanks.
    – datacon
    Commented Dec 17, 2020 at 2:54
  • Welcome to Vi and Vim! You might like How to navigate :help
    – D. Ben Knoble
    Commented Dec 17, 2020 at 22:10

1 Answer 1

0

Yes, / will search forward and ? will search backward.

Both will (by default) wrap around when they get to the bottom/top of the file.

To find the most recent occurrence of the search pattern in a log file, you typically want to use ?, to search backward, wrapping at the top and starting from the bottom of the file.

Unless you have a match on the very last line, using G is not necessary. In fact, it might make you miss a match, having the backward search start at the beginning of the last line of the file, rather than also search for the match on that last line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.