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:
- Nice looking dark interface, visual effects and Distraction Free Mode
- Panel with editable text in thumbnail. An interesting and really convenient find!
- Multiple selection and editing
- All features inherent to most advanced editors: syntax highlighting, code formatting, autocompletion, etc.
- That which is not "in the box" can be downloaded from the repository! Yes, after making the simplest manipulations in Sublime, a complete package management system appears, almost like in Ubunt or Debian.
- If this is not enough - right in the main menu there is a “New Plugin” item. Click on it and write a plugin that implements the functionality we need in Python. This will be discussed.
- This miracle costs $ 59 per one, or $ 500 for 10 licenses. However, if you do not want, then you can not pay. There are no restrictions in this case, just a reminder will occasionally pop up.
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
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