Contents

API Reference

Sublime API

Base Classes

Example Plugins

Several pre-made plugins come with Sublime Text 2, you can find them in the Packages/Default directory:

Module sublime

MethodsReturn ValueDescription
set_timeout(callback, delay)NoneCalls the given callback after the given delay (in milliseconds). Callbacks with an equal delay will be run in the order they were added. It is safe to call setTimeout from multiple threads.
status_message(string)NoneSets the message that appears in the status bar.
error_message(string)NoneDisplays an error dialog to the user.
message_dialog(string)NoneDisplays a message dialog to the user.
ok_cancel_dialog(string, <ok_button>)boolDisplays an ok / cancel question dialog to the user. If ok_button is provided, this may be used as the text on the ok button. Returns True if the user presses the ok button.
load_settings(base_name)SettingsLoads the named settings. The name should include a file name and extension, but not a path. The packages will be searched for files matching the base name, and the results will be collated into the settings object. Subsequent calls to load_settings with the name base_name will return the same object, and not load the settings from disk again.
save_settings(base_name)NoneFlushes any in-memory changes to the named settings object to disk.
windows()[Window]Returns a list of all the open windows.
active_window()WindowReturns the most recently used window.
packages_path()StringReturns the base path to the packages.
installed_packages_path()StringReturns the path where all the user's *.sublime-package files are.
get_clipboard()StringReturns the contents of the clipboard.
set_clipboard(string)NoneSets the contents of the clipboard.
score_selector(scope, selector)IntMatches the selector against the given scope, returning a score. A score of 0 means no match, above 0 means a match. Different selectors may be compared against the same scope: a higher score means the selector is a better match for the scope.
run_command(string, <args>)NoneRuns the named ApplicationCommand with the (optional) given arguments.
log_commands(flag)NoneControls command logging. If enabled, all commands run from key bindings and the menu will be logged to the console.
log_input(flag)NoneControls input logging. If enabled, all key presses will be logged to the console.
version()StringReturns the version number
platform()StringReturns the platform, which may be "osx", "linux" or "windows"
arch()StringReturns the CPU architecture, which may be "x32" or "x64"

Class sublime.View

