Sublime Text Plugin Quickstart

Based on the fact that the Sublime Text plugin example page is outdated below is a quick guide to create a Sublime Text Plugin for Sublime Text 2

  1. Go to Tools > New Plugin to auto generate a template for a plug-in
    import sublime, sublime_plugin
    
    class ExampleCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            self.view.insert(edit, 0, "Hello, World!")
  2. Save it as a .py file in Packages/User folder (it should be the default folder when you select save). I chose testPlugin.py
  3. You can run in from the Sublime Text console (Ctrl + ~ in Windows) using the following command. (if it doesn’t work try, try saving the file again with the console open to see any errors and also try closing and reopening Sublime Text)
    view.run_command("example")

Running Camel Case Commands

Use underscores

class ExampleTextCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")
 view.run_command("example_text")

sublime.Edit & sublime_plugin.EventListener

If an edit object is needed (seems to be needed for writing things on the page), use view.begin_edit() but don’t forget to end_edit()

They can be created by view.begin_edit(). Every call to view.begin_edit() must have a corresponding call to view.end_edit(), typically wrapped in a try … finally block.

Sublime Text 2 API reference

The EventListener class let’s you trigger things on an event happening and the code below shows inserting text to a document when it is opened. (Disclaimer: I’m not fluent in Python so I’m not sure if the below is fully correct but it does seem to work)

import sublime, sublime_plugin
class OnOpenCommand( sublime_plugin.EventListener):
   def on_load(self, view):
       try :
           edit = view.begin_edit()
           view.insert(edit, 0, "on load")
       finally :
           view.end_edit(edit)
Advertisements

Sublime Text: “ImportError: No module named sublimeplugin” i.e. bad plugin documentation

TLDR: Sublime Text’s plugin Examples page is out of date as of 30th May 2014. Look at “How to Create a Sublime Text 2 Plugin” for proper guidance.

For the popularity that Sublime Text gets, it seems that their plugin documentation has been left by the wayside

Their Plugin Examples page is out of date showing an example for SublimeText 1. I found that out when creating the plugin only to see the following error message in the Sublime Text console (Ctrl+~ in Windows)

ImportError: No module named sublimeplugin

Searching for this error I landed on this forum post which the link to “How to Create a Sublime Text 2 Plugin” from Tuts+ which seems to work.

There seems to be a change from camelCase to snake_case for functions thus all the instructions on the plugin page are incorrect.

Links API References: choose appropriate link for Sublime Text version

Windows XP DPI Registry Fix

The other day I ran into a laptop where the desktop looks like the following screenshot.

Tiny Text and System Icons

Click on the picture to view the full resolution for the image. The text is too small and as you can see there are 3 rows of icons in the system tray! The system tray is the default size and it can fit 3 rows of icons when it can normally only have 1. Tried clicking on “Advanced” button in the “Display Properties” Tab (shown below) and a DLL crashed.

Display Properties Dialog Box

Tried Changing things in the “Advanced Appearance” dialog and managed to make things more usable but it did not solve the problem.

Advanced Appearance Dialog Box

Then it dawned on me that it may be the DPI settings, which can only be accessed when clicking the “Advanced” button to get the graphics card advanced dialog box. But since it crashed I had to get an alternative fix

General Tab of Graphics Card Advanced Dialog Box

After some Googling I found this and modified the registry as follows:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI]
"LogPixels"=dword:00000060

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts]
"Courier 10,12,15 (VGA res)"="COURE.FON"
"MS Serif 8,10,12,14,18,24 (VGA res)"="SERIFE.FON"
"MS Sans Serif 8,10,12,14,18,24 (VGA res)"="SSERIFE.FON"
"Small Fonts (VGA res)"="SMALLE.FON"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\GRE_Initialize]
"FIXEDFON.FON"="vgafix.fon"
"FONTS.FON"="vgasys.fon"
"OEMFONT.FON"="vgaoem.fon"

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware Profiles001\Software\Fonts]
"FIXEDFON.FON"="vgafix.fon"
"FONTS.FON"="vgasys.fon"
"OEMFONT.FON"="vgaoem.fon"
"LogPixels"=dword:00000060

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware Profiles\Current\Software\Fonts]
"FIXEDFON.FON"="vgafix.fon"
"FONTS.FON"="vgasys.fon"
"OEMFONT.FON"="vgaoem.fon"
"LogPixels"=dword:00000060

Rebooted and viola! Back to business as per normal