⬆️ ⬇️

Writing a simple plugin for Sublime Text 2

Introduction


Strange, but after searching for references to the text editor Sublime Text 2, I almost did not find anything. I hasten to rectify the situation and tell habrayuzerom about this wonderful tool. First, I will very briefly talk about what he is so good about, then I will write a simple but useful plugin.



Why Sublime Text 2


I don’t presume that Sublime Text 2 is the best text editor, but I liked it very much, and here is why:



Formulation of the problem


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 the details do not lose the essence: 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 #      dec = v.substr(v.sel()[0]) #          if dec.isdigit(): v.replace(edit, v.sel()[0], hex(int(dec))[2:].upper()) else: #    ,      if len(dec) > self.MAX_STR_LEN: logMsg = dec[0:self.MAX_STR_LEN]+ "..." else: logMsg = dec sublime.status_message("\"" + logMsg + "\" isn't a decimal number!") 


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



Add menu items. We prescribe 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 Main.sublime-menu file. 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", "children": [ { "command": "dec_to_hex" } ] } ] 


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



That's all. And about how to pack our plugin in a package and share it with friends, as well as other interesting features of Sublime I will tell you next time if the topic is interesting to someone.



References:






UPD: wrote about Dev Builds and license costs. Thanks to Sky4eg , VCoder and vtx

Source: https://habr.com/ru/post/136529/



All Articles