What is Shell Integration?
Shell integration uses OSC (Operating System Command) escape sequences to communicate between your shell and WezTerm. These sequences provide metadata about:- Current working directory
- Command prompts vs output
- User and hostname information
- Custom shell state
Shell integration is automatically activated on Fedora, Debian, and Arch Linux packages for Bash and Zsh. On other systems, you’ll need to configure it manually.
Key Features
1
Working Directory Tracking
New tabs and panes automatically inherit the current working directory from the active pane.
2
Semantic Navigation
Jump through scrollback to the start of previous commands or select complete command output.
3
User Variables
Track custom shell state and trigger events based on shell activity.
4
Prompt Detection
Distinguish between prompts, input, and output for better copy/paste and navigation.
OSC 7: Working Directory
OSC 7 tells WezTerm the current working directory, enabling smart directory inheritance when spawning new tabs or panes.How it Works
Your shell emits an escape sequence with the current directory as a URL:Bash Configuration
Add to your~/.bashrc:
Zsh Configuration
Add to your~/.zshrc:
Windows cmd.exe
Configure in your.wezterm.lua:
PowerShell
Add to your PowerShell profile:PowerShell with Starship
If using Starship:OSC 133: Semantic Prompt Markup
OSC 133 marks distinct zones in your terminal output:- Prompt - Your shell prompt
- Input - Commands you type
- Output - Command results
Benefits
Select Command Output
Select Command Output
Instantly select all output from a command:
Implementation
The complete OSC 133 specification uses these sequences:OSC 133 ; A ST- Mark start of promptOSC 133 ; B ST- Mark start of input (command)OSC 133 ; C ST- Mark start of outputOSC 133 ; D [; exit_code] ST- Mark end of output
Learn more about OSC 133 Semantic Prompts.
OSC 1337: User Variables
User variables are pane-specific variables (similar to environment variables but scoped to a pane rather than a process).Built-in Variables
When using WezTerm’s shell integration, these variables are automatically maintained:Setting Custom Variables
Use the__wezterm_set_user_var function provided by shell integration:
Using Variables in Configuration
User variables trigger events that you can handle in your configuration:Accessing Variables
Read user variables from Lua:Complete Shell Integration Setup
WezTerm provides shell integration scripts in the wezterm repository.Installing for Bash/Zsh
1
Download the integration script
2
Source in your shell config
Add to
~/.bashrc or ~/.zshrc:3
Restart your shell
System-wide Installation
On most Unix systems, you can install to/etc/profile.d/:
Xonsh Support
For Xonsh users, use the term-integrations plugin:Advanced Examples
Dynamic Tab Titles with User Vars
Update tab titles based on the current command:Git Branch in Status Bar
In your shell configuration:.wezterm.lua:
Command Duration Tracking
Clink Integration (Windows)
Clink brings bash-style line editing to Windows cmd.exe.Configuration
Troubleshooting
Working directory not inherited
Working directory not inherited
Verify OSC 7 is being emitted:Check that
WEZTERM_PANE environment variable is set.User vars not working in tmux
User vars not working in tmux
Ensure your Restart tmux after updating the configuration.
tmux.conf includes:Shell integration not loaded
Shell integration not loaded
Verify the integration script is sourced:If the function is not found, check that your shell config sources the integration file.
Best Practices
1
Use provided integration scripts
Start with WezTerm’s official shell integration scripts rather than writing your own.
2
Test incrementally
Add features one at a time (OSC 7, then OSC 133, then custom user vars) to isolate issues.
3
Check for conflicts
Some prompt frameworks (like Starship or Oh My Zsh) may conflict. Check their documentation for integration guides.
4
Update regularly
Shell integration improves over time. Periodically update your integration scripts.
Learn More
- Terminal Basics - Understand escape sequences and PTYs
- Multiplexing - Tabs, panes, and domains
- Escape Sequences - Complete reference
- Events - Handle user-var-changed and other events