Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the find and replace panel with a pre-populated search string?

I know that it is possible to show the Sublime Text "Find and Replace" panel using the show_panel command (either via a keybinding or a plugin), and control which arguments are enabled/disabled.

Example run from Sublime Console panel:

window.run_command('show_panel', { 'panel': 'replace', 'regex': True, 'case_sensitive': False, 'whole_word': False, 'in_selection': False, 'wrap': True, 'highlight': True, 'preserve_case': True })

What I would like to know, is: is there a way to pre-populate the Find What: and Replace With: values?

I found this forum post but it has no reply, and the unofficial documentation is of no help in this case.

I have tried:

  • 'find_what': 'string'
  • 'replace_with': 'string'
  • 'find_history': 'string'
  • 'replace_history': 'string'
  • 'find_history': ['string']
  • 'replace_history': ['string']
  • 'find': 'string'
  • 'replace': 'string'

EDIT: I have also tried:

  • characters
  • find_characters
  • look_for
  • search_for
  • find_regex
  • find_string
  • search_string
  • replacement
  • search_characters

and none of the above makes any difference - the panel is always pre-populated with the previous search and replace values, not what I pass in.


I am aware of the commands slurp_find_string and slurp_replace_string, which will take the current selection and update the Find What / Replace With values respectively, but I would like a way to do it without having to mess around with selections first - I just want to pass the values as arguments directly to the show_panel command.

Does anyone know what parameters/arguments can be used to control this?

like image 253
Keith Hall Avatar asked Jul 22 '16 06:07

Keith Hall


1 Answers

You can run an insert command on the window just after you've run the show_panel command.

import sublime_plugin


class ShowPanelPrefilledCommand(sublime_plugin.WindowCommand):
    def run(self, interactive=True):
        self.window.run_command('show_panel', {
            'panel': 'find_in_files',
            'where': '<open folders>',
            'whole_word': False,
            'case_sensitive': False,
            'preserve_case': False,
            'regex': False,
            'use_buffer': False,
            'show_context': False,
        })

        self.window.run_command('insert', {'characters': 'hello'})

        if not interactive:
            self.window.run_command('find_all', {'close_panel': True})
like image 79
Gerard Roche Avatar answered Oct 01 '22 20:10

Gerard Roche