This page has been robot translated, sorry for typos if any. Original content here.

Writing a simple plugin for Sublime Text 2

On this topic:

Sublime Text 2 FAQSublime Text 2 FAQ

The example is real, from engineering practice. Translation of the selected number in the text from decimal to hexadecimal number system. The feature should be accessible from the main and context menu as well as by the key combination Ctrl + Shift + H. Execution result: a hex number written using numbers and uppercase letters with no leading characters like “0x”. If the selected text is not a number - swear about it with the status bar. I draw your attention that the example is intended to be simplified to the maximum, so that for the details the essence is not lost: the simplicity of creating plugins for Sublime.

Writing a plugin

Click Tools -> New Plugin ... and see the blank. Change the class name and write the functionality in the run method. I got the following:

 import sublime, sublime_plugin

 class DecToHexCommand (sublime_plugin.TextCommand):
     MAX_STR_LEN = 10
     def run (self, edit):
         v = self.view

         # Get the value of the first selected block
         dec = v.substr (v.sel () [0])

         # Replace decimal number with hexadecimal or output error message		
         if dec.isdigit ():
             v.replace (edit, v.sel () [0], hex (int (dec)) [2:]. upper ())
             # Cutting too long lines that do not fit in the statusbar 
             if len (dec)> self.MAX_STR_LEN:
                 logMsg = dec [0: self.MAX_STR_LEN] + "..."
                 logMsg = dec
             sublime.status_message ("\" "+ logMsg +" \ "isn't a decimal number!")	

We save there where the editor with the name will offer

Add menu items. We register the keyboard combination.

Let's start with the "hot keys".

In the menu, click Preferences -> Key Bindings-User . A file with settings in JSON format opens. Most likely empty. Add a line to it.

  {"keys": ["ctrl + shift + h"], "command": "dec_to_hex"}

We save. Everything. The principle can already be used. If it doesn’t work, it’s worth looking at what is written on the console (Ctrl + `).

In order to add an item to the context menu, create the Context.sublime-menu file with the following content:

         "command": "dec_to_hex"

I think that, as in the previous case, everything is clear without comment.

We save in the same directory in which the plugin is saved.

Those. % USERPROFILE% \ AppData \ Roaming \ Sublime Text 2 \ Packages \ User , for Windows users. In the same place we create the file Main.sublime-men u. I thought that this item would be most appropriate in the Edit menu, so I wrote the following in the Main.sublime-menu file:

         "id": "edit",  
             {"command": "dec_to_hex"}  

We are checking. In the main and context menu items should appear with the name Dec To Hex

References: - Plugin API Reference - Download - Dev Builds. I use the dev version, I have not yet caught the bugs. - An article on the same topic in English