Using Windows

Basics

VPE provides a Window as a wrapper around Vim’s built in window class. All VPE objects return a Window instead of a plain old Vim window.

A given Window becomes unusable if the underlying Vim window is closed, so check that valid is True if there is any doubt. In practice, most code is able to assume that the valid property is set.

Attributes and methods

The Window class provides all the attributes and methods of Vim’s window class. However some attribute types and return values are different. In such cases, the VPE value is compatible with the Vim type. The differences are listed in the folling table.

Attrribute

Vim type

VPE type

vars

vim.dictionary

Variables

options

vim.options

Options

tabpage

vim.tabpage

TabPage

buffer

vim.buffer

Buffer

The Window class also provides a number of additional properties and methods, including:

The temp_options context manager provides exactly the same function for a Window s does the Buffer.temp_options method. Read temporary buffer options to see how this is used.

The id is the unique window ID (not its number) that is required as the argument for a number of built-in Vim functions; such as getwininfo().

Context Managers

The vpe module provides some context managers that support working with windows.

The saved_current_window context manager is useful when executing code that might switch to another window.

with vpe.saved_current_window():
    ...
    # May change current window, but that change will be undone when the
    # context exits.
    split_window_if_required()

If you need to temporarily switch to a different window, use temp_active_window.

with vpe.temp_active_window(alt_window):
    # Now vim.current.window will be alt_window for the duration of the
    # context.
    ...

Vim provides the functions winsaveview() and winrestview() as a mechanism to ‘protect’ the user from operations that jump around a buffer. The saved_winview context manager wraps these up more conveniently.

with vpe.saved_winview():
    vim.command('$')
    ...