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

Sublime Text 3: Simple plug-in that changes color theme depending on remote host


Setup: I use Sublime Text 3 (ST), and I often have 2-3 different sessions with Sublime + iTerm2 open in different remote workspaces using RemoteSubl.

Using a simple batch script, I have set my iTerm2 to change colours (by activating a different iTerm user) when I ssh into a different host.

I was wondering if the same could be done for RemoteSubl? Such that when I open something from a specific host/ip/port, then Sublime opens in a different colour scheme, depending on the host/ip/port.


Solution attempt: So far, this is my attempt at building a small plugin that changes colour scheme when host is remote_host.

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):

    def run(self, view):

        try:

            host = view.settings().get('remote_subl.host')

            print(host)

            if host == 'remote_host':

                view.settings().set(
                    'color_scheme',
                    'Packages/Color Scheme - Default/Mariana.tmTheme')

                print(view.settings().get('color_scheme'))

        except:
            print("Not on remote_host")
            pass

Problem: When using using view.settings().get('remote_subl.host') in the console it works fine, and returns remote_host. However, when running the script view.run_command('example') I get the "Not on remote_host" print, indicating that the try loop fails for some reason.


After Keiths suggestions:

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):

    def run(self, view):
        view = self.view
        host = view.settings().get('remote_subl.host', None)

        print(host)

        if host:

            view.settings().set(
                'color_scheme',
                'Packages/Color Scheme - Default/Mariana.tmTheme')

            print(view.settings().get('color_scheme'))

        if host is None:

            view.settings().set(
                'color_scheme',
                'Packages/Color Scheme - Default/Monokai.tmTheme')

            print(view.settings().get('color_scheme'))

Solution

  • view isn't an argument that is passed to the TextCommand's run method. Instead, it is a property on self. Changing it to the following should work:

    import sublime
    import sublime_plugin
    
    
    class ExampleCommand(sublime_plugin.TextCommand):
    
        def run(self, edit):
            view = self.view
    
            try:
    
                host = view.settings().get('remote_subl.host')
    
                print(host)
    
                if host == 'dsintmain':
    
                    view.settings().set(
                        'color_scheme',
                        'Packages/Color Scheme - Default/Mariana.tmTheme')
    
                    print(view.settings().get('color_scheme'))
    
            except:
                print("Not on remote_host")
                pass
    

    I would also recommend printing the exception that occurs to help debug things like this in future. Even better, rather than expecting an exception in normal usage, provide a default value to the get method on the settings (i.e. None) and remove the exception handling altogether.

    host = view.settings().get('remote_subl.host', None)
    

    That way, if something does really go wrong, you'll see the traceback in the ST console.