Sublime Forum

Find the previous occurence of a regex pattern


If I had a list like this,

* Adam
likes coffee
* Beth
blon[the cursor is here]de
* Carl

I’d like to search the previous occurrence of the regex pattern “^*” and move the cursor before it like this

* Adam
likes coffee
[the cursor is now here]* Beth
* Carl

I wrote the snippet below but the method find_prev() doesn’t seem to exist.
How can I find the previous occurrence of a regex pattern?

import sublime, sublime_plugin
class JumpToLastAsteriskCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    sels = self.view.sel()
    self.view.sel().add((self.view.find_prev('^\*', sel.end())))
1 Like

How to do find_under_expand select backwards?

currently the workaround can be very inefficient: find all occurrences and then filter that list to find the closest match region before the caret position


API Suggestions

I’m new to Sublime Text plugin development, but it’s harder than I thought to even just create simple commands compared to doing the same for Emacs. Is Sublime Text famous for having a faulty API?

Sublime Text is really popular among developers theses days, and I thought they would have better and more complete API. So not being able to even just search backwards confuses me a lot! It’s really easy to do this in Emacs! :confused:



not at all, but there are a few places where people have asked for improvements or new APIs:

like I said, it’s possible, just not as simple as it should be. Some people even use the Python re module to do it, rather than relying on ST’s find API.