
Having recently discovered Sublime Text 2 for myself, I was surprised at how such an effective tool could be devised. Anyone who is spoiled by all sorts of IDEs, usually does not see any advantages, except for a beautiful code highlighting (although this is what initially attracted me).
I will not begin to argue how good this editor is and to paint all its functionality - there are already a lot of topics on this topic. I just want to show how you can make from it the tool for which you donβt feel sorry for the $ 59 that the developers want for it.
Plugins
The main advantage of Sublime Text 2 over the rest of the zoo editors and IDE for me is the ability to write your own plugins. It does not need to study tons of literature, such as if you are going to write a plugin for Eclipse or Geany. Plugins are written in Python and
the project site has an API Reference, which was enough for me to start expanding the editor to fit my needs.
')
I also really liked the fact that the editor has a convenient python console that you can use to debug your plugin.
Wishlist
I will try to formulate some of the opportunities that I really needed and I did not find ready-made solutions.
Creating backups
I develop php and work with many projects at the same time. It just so happened that the version control system did not take root for a variety of reasons, so I was in vital need of a convenient mechanism for creating backups.
Before Sublime, I used Geany and it had a fairly convenient add-in that allows you to make backup copies of the edited files in the selected directory. The only disadvantage of this add-in was that backups were created every time they were saved. Something similar to me and I wanted to Sublime, but that backups were created on request (for example, when pressing hot keys). In fairness, I note that among the ready-made solutions there is a similar plugin AutomaticBackups, but we will write our bike. For fun.
File comparison
When you work on a project not one, it is necessary to look at the changes made by another programmer (or compare an ftp file with a local copy). Usually in this case I opened the
meld , selected the necessary files there and looked at the difference. I also wanted this function to be placed on the editor. I imagined it this way: I open two files in the editor, arrange the tabs one by one, press the hot key and open the meld with the selected file and with the file whose tab in the editor goes next. It seemed to me very convenient.
List of all open files
On the specifics of the work, I often need to make a list of edited files (yes, here it is life without svn). I would like to receive a list of open files, too, by pressing the hotkey.
Let's get to work
I described that small minimum of features that I needed for a comfortable transition to Sublime.
Further I will show how the plugins for my needs were written and how simple it was. I note that I wrote everything under Ubuntu and how it is done in Windows, I can not even imagine.
Backup plugin
Let's get started Open the editor, choose Tools-> New Plugin in the menu ... A tab with a blank of the form opens:
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
. , , . Command. , _
~/.config/sublime-text-2/Packages/User/ backup.py:
import sublime, sublime_plugin
class BackupCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
:
β . sublime. sublime active_window β , . views β . , view, :
sublime.active_window().active_view()
view file_name(), . - . Preferences->Key Bindings β User backup F7:
[
{ "keys": ["f7"], "command": "backup" }
]
:
import sublime, sublime_plugin
class BuckupCommand(sublime_plugin.TextCommand):
def run(self, edit):
print sublime.active_window().active_view().file_name()
, Python (ctrl+`) F7. .
API β .
sublime.status_message('')
API :
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import os, time, shutil, sys
# UnicodeDecodeError
sys.setdefaultencoding('utf-8')
# ,
backup_dir = "/home/deadlink/backup/"
class BackupCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.create(sublime.active_window().active_view().file_name())
def create(self, filename):
#
now = time.localtime()
# ___
prefix = str(now.tm_hour)+"_"+str(now.tm_min)+"_"+str(now.tm_sec)+"_"
# __
stamp = str(now.tm_year)+"_"+str(now.tm_mon)+"_"+str(now .tm_mday)
# / / / /
full_path = backup_dir+stamp+os.path.dirname(filename)
# ,
if not os.path.isdir(full_path):
os.makedirs(full_path) # ,
#
shutil.copyfile(filename, full_path+"/"+prefix+os.path.basename(filename))
#
sublime.status_message(" " + filename + " !")
, ~/backup/, , SideBar ( View->SideBar->show). File->OpenFolder backup. . F7 . :

Diff
Diff, , Backup. :
view, . , :
sublime.active_window().views()
, . view id. , view . :
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import subprocess
class DiffCommand(sublime_plugin.TextCommand):
def run(self, edit):
cur = num = 0
# view
views = sublime.active_window().views()
# view , id
for view in views:
if view.id() == sublime.active_window().active_view().id():
# id , view
cur = num
num += 1
# view
if num-1 != cur:
# meld
subprocess.Popen([
'meld',
views[cur].file_name(),
views[cur+1].file_name()
])
meld. Ubuntu, :
sudo apt-get install meld
diff - . Preferences->Key Bindings β User F2:
[
{ "keys": ["f7"], "command": "backup" },
{ "keys": ["f2"], "command": "diff" }
]
, F2. meld, . !
. , , :
import sublime, sublime_plugin
class OpenedFilesCommand(sublime_plugin.TextCommand):
def run(self, edit):
# view
views = sublime.active_window().views()
files = ""
# view files
for view in views:
files += view.file_name()+"\n"
#
sublime.active_window().new_file()
#
sublime.active_window().active_view().insert(edit, 0, files)
, ctrl+shift+f:
[
{ "keys": ["f7"], "command": "backup" },
{ "keys": ["f2"], "command": "diff" },
{ "keys": ["ctrl+shift+f"], "command": "opened_files" }
]
, .
, Sublime Text 2. - , .
Python, , . , - .