Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select to the next bookmark in sublime text 3

Is there a way to select the text between the current cursor position and the next/previous bookmark in SublimeText3?

Combinations with the shift key don't work: shiftF2 goes to the previous bookmark (that's shift + F2 = "go to the next bookmark"). Holding shift while selecting the "next bookmark" menu item doesn't work either.

like image 584
drevicko Avatar asked Aug 14 '14 12:08

drevicko


2 Answers

In order to do that, you'll probablly need a plugin. I have just made this simple plugin that selects from the current cursor position to the next/previous bookmark depending on the value of the forward argument.

This is the plugin:

import sublime, sublime_plugin

class SelectToBookmarkCommand(sublime_plugin.TextCommand):
    def run(self, edit, **args):
        """Get initial position"""
        initialPoint = self.view.sel()[0].begin()

        """Clear selected things (if any)"""
        self.view.sel().clear()

        """Move to next bookmark or previous bookmark"""
        forward = args.get('forward','true')
        if forward is True:
            self.view.run_command("next_bookmark")
        else:
            self.view.run_command("prev_bookmark")


        """Get current position (position of the bookmark)"""
        finalPoint = self.view.sel()[0].begin()

        """Clear selected things (if any)"""
        self.view.sel().clear()

        """Region to select"""
        regionToSelect = sublime.Region(initialPoint, finalPoint)

        """Add the region to the selection"""
        self.view.sel().add(regionToSelect)

Use Tools > New Plugin and use the provided plugin. Save it as SelectToBookmark.py . Finally, add the keyBindings to your user file using something like this:

{
    "keys": ["ctrl+alt+e"],
    "command": "select_to_bookmark",
    "args": {"forward": true}
}

Use another keyBinding with forward argument set to false to select from the current position to the previous bookmark.

Edit: As user @MattDMo has commented: "make sure you save the .py file in Packages/User - you can locate the directory on your system (if it doesn't come up automatically) by selecting the Preferences -> Browse Packages... menu option"

like image 141
sergioFC Avatar answered Oct 13 '22 02:10

sergioFC


Similar to @sergioFC's answer. This version is used for the SublimeBookmark package.

import sublime, sublime_plugin

class SelectToBookmarkCommand(sublime_plugin.TextCommand):
    def run(self, edit, **args):
        """Get initial position"""
        initialPoint = self.view.sel()[0].begin()

        """Clear selected things (if any)"""
        self.view.sel().clear()

        """Move to next bookmark or previous bookmark"""
        forward = args.get('forward','true')
        if forward is True:
            self.view.window().run_command("sublime_bookmark",{ "type" : "goto_previous" })
        else:
            self.view.window().run_command("sublime_bookmark",{ "type" : "goto_next" })


        """Get current position (position of the bookmark)"""
        finalPoint = self.view.sel()[0].begin()

        """Clear selected things (if any)"""
        self.view.sel().clear()

        """Region to select"""
        regionToSelect = sublime.Region(initialPoint, finalPoint)

        """Add the region to the selection"""
        self.view.sel().add(regionToSelect)
like image 31
Jon Avatar answered Oct 13 '22 02:10

Jon