Sublime Forum

Find the previous occurence of a regex pattern

#1

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
blonde
* 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().clear() 
    self.view.sel().add((self.view.find_prev('^\*', sel.end())))
1 Like

How to do find_under_expand select backwards?
#2


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

0 Likes

API Suggestions
#3

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:

0 Likes

#4

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.

0 Likes