Write a simple plugin 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 it with the status bar. I draw your attention that the example intends 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. We 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.

Add 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 opens. Most likely empty. We add a line to it.

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

Saving. 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 we 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 it 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 decided 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"} 
  ] 
  } 
 ]

We check. 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