How to Develop the Sublime Text 2 plug-in

Source: Internet
Author: User

Sublime Text 2 is a highly customizable Text editor that has always been attractive to programmers who want to have a fast and powerful modern editing tool. Now, we will create our own Sublime plugin to implement cross-browser CSS Processing Using Nettuts + Prefixr API.

When this is done, you will have an in-depth understanding of how to create a Sublime Prefixr plugin and the ability to write your own editor plug-in.

Ubuntu 12.10 install and crack Sublime Text 2

Install Sublime Text 2 on Ubuntu 13.04

Terms and references
The extension model for Sublime Text 2 is fairly full-featured.

The Extension Model of Sublime Text 2 is quite comprehensive. You can change the syntax highlighting, the actual editor appearance, and all menu items. In addition, you can create a new build environment, auto-completion, language definition, code section, Macro, Key Binding, mouse binding, and plug-in. All these different forms of modification are implemented by the organization's files in the package.

Pacakage is a folder stored in your Packages directory. You can clickPreferences> Browse Packages...Go to your Packages directory. You can also create a zip file and change the extension to. sublime-package to package the pacakage into a separate file. In this tutorial, we will discuss how to package.

Sublime is bound to many different packages. Most of the packages associated with specific languages are language definitions, auto-completion, and build environments. In addition to language-related packages, there are two Default and User packages. Defaultpackage includes all the standard key bindings, menu definitions, File Settings, and a lot of plug-ins written in python.

 

During the process of writing a plugin, the Sublime Text 2 API reference will be essential.

To write a plug-in, Sublime Text 2 API reference is fundamental. In addition, Defaultpackage is also a good reference for how to do our work. Most of the functions of the editor are implemented through the commans command. All operations except the typed characters can be completed through commans. ViewPreferences> Key Bindings-DefaultMenu, you can find many useful built-in functions.

Now, the difference between pacakge and the production check is clear. You can start to write our plug-in.

Step 1-start

Sublime has a function to generate the Python code framework required by a simple plug-in. SelectTools> New Plugin...Menu to open a new file with the following style:

Import sublime, sublime_plugin

Class ExampleCommand (sublime_plugin.TextCommand ):
Def run (self, edit ):
Self. view. insert (edit, 0, "Hello, World! ")

We can see that two Sublime Python modules are introduced so that we can access their APIs and create a new class. Before editing and creating our own plug-in, save the file.

To save this file, we need to create a package to save it. Press ctrl + s (Windows/Linux) or cmd + s (OS X) to save the file. In the "save" dialog box, Userpackage is opened by default. Instead of storing our files, create a new folder named Prefixr.

Packages/
...
-OCaml/
-Perl/
-PHP/
-Prefixr/
-Python/
-R/
-Rails/
...

Now, save our file in the Prefixr folder and name it Prefixr. py. In fact, the file name is not important. You only need to use. py as the extension. For convenience, use the plug-in name.

Now, the plug-in has been saved. We can try to run it. Enter ctrl + 'to open the Sublime console, which is a Python console that can access the API. Enter the following Python code to test our new plug-in:

View. run_command ('example ')

You will see that Hello World is inserted at the beginning of our plug-in file. Next, Undo the new Insert before proceeding.

Step 2-type and name of Comman

For a plug-in, Sublime provides three types of commands.

  • Text commands provides the ability to access the selected file or buffer through a View object
  • Window commands provides a Window object that can reference the current Window.
  • Application commands does not reference any specific window, file or buffer, which is rarely used.

Because we need to use our plug-ins to operate the content in the CSS file or buffer, we need to use the sublime_plugin.TextCommand class as the base class of our customized Prefixr command. In this case, we need to name the class of our command.

In our code framework, you can see the following classes:

class ExampleCommand(sublime_plugin.TextCommand):

When we run the command, the following code is executed in the console:

view.run_command('example')

Sublime will inherit from any sublime_plugin class
(TextCommand, WindowCommand or ApplicationCommand) The Command Suffix of the class name is removed, and the camper name is replaced by an underscore.

In this way, to create a command named prefixr, the class name must be PrefixrCommand.

 

class PrefixrCommand(sublime_plugin.TextCommand):
  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.