Search code examples
pythonpython-3.xsublimetext3sublimetextsublime-text-plugin

How to get content of each active tab in column of Sublime Text 3?


I'm writing a plugin for Sublime, able to get content of active view via self.view. But if I have two opened files in different columns, how to get content (or at least window.id) of active tab in each window via SublimeText3 API? Shall it be done via views() method of sublime.Window class?

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print (self.view.id())

-> it works

class TestCommand(sublime_plugin.WindowCommand):
    def run(self, edit):
        print (self.window.views())

-> it doens't works and there is no errors in console when I ran view.run_command('test')

Any suggestion is much appreciated.


Solution

  • If you look at the documentation you linked, self.window.views() returns a list of views. Views are objects, and cannot be printed. Try this instead:

    class TestCommand(sublime_plugin.WindowCommand):
        def run(self):
            print([view.id() for view in self.window.views()])
    

    This will print the unique id of each view in the window. You can substitute any method of sublime.View if you want.

    To run WindowCommands from Sublime's console, use

    window.run_command("command_name")
    

    whereas a TextCommand is run by accessing the view object:

    view.run_command("command_name")