Skip to main content

Context API (cx)

The cx object is the primary interface for reading Yazi's UI state. It is available in sync entry points (entry_sync) and in any callback that receives a job parameter.

Top-Level Fields

FieldTypeDescription
cx.activeTabThe currently focused tab
cx.tabs[Tab]All open tabs
cx.tasksTasksTask manager state
cx.yanked[File]Currently yanked (copied) files
cx.selected[File]Currently selected files
cx.modeModeCurrent manager mode
cx.visualVisualVisual selection state (if active)

Tab Struct

Each Tab provides:

FieldTypeDescription
currentFolderThe currently viewed folder
parentFolderThe parent folder
previewFolderThe preview folder
windowsintegerNumber of windows split
modestringTab mode (normal, select, etc.)
namestringTab name (customizable)
cwdUrlCurrent working directory URL

Folder

A Folder contains:

FieldTypeDescription
files[File]List of files in the folder
hoveredFileThe currently hovered file
offsetintegerScroll offset
cwdUrlThis folder's working directory
-- Access currently hovered file
local file = cx.active.current.hovered
if file then
ya.err("Hovered: " .. file.name)
end
-- List all files in current directory
for _, f in ipairs(cx.active.current.files) do
ya.err(f.name)
end

Mode Struct

FieldTypeDescription
modestringMode name: "normal", "select", "unset"
stylestringDisplay style name

Visual Mode

When visual mode is active (cx.visual):

FieldTypeDescription
startintegerStarting index of selection
endintegerEnding index of selection
modestringVisual mode direction
if cx.visual then
local count = math.abs(cx.visual.end - cx.visual.start) + 1
ya.err("Visual selection: " .. count .. " files")
end

Tasks

The cx.tasks object provides access to the task manager:

Field/MethodDescription
cx.tasks:running()Number of running tasks
cx.tasks:failed()Number of failed tasks
cx.tasks:finished()Number of completed tasks
cx.tasks:total()Total tasks
info

cx is read-only in sync contexts. To modify state, emit events via ya.emit() or use ya.sync() with a callback.