Sublime Forum

Add Bookmark (not toggle) based upon find results?

#1

I would like to be able to add bookmarks (not toggle) to all lines matching results of a find operation. Currently in SublimeText I have figured out I can do Ctrl+F, type in search term, do “find_all” (Alt+Enter), do “toggle_bookmark” (Ctrl+F2). However there are a few problems with this.

  1. I am looking for a keyboard shortcut command that is “add_bookmark” and not “toggle_bookmark”. The problem is that if I have some lines already bookmarked and they ALSO match the results of my find, then I would not like the bookmark to be removed from these lines. I would like bookmarks to only be added based on the results of the find, not toggled and not ever removed.

  2. I would like to do it all in one keystroke. Thus I propose a new keyboard shortcut command “find_all_addbookmark” which will do “find_all” followed by a “add_bookmark” operation based upon a selected word or entry in the find bar. I think it would be helpful to simple users to even have this as a button on the find bar since it is so handy.

Thanks.

0 Likes

MarkLinesContaining - Bookmark lines from custom panel
#2

Okay figured out a temporary solution via doing multiple commands in a plugin. Though it has the shortcomings that “Find All” changes your cursor position to the first found item. And also it is necessary to clear all previous bookmarks since there is no “Add Bookmark” functionality in Sublime other than “Toggle Bookmark” (which would unbookmark lines that were previously bookmarked and also match the search text).

Preferably, a properly done implementation of find_all_bookmark would not change the cursor position - it would just add bookmarks to the lines which match the search text and leave the original cursor position alone. Then the user can use “Next Bookmark” and “Prev Bookmark” to go through the search results as bookmarks from the original cursor position.

Create a file named find_all_bookmark.py

[code]import sublime, sublime_plugin

class FindAllBookmarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
context = self.view.window()
# context.run_command(“find_all”, {“close_panel”: true}, {“context”: “window”}) # NameError: global name ‘true’ is not defined
context.run_command(“find_all”, {“context”: “window”})
context.run_command(“hide_panel”)
context.run_command(“clear_bookmarks”)
context.run_command(“toggle_bookmark”)
context.run_command(“single_selection”)
[/code]

Add the following to Key Bindings - User

{ "keys": "ctrl+enter"], "command": "find_all_bookmark", "context": {"key": "panel", "operand": "find"}, {"key": "panel_has_focus"}]},
0 Likes

#3

You can add bookmarks manually with:

view.add_regions("bookmarks", [view.sel()[0]], "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)

And remove them (all) with:

view.erase_regions("bookmarks")

Using the view.find_all() API, you can set bookmarks where you want without using the find_all command and moving the cursor.

If I add to do it myself, I’ll probably not use bookmarks but a custom region name and create another command to replace the “Next Bookmark” and “Prev Bookmark” to navigate.

0 Likes

#4

Thanks a million bizoo. Your suggestions were spot on. I elected to re-use and append to the bookmarks region key so I can cumulatively add to the bookmarks based on multiple searches. Also I can clear and toggle the bookmarks with my existing hotkeys. Though it is a fantastic idea that you can set custom bookmark keys and assign separate hotkeys for them as well.

Here is mark_lines_containing.py for anyone else who might enjoy:

{ "keys": "alt+m"], "command": "mark_lines_containing" },

[code]import sublime, sublime_plugin

class MarkLinesContainingCommand(sublime_plugin.WindowCommand):

def run(self):
	self.window.show_input_panel("Mark Lines Containing:", "", self.on_done, None, None)
	pass

def on_done(self, text):
	ExistingBookmarks = self.window.active_view().get_regions("bookmarks")

	RegionsResult = self.window.active_view().find_all(text, sublime.IGNORECASE)

	for ThisExistingBookmark in ExistingBookmarks:
		RegionsResult.append(ThisExistingBookmark)

	self.window.active_view().add_regions("bookmarks", RegionsResult, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)

[/code]

0 Likes