Summary
Effortlessly clear the viewport in WezTerm without losing scrollback:
By customizing your wezterm.lua you can emulate Windows PowerShell’s Ctrl+L behavior – clearing the visible screen while preserving the scroll history.
Root Cause
- The default action
ClearScrollbackAndViewportmerges the viewport clear with scrubbing the scrollback buffer. - WezTerm exposes a separate action,
ClearViewport, that only clears the visible area, keeping the scrollback intact.
Why This Happens in Real Systems
- Terminal emulators often unify “clear screen” and “clear history” as a single command to reduce complexity.
- Users rarely need the scrollback preserved when a screen is refreshed; defaults favor a clean slate.
- Custom keybindings can override this behavior, but the wrong action constant must be used.
Real-World Impact
-
With the wrong binding
- Pressing
Ctrl+Ldeletes all past output. - Users lose context, making debugging or manual verification harder.
- Pressing
-
With the correct binding
- The viewport collapses, but all previous lines stay scrollable.
- Users retain full history while having a tidy workspace.
Example Code (if necessary and relevant)
local wezterm = require 'wezterm'
local config = {}
-- Ensure the `keys` table exists
config.keys = config.keys or {}
-- Map Ctrl+L to clear only the viewport, preserving scrollback
table.insert(config.keys, {
key = 'l',
mods = 'CTRL',
action = wezterm.action.ClearViewport,
})
return config
How Senior Engineers Fix It
- Identify the correct action – use
ClearViewportinstead ofClearScrollbackAndViewport. - Add the binding in a dedicated section of the
wezterm.luato keep configurations readable. - Test on the target platform (Windows, macOS, Linux) to ensure the key combination behaves as expected.
- Document the change in the project wiki or README so others reuse the pattern.
Why Juniors Miss It
- Assuming a single “clear screen” command covers all use cases.
- Overlooking the action namespace (
ClearViewportvs.ClearScrollbackAndViewport). - Skipping alphabetical reads of docs – the key bind vocabulary in WezTerm is case‑sensitive.
- Adding code inside prose – leading to syntax errors or mis‑displayed config files.