I have an error report where often the known errors are in the top few thousand lines, formatting issue rather than quantity unfortunately.

I want to run :g/pattern/ from the end of the file.

I know :g/pattern/m0 reverses the file which is mainly the same thing but if I don't put it back to how it was the line references are wrong when I query the error.

I tried $,1g/pattern/ but got Backwards range given, Ok to swap (y/n)?.

Selecting n aborts the command.

Is this achievable?

  • Do you need to act on the lines? Or just search?
    – D. Ben Knoble
    Aug 15, 2019 at 3:48
  • @Steve, I think you could be more specific about why you need to process the file from the bottom up. Right now, it's hard to tell what exactly is your motivation...
    – filbranden
    Aug 15, 2019 at 4:53
  • Hi @filbranden I have a very large file with multiple **error lines per physical error. When there is a defect impacting the report I often miss the trees for the forest. Reversing the search is just a way of filtering out most of the errors at the top of the file and ensuring I don't miss some. :g/pattern/m0 works but I was hoping to use a reverse range
    – Steve
    Aug 15, 2019 at 4:59
  • 1
    @Steve Are you just searching? If so, why not ?pattern, which will search backwards? Why are you using :g? To search for those lines? Using / and ? are the search operators, and the latter searches backwards... Are you using the pattern to look for the lines with the error? Perhaps some form of folding might help you, folding all lines that are not the ones you care about... But it's still hard to figure out exactly what you want. Can you give more specific examples? Perhaps a snippet of a file and the operation you're trying to accomplish there? Please edit the question.
    – filbranden
    Aug 15, 2019 at 5:12
  • 1
    /doh I'd been using :g to get a list of the error and just forgot ? did what I want
    – Steve
    Aug 16, 2019 at 0:09

2 Answers 2


The simplest idea is to filter


And then undo when you want the whole file back.

  • Aside: known errors is a huge red flag.
    – D. Ben Knoble
    Aug 17, 2019 at 13:12
  • More there is a config issue causing false positives. When the config is finished they should/will go away
    – Steve
    Aug 19, 2019 at 23:43

Use ? to search backwards.

If 'wrapscan' is set (which is the default), you can use ? at the start of the file, and it will wrap to the end and continue the search from there, effectively finding the last occurrence of the pattern in the file.

From there, you can use the n command, repeatedly, to jump to the preceding occurrences.

This should allow you to easily find the lines containing a specific pattern at the end of a log file such as the one you describe.

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.