Plugin Basics
Yazi plugins are written in Lua and follow a simple structure. Each plugin is a Lua module that returns a table with functions.Minimal Plugin Example
Here’s the simplest possible plugin:Plugin Structure
Module Pattern
All plugins follow this pattern:M table can contain:
- Methods (functions)
- State variables
- Configuration options
Entry Points
Different plugin types have different entry points:Step-by-Step Plugin Creation
Let’s create a plugin that counts files in the current directory.1
Create the plugin file
Create
~/.config/yazi/plugins/filecount.lua:2
Access current directory
Use the
cx (context) global to access Yazi’s state:3
Add filtering logic
Count different file types:
4
Bind to a key
Add to
keymap.toml:Accessing Yazi State
Yazi provides global objects to access its state:cx - Context
The main state object:
rt - Runtime
Configuration and runtime settings:
th - Theme
Access theme colors and styles:
Using the Lua API
Yazi provides a rich API through theya global:
Notifications
User Input
File Operations
Running Commands
Emitting Events
Plugin Configuration
Setup Function
Plugins can have asetup() function for initialization:
init.lua:
Async Programming
For long-running operations, use async:Error Handling
Best Practices
Use local variables
Use local variables
Always declare variables with
local to avoid polluting the global namespace:Check for nil values
Check for nil values
Always validate data before using it:
Use async for slow operations
Use async for slow operations
Don’t block the UI thread:
Provide user feedback
Provide user feedback
Always notify users of success or failure:
Debugging
Print to stderr
Check Yazi logs
Logs are written to:- Linux/macOS:
~/.local/state/yazi/yazi.log - Windows:
%APPDATA%\yazi\state\yazi.log
Next Steps
UI Plugins
Customize Yazi’s interface
Functional Plugins
Add new commands
Previewers
Create file previewers
Fetchers
Build metadata fetchers