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

Write a simple plug-in for Sublime Text 2

On this topic:


Sublime Text 2 FAQSublime Text 2 FAQ

An example is real, from engineering practice. Translation of the selected number in the text from decimal to hexadecimal. The feature should be accessible from the main and context menu as well as by pressing Ctrl + Shift + H. Result of execution: the number in hex is written using the upper case letters and letters without any leading characters like "0x". If the selected text is not a number - we swear about this with the status bar. I draw your attention that the example is intended to be simplified to the maximum, so that the details are not lost: the simplicity of creating plug-ins for Sublime.

Writing a plugin

Click Tools -> New Plugin ... and see the workpiece. Change the name of the class and write the functional 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 the decimal number with hexadecimal or output an error message		
         if dec.isdigit ():
             v.replace (edit, v.sel () [0], hex (int (dec)) [2:]. upper ())
         else:
             # Crop too long lines that do not fit in the status bar 
             if len (dec)> self.MAX_STR_LEN:
                 logMsg = dec [0: self.MAX_STR_LEN] + "..."
             else:
                 logMsg = dec
             sublime.status_message ("\" "+ logMsg +" \ "is not a decimal number!")	

We save where the editor with the name dec_to_hex.py will offer.

Adding menu items. We prescribe a keyboard combination.

Let's start with the "hot keys".

In the menu, click Preferences -> Key Bindings-User . The file with the settings in JSON format is opened. Most likely empty. We add a line to it.

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

We save. All. Principle can already be used. If it does not work, you should see what's written about this in the console (Ctrl + `).

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

 [  
     {  
         "command": "dec_to_hex"
     }  
 ] 

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

We save to the same directory in which the plugin was 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 figured that this item would be most appropriate in the Edit menu, so in the Main.sublime-menu file I wrote the following:

 [  
     {  
         "id": "edit",  
         "children":  
         [  
             {"command": "dec_to_hex"}  
         ]  
     }    
 ]

Checking. In the main and context menu should appear items with the name Dec To Hex

References:

www.sublimetext.com/docs/api-reference - Plugin API Reference

www.sublimetext.com/download - Download

www.sublimetext.com/dev - Dev Builds. I'm using dev version, I did not catch bugs yet.

net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/ - Article on the same topic in English