Friday, February 10, 2012

Sublime Text default to empty file

I recently began using the editor Sublime Text. Previously I was using Notepad++ which is also a fine editor, but not as "futuristic" as Sublime Text. I had gotten used to Notepad++'s behaviour of defaulting to an empty file when all tabs are closed, and found that this behaviour was easy to achieve in Sublime Text as well, thanks to its amazing API. To get the behaviour, drop the following Python source into something like Data/Packages/User/default_to_empty_file.py.


import sublime, sublime_plugin

class DefaultToEmptyFile(sublime_plugin.EventListener):
def on_close(self, view):
for window in sublime.windows():
if window == None:
continue

if len(window.views()) <= 1:
window.new_file()

def on_load(self, view):
window = view.window()
if window == None:
return

views = window.views()
if len(views) == 2:
for v in views:
if v != view and v.file_name() == None and v.size() == 0:
window.focus_view(v)
window.run_command('close_file')