Represents a view into a text buffer. Note that multiple views may refer to the same buffer, but they have their own unique selection and geometry.
MethodsReturn ValueDescription
id()intReturns a number that uniquely identifies this view.
buffer_id()intReturns a number that uniquely identifies the buffer underlying this view.
file_name()StringThe full name file the file associated with the buffer, or None if it doesn't exist on disk.
name()StringThe name assigned to the buffer, if any
set_name(name)NoneAssigns a name to the buffer
is_loading()boolReturns true if the buffer is still loading from disk, and not ready for use.
is_dirty()boolReturns true if there are any unsaved modifications to the buffer.
is_read_only()boolReturns true if the buffer may not be modified.
set_read_only(value)NoneSets the read only property on the buffer.
is_scratch()boolReturns true if the buffer is a scratch buffer. Scratch buffers never report as being dirty.
set_scratch(value)NoneSets the scratch property on the buffer.
settings()SettingsReturns a reference to the views settings object. Any changes to this settings object will be private to this view.
window()WindowReturns a reference to the window containing the view.
run_command(string, <args>)NoneRuns the named TextCommand with the (optional) given arguments.
size()intReturns the number of character in the file.
substr(region)StringReturns the contents of the region as a string.
substr(point)StringReturns the character to the right of the point.
begin_edit(<command>, <args>)EditCreates an edit object, demarcating an undo group. A corresponding call to end_edit() is required.
end_edit(edit)EditFinishes the edit.
insert(edit, point, string)intInserts the given string in the buffer at the specified point. Returns the number of characters inserted: this may be different if tabs are being translated into spaces in the current buffer.
erase(edit, region)NoneErases the contents of the region from the buffer.
replace(edit, region, string)NoneReplaces the contents of the region with the given string.
sel()RegionSetReturns a reference to the selection.
line(point)RegionReturns the line that contains the point.
line(region)RegionReturns a modified copy of region such that it starts at the beginning of a line, and ends at the end of a line. Note that it may span several lines.
full_line(point)RegionAs line(), but the region includes the trailing newline character, if any.
full_line(region)RegionAs line(), but the region includes the trailing newline character, if any.
lines(region)[Region]Returns a list of lines (in sorted order) intersecting the region.
split_by_newlines(region)[Region]Splits the region up such that each region returned exists on exactly one line.
word(point)RegionReturns the word that contains the point.
word(region)RegionReturns a modified copy of region such that it starts at the beginning of a word, and ends at the end of a word. Note that it may span several words.
find(pattern, fromPosition, <flags>)RegionReturns the first Region matching the regex pattern, starting from the given point, or None if it can't be found. The optional flags parameter may be sublime.LITERAL, sublime.IGNORECASE, or the two ORed together.
find_all(pattern, <flags>, <format>, <extractions>)[Region]Returns all (non-overlapping) regions matching the regex pattern. The optional flags parameter may be sublime.LITERAL, sublime.IGNORECASE, or the two ORed together. If a format string is given, then all matches will be formatted with the formatted string and placed into the extractions list.
rowcol(point)(int, int)Calculates the 0 based line and column numbers of the point.
text_point(row, col)intCalculates the character offset of the given, 0 based, row and column. Note that 'col' is interpreted as the number of characters to advance past the beginning of the row.
set_syntax_file(syntax_file)NoneChanges the syntax used by the view. syntax_file should be a name along the lines of Packages/Python/Python.tmLanguage. To retrieve the current syntax, use view.settings().get('syntax').
extract_scope(point)RegionReturns the extent of the syntax name assigned to the character at the given point.
scope_name(point)StringReturns the syntax name assigned to the character at the given point.
score_selector(point, selector)IntMatches the selector against the scope at the given location, returning a score. A score of 0 means no match, above 0 means a match. Different selectors may be compared against the same scope: a higher score means the selector is a better match for the scope.
find_by_selector(selector)[Regions]Finds all regions in the file matching the given selector, returning them as a list.
show(point, <show_surrounds>)NoneScroll the view to show the given point.
show(region, <show_surrounds>)NoneScroll the view to show the given region.
show(region_set, <show_surrounds>)NoneScroll the view to show the given region set.
show_at_center(point)NoneScroll the view to center on the point.
show_at_center(region)NoneScroll the view to center on the region.
visible_region()RegionReturns the currently visible area of the view.
viewport_position()VectorReturns the offset of the viewport in layout coordinates.
set_viewport_position(vector, <animate<)NoneScrolls the viewport to the given layout position.
viewport_extent()vectorReturns the width and height of the viewport.
layout_extent()vectorReturns the width and height of the layout.
text_to_layout(point)vectorConverts a text position to a layout position
layout_to_text(vector)pointConverts a layout position to a text position
line_height()realReturns the light height used in the layout
em_width()realReturns the typical character width used in the layout
add_regions(key, [regions], scope, <icon>, <flags>)NoneAdd a set of regions to the view. If a set of regions already exists with the given key, they will be overwritten. The scope is used to source a color to draw the regions in, it should be the name of a scope, such as "comment" or "string". If the scope is empty, the regions won't be drawn.

The optional icon name, if given, will draw the named icons in the gutter next to each region. The icon will be tinted using the color associated with the scope. Valid icon names are dot, circle, bookmark and cross.

The optional flags parameter is a bitwise combination of:

  • sublime.DRAW_EMPTY. Draw empty regions with a vertical bar. By default, they aren't drawn at all.
  • sublime.HIDE_ON_MINIMAP. Don't show the regions on the minimap.
  • sublime.DRAW_EMPTY_AS_OVERWRITE. Draw empty regions with a horizontal bar instead of a vertical one.
  • sublime.DRAW_OUTLINED. Draw regions as an outline, rather than filled in.
  • sublime.PERSISTENT. Save the regions in the session.
  • sublime.HIDDEN. Don't draw the regions.
get_regions(key)[regions]Return the regions associated with the given key, if any
erase_regions(key)NoneRemoved the named regions
set_status(key, value)NoneAdds the status key to the view. The value will be displayed in the status bar, in a comma separated list of all status values, ordered by key. Setting the value to the empty string will clear the status.
get_status(key)StringReturns the previously assigned value associated with the key, if any.
erase_status(key)NoneClears the named status.
command_history(index, <modifying_only>)(String,Dict,int)Returns the command name, command arguments, and repeat count for the given history entry, as stored in the undo / redo stack.

Index 0 corresponds to the most recent command, -1 the command before that, and so on. Positive values for index indicate to look in the redo stack for commands. If the undo / redo history doesn't extend far enough, then (None, None, 0) will be returned.

Setting modifying_only to True (the default is False) will only return entries that modified the buffer.

fold([regions])boolFolds the given regions, returning False if they were already folded
fold(region)boolFolds the given region, returning False if it was already folded
unfold(region)[regions]Unfolds all text in the region, returning the unfolded regions
unfold([regions])[regions]Unfolds all text in the regions, returning the unfolded regions
encoding()StringReturns the encoding currently associated with the file
set_encoding(encoding)NoneApplies a new encoding to the file. This encoding will be used the next time the file is saved.
line_endings()StringReturns the line endings used by the current file.
set_line_endings(line_endings)NoneSets the line endings that will be applied when next saving.

Class sublime.RegionSet

Maintains a set of Regions, ensuring that none overlap. The regions are kept in sorted order.
MethodsReturn ValueDescription
clear()NoneRemoves all regions.
add(region)NoneAdds the given region. It will be merged with any intersecting regions already contained within the set.
add_all(region_set)NoneAdds all regions in the given set.
subtract(region)NoneSubtracts the region from all regions in the set.
contains(region)boolReturns true iff the given region is a subset.

Class sublime.Region

Represents an area of the buffer. Empty regions, where a == b are valid.
ConstructorsDescription
Region(a, b)Creates a Region with initial values a and b.
PropertiesTypeDescription
aintThe first end of the region.
bintThe second end of the region. May be less that a, in which case the region is a reversed one.
MethodsReturn ValueDescription
begin()intReturns the minimum of a and b.
end()intReturns the maximum of a and b.
size()intReturns the number of characters spanned by the region. Always >= 0.
empty()boolReturns true iff begin() == end().
cover(region)RegionReturns a Region spanning both this and the given regions.
intersection(region)RegionReturns the set intersection of the two regions.
intersects(region)boolReturns True iff this == region or both include one or more positions in common.
contains(region)boolReturns True iff the given region is a subset.
contains(point)boolReturns True iff begin() <= point <= end().

Class sublime.Edit

Edit objects have no functions, they exist to group buffer modifications.

They can be created by view.begin_edit(). Every call to view.begin_edit() must have a corresponding call to view.end_edit(), typically wrapped in a try ... finally block.

MethodsReturn ValueDescription
(no methods)

Class sublime.Window

MethodsReturn ValueDescription
id()intReturns a number that uniquely identifies this window.
new_file()ViewCreates a new file. The returned view will be empty, and its is_loaded method will return True.
open_file(file_name, <flags>)ViewOpens the named file, and returns the corresponding view. If the file is already opened, it will be brought to the front. Note that as file loading is asynchronous, operations on the returned view won't be possible until its is_loading() method returns False.

The optional flags parameter is a bitwise combination of:

  • sublime.ENCODED_POSITION. Indicates the file_name should be searched for a :row or :row:col suffix
  • sublime.TRANSIENT. Open the file as a preview only: it won't have a tab assigned it until modified
active_view()ViewReturns the currently edited view.
active_view_in_group(group)ViewReturns the currently edited view in the given group.
views()[View]Returns all open views in the window.
views_in_group(group)[View]Returns all open views in the given group.
num_groups()intReturns the number of view groups in the window.
active_group()intReturns the index of the currently selected group.
focus_group(group)NoneMakes the given group active.
focus_view(view)NoneSwitches to the given view.
get_view_index(view)(group, index)Returns the group, and index within the group of the view. Returns -1 if not found.
set_view_index(view, group, index)NoneMoves the view to the given group and index.
folders()[String]Returns a list of the currently open folders.
run_command(string, <args>)NoneRuns the named WindowCommand with the (optional) given arguments.
show_quick_panel(items, on_done, <flags>)NoneShows a quick panel, to select an item in a list. on_done will be called once, with the index of the selected item. If the quick panel was cancelled, on_done will be called with an argument of -1.

Items may be an array of strings, or an array of string arrays. In the latter case, each entry in the quick panel will show multiple rows.

Flags currently only has one option, sublime.MONOSPACE_FONT

show_input_panel(caption, initial_text, on_done, on_change, on_cancel)ViewShows the input panel, to collect a line of input from the user. on_done and on_change, if not None, should both be functions that expect a single string argument. on_cancel should be a function that expects no arguments. The view used for the input widget is returned.
get_output_panel(name)ViewReturns the view associated with the named output panel, created it if required. The output panel can be shown by running the show_panel window command, with the panel argument set to the name with an "output." prefix.

Class sublime.Settings

MethodsReturn ValueDescription
get(name)valueReturns the named setting.
get(name, default)valueReturns the named setting, or default if it's not defined.
set(name, value)NoneSets the named setting. Only primitive types, lists, and dictionaries are accepted.
erase(name)NoneRemoves the named setting. Does not remove it from any parent Settings.
has(name)boolReturns true iff the named option exists in this set of Settings or one of its parents.
add_on_change(key, on_change)NoneRegister a callback to be run whenever a setting in this object is changed.
clear_on_change(key)NoneRemove all callbacks registered with the given key.

Module sublime_plugin

MethodsReturn ValueDescription
(no methods)

Class sublime_plugin.EventListener

Note that many of these events are triggered by the buffer underlying the view, and thus the method is only called once, with the first view as the parameter.
MethodsReturn ValueDescription
on_new(view)NoneCalled when a new buffer is created.
on_clone(view)NoneCalled when a view is cloned from an existing one.
on_load(view)NoneCalled when the file is finished loading.
on_close(view)NoneCalled when a view is closed (note, there may still be other views into the same buffer).
on_pre_save(view)NoneCalled just before a view is saved.
on_post_save(view)NoneCalled after a view has been saved.
on_modified(view)NoneCalled after changes have been made to a view.
on_selection_modified(view)NoneCalled after the selection has been modified in a view.
on_activated(view)NoneCalled when a view gains input focus.
on_deactivated(view)NoneCalled when a view loses input focus.
on_query_context(view, key, operator, operand, match_all)bool or NoneCalled when determining to trigger a key binding with the given context key. If the plugin knows how to respond to the context, it should return either True of False. If the context is unknown, it should return None.

operator is one of:

  • sublime.OP_EQUAL. Is the value of the context equal to the operand?
  • sublime.OP_NOT_EQUAL. Is the value of the context not equal to the operand?
  • sublime.OP_REGEX_MATCH. Does the value of the context match the regex given in operand?
  • sublime.OP_NOT_REGEX_MATCH. Does the value of the context not match the regex given in operand?
  • sublime.OP_REGEX_CONTAINS. Does the value of the context contain a substring matching the regex given in operand?
  • sublime.OP_NOT_REGEX_CONTAINS. Does the value of the context not contain a substring matching the regex given in operand?

match_all should be used if the context relates to the selections: does every selection have to match (match_all = True), or is at least one matching enough (match_all = Fals)?

Class sublime_plugin.ApplicationCommand

MethodsReturn ValueDescription
run(<args>)NoneCalled when the command is run.
is_enabled(<args>)boolReturns true if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>)boolReturns true if the command should be shown in the menu at this time. The default implementation always returns True.
description(<args>)StringReturns a description of the command with the given arguments. Used in the menu, if no caption is provided. Return None to get the default description.

Class sublime_plugin.WindowCommand

WindowCommands are instantiated once per window. The Window object may be retrieved via self.window
MethodsReturn ValueDescription
run(<args>)NoneCalled when the command is run.
is_enabled(<args>)boolReturns true if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>)boolReturns true if the command should be shown in the menu at this time. The default implementation always returns True.
description(<args>)StringReturns a description of the command with the given arguments. Used in the menu, if no caption is provided. Return None to get the default description.

Class sublime_plugin.TextCommand

TextCommands are instantiated once per view. The View object may be retrieved via self.view
MethodsReturn ValueDescription
run(edit, <args>)NoneCalled when the command is run.
is_enabled(<args>)boolReturns true if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>)boolReturns true if the command should be shown in the menu at this time. The default implementation always returns True.
description(<args>)StringReturns a description of the command with the given arguments. Used in the menus, and for Undo / Redo descriptions. Return None to get the default description.
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.