Python API

See also

Official Documentation
API documentation.

Missing in the official docs

There are quite a few things that are not (yet) documented in the official docs, this section tries to solve this.

sublime module

class sublime.Window

This class represents windows in Sublime Text and provides an interface of methods to interact with them. For all available methods, see the official documentation.

set_layout(layout)

Changes the tile-based panel layout of view groups.

Parameters:layout (dict) – specifies the new layout, see below
Returns:None

Expects a dictionary like this:

{"cols": [float], "rows": [float], "cells": [[int]]}

where [type] represents a list of type.

cols
A list of the column separators (floating point numbers), should start with 0 (left) and end with 1 (right).
rows
A list of the row separators (floating point numbers), should start with 0 (top) and end with 1 (bottom).
cells

A list of cell lists which describe a cell’s boundaries each. Cells can be imagines as rectangles with the rows and cols specified along in this dictionary. Think like this:

[x1, y1, x2, y2]

where all values are integers respectively and map to the cols and rows indicies. Thus, a cell with [0, 0, 1, 2] translates to a cell from the top left to the first column and the second row separator (in a 2x2 grid this would be bottom center).

Note

rows and cols are not tested for boundaries and they are not adjusted either. Thus, it is possible to specify values lower than 0 or higher than 1 and Sublime Text will in fact treat them accordingly. That means you can crop views or create borders. It is not known whether the “background color” of these empty spaces can be modified, the default is black. Use at your own risk!

The order of column or row separators is not checked either. If you, for example, use a reversed column list like [1, 0.5, 0] you get to see two black panels. Sublime Text is unusable in this state.

Examples:

# A 2-column layout with a separator in the middle
window.set_layout({
    "cols": [0, 0.5, 1],
    "rows": [0, 1],
    "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
})
# A 2x2 grid layout with all separators in the middle
window.set_layout({
    "cols": [0, 0.5, 1],
    "rows": [0, 0.5, 1],
    "cells": [[0, 0, 1, 1], [1, 0, 2, 1],
              [0, 1, 1, 2], [1, 1, 2, 2]]
})
# A 2-column layout with the separator in the middle and the right
# column being split in half
window.set_layout({
    "cols": [0, 0.5, 1],
    "rows": [0, 0.5, 1],
    "cells": [[0, 0, 1, 2], [1, 0, 2, 1],
                            [1, 1, 2, 2]]
})
class sublime.View

Similar to Window, this class represents views in Sublime Text and provides an interface of methods to interact with them. For all available methods, see the official documentation.

match_selector(point, selector)

Matches the scope at point against the specified selector.

Parameters:
  • point (int) – Point in the view whose scope the selector should be matched against.
  • selector (str) – A scope selector.
Returns bool:

Whether the selector matches or not.

Equivalent to:

view.score_selector(point, selector) != 0
# or
sublime.score_selector(view.scope_name(point), selector) != 0

sublime_plugin module

class sublime_plugin.EventListener
on_query_completions(view, prefix, locations)

Called whenever the completion list is requested.

This accounts for all views and all windows, so in order to provide syntax-specific completions you should test the current scope of locations with match_selector().

view
A View instance for which the completions should be made.
prefix
The text entered so far. This is only until the next word separator.
locations

Array of points in view where the completion should be inserted. This can be interpreted as the current selection.

If you want to handle completions that depend on word separator characters you need to test each location individually. See Completions with multiple cursors on how Sublime Text handles completions with multiple cursors.

Return value

Expects two (three) formats for return values:

  1. [[trigger, contents], ...]

    A list of completions similar to Trigger-based Completions but without mapping keys. trigger may use the \\t description syntax.

    Note: In Sublime Text 3, completions may also consist of plain strings instead of the trigger-contents-list.

  2. ([[trigger, contents], ...], flags)

    Basically the same as above but wrapped in a 2-sized tuple. The second element, the flags, may be a bitwise OR combination of these flags:

    sublime.INHIBIT_WORD_COMPLETIONS

    Prevents Sublime Text from adding its word completions to the completion list after all plugins have been processed. This consists of any word in the current document that is longer than 3 characters.

    sublime.INHIBIT_EXPLICIT_COMPLETIONS

    Prevents Sublime Text from suggesting entries from .sublime-completions files. Therefore, with this flag set, it will only show completions that are returned by plugins from their on_query_completions methods (along with word completions unless the above flag is also set.)

    Flags are shared among all completions, once set by one plugin you can not revert them.

  3. Anything else (e.g. None)

    No effect.

Example:
See Another Plugin Example: Feeding the Completions List for an example on how to use this event.

Exploring the API

A quick way to see the API in action:

  1. Add Packages/Default (Preferences | Browse Packages…) to your project.
  2. Ctrl + Shift + F
  3. Enter *.py in the In Files: field
  4. Check Use Buffer option
  5. Search API name
  6. F4
  7. Study relevant source code