Sublime Forum

Can I create a hotkey that jumps to predefined word?

#1

In my vue files I would love if I could create a hotkey that jumped to “” That way I could quickly move up and down between my template section and my script section. I know I can do a search and then type in but I was hoping to just have the hotkey immediately focus me on the .
Thanks!

0 Likes

#2

I’m not aware of any such command that’s built in, since you don’t want to do a search first. However it’s possible with a plugin to do something like that. Here’s an example of one (see this video if you’re not sure how to install a plugin manually).

import sublime
import sublime_plugin


class JumpToTextCommand(sublime_plugin.TextCommand):
    """
    Jump the cursor to the next occurrence of the provided text, if any.
    """
    def run(self, edit, text):
        pt = self.view.sel()[0].b
        region = self.view.find(text, pt, sublime.LITERAL | sublime.IGNORECASE)

        if region is None:
            region = self.view.find(text, 0, sublime.LITERAL | sublime.IGNORECASE)

        if region:
            self.view.sel().clear()
            self.view.sel().add(region)
            self.view.show_at_center(region)
        else:
            self.view.window().status_message('No matching text found')

Once you put it in place, you just need to define yourself a key binding that will invoke the command, and give it the text that you’d like to jump to. I’m guessing for you that’s a tag, since it doesn’t show up in your post. Just replace the text view in this sample with whatever text you’d like to jump to.

    { "keys": ["ctrl+alt+t"], "command": "jump_to_text",
       "args": {
            "text": "view"
        }
    },

The command above is not case sensitive; if you want it to find and navigate to text in a case sensitive manner, alter sublime.LITERAL | sublime.IGNORECASE in both places to sublime.LITERAL instead.

0 Likes

#3

Hi @OdatNurd
Thank you so much! Your youtube video was super helpful and the plugin you provided worked great. You have saved me a ton of time going forward :slight_smile:

1 Like