Last active
February 17, 2025 04:27
-
-
Save dangkhoasdc/197e60f7fe0619f393fb259eefd6c721 to your computer and use it in GitHub Desktop.
wezterm config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Copied from: https://alexplescan.com/posts/2024/08/10/wezterm/ | |
local wezterm = require 'wezterm' | |
local config = wezterm.config_builder() | |
local act = wezterm.action | |
config.font_size = 13 | |
config.window_background_opacity = 0.9 | |
config.macos_window_background_blur = 30 | |
-- visual | |
wezterm.on('update-right-status', function(window, pane) | |
-- Each element holds the text for a cell in a "powerline" style << fade | |
local cells = {} | |
-- Figure out the cwd and host of the current pane. | |
-- This will pick up the hostname for the remote host if your | |
-- shell is using OSC 7 on the remote host. | |
local cwd_uri = pane:get_current_working_dir() | |
if cwd_uri then | |
local cwd = '' | |
local hostname = '' | |
if type(cwd_uri) == 'userdata' then | |
-- Running on a newer version of wezterm and we have | |
-- a URL object here, making this simple! | |
cwd = cwd_uri.file_path | |
hostname = cwd_uri.host or wezterm.hostname() | |
else | |
-- an older version of wezterm, 20230712-072601-f4abf8fd or earlier, | |
-- which doesn't have the Url object | |
cwd_uri = cwd_uri:sub(8) | |
local slash = cwd_uri:find '/' | |
if slash then | |
hostname = cwd_uri:sub(1, slash - 1) | |
-- and extract the cwd from the uri, decoding %-encoding | |
cwd = cwd_uri:sub(slash):gsub('%%(%x%x)', function(hex) | |
return string.char(tonumber(hex, 16)) | |
end) | |
end | |
end | |
-- Remove the domain name portion of the hostname | |
local dot = hostname:find '[.]' | |
if dot then | |
hostname = hostname:sub(1, dot - 1) | |
end | |
if hostname == '' then | |
hostname = wezterm.hostname() | |
end | |
table.insert(cells, cwd) | |
table.insert(cells, hostname) | |
end | |
-- I like my date/time in this style: "Wed Mar 3 08:14" | |
local date = wezterm.strftime '%a %b %-d %H:%M' | |
table.insert(cells, date) | |
-- An entry for each battery (typically 0 or 1 battery) | |
for _, b in ipairs(wezterm.battery_info()) do | |
table.insert(cells, string.format('%.0f%%', b.state_of_charge * 100)) | |
end | |
-- The powerline < symbol | |
local LEFT_ARROW = utf8.char(0xe0b3) | |
-- The filled in variant of the < symbol | |
local SOLID_LEFT_ARROW = utf8.char(0xe0b2) | |
-- Color palette for the backgrounds of each cell | |
local colors = wezterm.gradient_colors({preset="Inferno"}, 10) | |
-- Foreground color for the text across the fade | |
local text_fg = '#c0c0c0' | |
-- The elements to be formatted | |
local elements = {} | |
-- How many cells have been formatted | |
local num_cells = 0 | |
-- Translate a cell into elements | |
local function push(text, is_last) | |
local cell_no = num_cells + 1 | |
table.insert(elements, { Foreground = { Color = text_fg } }) | |
table.insert(elements, { Background = { Color = colors[cell_no] } }) | |
table.insert(elements, { Text = ' ' .. text .. ' ' }) | |
if not is_last then | |
table.insert(elements, { Foreground = { Color = colors[cell_no + 1] } }) | |
table.insert(elements, { Text = SOLID_LEFT_ARROW }) | |
end | |
num_cells = num_cells + 1 | |
end | |
while #cells > 0 do | |
local cell = table.remove(cells, 1) | |
push(cell, #cells == 0) | |
end | |
window:set_right_status(wezterm.format(elements)) | |
end) | |
-- hotkeys | |
config.leader = { key = 'b', mods = 'ALT', timeout_milliseconds = 1000 } | |
config.keys = { | |
-- ... add these new entries to your config.keys table | |
{ | |
-- I'm used to tmux bindings, so am using the quotes (") key to | |
-- split horizontally, and the percent (%) key to split vertically. | |
key = '"', | |
-- Note that instead of a key modifier mapped to a key on your keyboard | |
-- like CTRL or ALT, we can use the LEADER modifier instead. | |
-- This means that this binding will be invoked when you press the leader | |
-- (CTRL + A), quickly followed by quotes ("). | |
mods = 'LEADER', | |
action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' }, | |
}, | |
{ | |
key = '%', | |
mods = 'LEADER', | |
action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' }, | |
}, | |
-- ... add these new entries to your config.keys table | |
{ | |
-- I like to use vim direction keybindings, but feel free to replace | |
-- with directional arrows instead. | |
key = 'j', -- or DownArrow | |
mods = 'LEADER', | |
action = wezterm.action.ActivatePaneDirection('Down'), | |
}, | |
{ | |
key = 'k', -- or UpArrow | |
mods = 'LEADER', | |
action = wezterm.action.ActivatePaneDirection('Up'), | |
}, | |
{ | |
key = 'h', -- or LeftArrow | |
mods = 'LEADER', | |
action = wezterm.action.ActivatePaneDirection('Left'), | |
}, | |
{ | |
key = 'l', -- or RightArrow | |
mods = 'LEADER', | |
action = wezterm.action.ActivatePaneDirection('Right'), | |
}, | |
{ | |
key = 'x', -- or RightArrow | |
mods = 'LEADER', | |
action = wezterm.action.CloseCurrentPane {confirm = true}, | |
}, | |
{ | |
-- When we push LEADER + R... | |
key = 'r', | |
mods = 'LEADER', | |
-- Activate the `resize_panes` keytable | |
action = wezterm.action.ActivateKeyTable { | |
name = 'resize_panes', | |
-- Ensures the keytable stays active after it handles its | |
-- first keypress. | |
one_shot = false, | |
-- Deactivate the keytable after a timeout. | |
timeout_milliseconds = 1000, | |
} | |
}, | |
-- workspaces | |
{ | |
key = '9', | |
mods = 'ALT', | |
action = act.ShowLauncherArgs { | |
flags = 'FUZZY|WORKSPACES', | |
}, | |
}, | |
{ | |
key = 'N', | |
mods = 'CTRL|SHIFT', | |
action = act.PromptInputLine { | |
description = wezterm.format { | |
{ Attribute = { Intensity = 'Bold' } }, | |
{ Foreground = { AnsiColor = 'Fuchsia' } }, | |
{ Text = 'Enter name for new workspace' }, | |
}, | |
action = wezterm.action_callback(function(window, pane, line) | |
-- line will be `nil` if they hit escape without entering anything | |
-- An empty string if they just hit enter | |
-- Or the actual line of text they wrote | |
if line then | |
window:perform_action( | |
act.SwitchToWorkspace { | |
name = line, | |
}, | |
pane | |
) | |
end | |
end), | |
}, | |
}, | |
} | |
local function resize_pane(key, direction) | |
return { | |
key = key, | |
action = wezterm.action.AdjustPaneSize { direction, 3 } | |
} | |
end | |
config.key_tables = { | |
resize_panes = { | |
resize_pane('j', 'Down'), | |
resize_pane('k', 'Up'), | |
resize_pane('h', 'Left'), | |
resize_pane('l', 'Right'), | |
}, | |
} | |
return config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment