Sublime Forum

It is possible to copy the command palette input text without using window.run_command("copy")?


On the recent development build 3156 of Sublime Text, there was this fix:

Reinvoking the Command Palette while it is open, but without focus, now works properly

However, that bug was the only way I have to close the command palette without erasing my initial text: Core$1814. On build 3155, If I called the command palette command again, when it was without focus, then it does not deleted my inputted text.

Now, I wrote a plugin ( which runs as series of commands like:

self.window.run_command( "show_overlay", {"overlay": "command_palette"} )
self.window.run_command( "select_all" )
self.window.run_command( "copy" )
current_clipboard_text = sublime.get_clipboard()
sublime.set_clipboard( clipboard_text )
self.window.run_command( "paste" )
self.window.run_command( "select_all" )

Then it was able to fix the issue Core$1814, however now I loose the clipboard text I had before opening the command palette, as it is replaced by the text I copied from the command palette input widget.

I am thinking about saving and restoring it before running these commands, however is there another way I can copy the contents of the Command Palette other than:

self.window.run_command( "select_all" )
self.window.run_command( "copy" )

Beyond `sublime.project_file_name()` can I get something like `sublime.workspace_file_name()`?

After search on the forum I found this post by jps in 2011:

Then I did what he said and my TextCommand worked on the widget. This 19b2534 is the commit where I removed my older code using the clipboard command, and added the new command using view.sel():

Here it is how I open the command palette now using the WindowCommand:

class FixedCommandPaletteLastInputHistoryCommand(sublime_plugin.WindowCommand):

    def run(self):
        global is_command_palette_open
        global is_command_palette_just_closed

        if is_command_palette_open:
            self.window.run_command( "fixed_hide_overlay_which_is_correctly_logged" )

            self.window.run_command( "show_overlay", {"overlay": "command_palette"} )
            self.window.run_command( "select_all" )

            self.window.run_command( "fixed_command_palette_last_input_history_helper")
            self.window.run_command( "select_all" )

            is_command_palette_open = True

And this is the recent/just created fixed_command_palette_last_input_history_helper which extends the TextCommand class:

class FixedCommandPaletteLastInputHistoryHelperCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        selections = self.view.sel()

        global widget_text
        current_widget_text = self.view.substr( selections[0] )

        if len( current_widget_text ) \
                and current_widget_text != "\n":

            widget_text = current_widget_text
            self.view.erase( edit, selections[0] )

        self.view.run_command( "append", {"characters": widget_text} )

In order for everything work and it remember the command palette input after closing it with the escape key, still there are other commands and keybinds required, you can find remaining code at:




Now, I have a dedicated plugin to remember the command palette input: