Skip to content

Instantly share code, notes, and snippets.

@zw963
Last active February 5, 2024 17:30
Show Gist options
  • Save zw963/b02e1cd320538a7276f0e0b666b2b8cc to your computer and use it in GitHub Desktop.
Save zw963/b02e1cd320538a7276f0e0b666b2b8cc to your computer and use it in GitHub Desktop.
The best emacs like editor for use in terminal. (e.g. docker/remote vps)
-- Pull in the wezterm API
local wezterm = require 'wezterm'
local act = wezterm.action
local mux = wezterm.mux
-- This table will hold the configuration.
local config = {}
-- In newer versions of wezterm, use the config_builder which will
-- help provide clearer error messages
if wezterm.config_builder then
config = wezterm.config_builder()
end
config.set_environment_variables = {
SHELL="/usr/bin/bash"
}
config.window_close_confirmation = 'AlwaysPrompt'
-- 使用非 login shell, 即 .bashrc
config.default_prog = { '/usr/bin/bash' }
config.enable_scroll_bar = true
config.scrollback_lines = 1000000
-- enable this settings, and start from another terminal, e.g. konsole
-- config.debug_key_events = true
-- config.enable_wayland = false
-- This is where you actually apply your config choices
-- For example, changing the color scheme:
-- config.color_scheme = 'Vs Code Dark+ (Gogh)'
config.window_background_opacity = 0.85
config.font = wezterm.font 'Fira Code Retina'
config.disable_default_key_bindings = true
-- config.disable_default_mouse_bindings = true
config.leader = { key = 'x', mods = 'CTRL' }
config.keys = {
{
key = '3',
mods = 'LEADER',
action = act.SplitHorizontal { domain = 'CurrentPaneDomain' },
},
{
key = '2',
mods = 'LEADER',
action = act.SplitVertical { domain = 'CurrentPaneDomain' },
},
{
key = 'o',
mods = 'LEADER',
action = act.PaneSelect
},
{
key = '=',
mods = 'LEADER',
action = act.ResetFontSize
},
{
key = '-',
mods = 'LEADER',
action = act.ResetFontSize
},
{
key = 'r',
mods = 'LEADER',
action = act.ActivateKeyTable {
name = 'resize_pane',
one_shot = false
},
},
{
key = 'x',
mods = 'LEADER',
action = act.SendKey { key = 'x', mods = 'CTRL' },
},
{ key = 'h', mods = 'ALT', action = act.ActivatePaneDirection 'Left' },
{ key = 'l', mods = 'ALT', action = act.ActivatePaneDirection 'Right' },
{ key = 'k', mods = 'ALT', action = act.ActivatePaneDirection 'Up' },
{ key = 'j', mods = 'ALT', action = act.ActivatePaneDirection 'Down' },
{ key = 'RightArrow', mods = 'CTRL', action = act.AdjustPaneSize { 'Right', 5 } },
{ key = 'LeftArrow', mods = 'CTRL', action = act.AdjustPaneSize { 'Left', 5 } },
{ key = 'UpArrow', mods = 'CTRL', action = act.AdjustPaneSize { 'Up', 5 } },
{ key = 'DownArrow', mods = 'CTRL', action = act.AdjustPaneSize { 'Down', 5 } },
{ key = '0', mods = 'ALT', action = act.ActivateTabRelative(1) },
{ key = '9', mods = 'ALT', action = act.ActivateTabRelative(-1) },
{ key = '0', mods = 'CTRL|ALT', action = act.MoveTabRelative(1) },
{ key = '9', mods = 'CTRL|ALT', action = act.MoveTabRelative(-1) },
{ key = 'e', mods = 'SHIFT|CTRL', action = act.TogglePaneZoomState },
{ key = 'F10', mods = '', action = act.ToggleFullScreen },
-- { key = 'Enter', mods = 'ALT', action = act.ToggleFullScreen },
-- { key = '\'', mods = 'CTRL', action = act.SplitVertical{ domain = 'CurrentPaneDomain' } },
-- { key = '%', mods = 'SHIFT|CTRL', action = act.SplitHorizontal{ domain = 'CurrentPaneDomain' } },
{ key = '-', mods = 'CTRL', action = act.DecreaseFontSize },
{ key = '=', mods = 'CTRL', action = act.IncreaseFontSize },
-- { key = 'n', mods = 'CTRL', action = act.SpawnWindow },
-- { key = 'N', mods = 'SHIFT|CTRL', action = act.SpawnWindow },
-- { key = 'P', mods = 'CTRL', action = act.ActivateCommandPalette },
-- { key = 'P', mods = 'SHIFT|CTRL', action = act.ActivateCommandPalette },
-- { key = 'R', mods = 'CTRL', action = act.ReloadConfiguration },
-- { key = 'R', mods = 'SHIFT|CTRL', action = act.ReloadConfiguration },
-- { key = 'U', mods = 'CTRL', action = act.CharSelect{ copy_on_select = true, copy_to = 'ClipboardAndPrimarySelection' } },
-- { key = 'U', mods = 'SHIFT|CTRL', action = act.CharSelect{ copy_on_select = true, copy_to = 'ClipboardAndPrimarySelection' } },
-- { key = 'X', mods = 'CTRL', action = act.ActivateCopyMode },
-- { key = 'X', mods = 'SHIFT|CTRL', action = act.ActivateCopyMode },
-- { key = 'Z', mods = 'CTRL', action = act.TogglePaneZoomState },
-- { key = 'Z', mods = 'SHIFT|CTRL', action = act.TogglePaneZoomState },
{ key = 'k', mods = 'SHIFT|CTRL', action = act.ClearScrollback 'ScrollbackAndViewport' },
{ key = 'l', mods = 'SHIFT|CTRL', action = act.ShowDebugOverlay },
-- { key = 'm', mods = 'SHIFT|CTRL', action = act.Hide },
-- { key = 'n', mods = 'SHIFT|CTRL', action = act.SpawnWindow },
-- { key = 'n', mods = 'SUPER', action = act.SpawnWindow },
-- { key = 'p', mods = 'SHIFT|CTRL', action = act.ActivateCommandPalette },
-- { key = 'r', mods = 'SHIFT|CTRL', action = act.ReloadConfiguration },
-- { key = 'r', mods = 'SUPER', action = act.ReloadConfiguration },
-- { key = 't', mods = 'SHIFT|CTRL', action = act.SpawnTab 'CurrentPaneDomain' },
{
key = 't',
mods = 'SHIFT|CTRL',
action = wezterm.action_callback(
function(win, pane)
local mux_win = win:mux_window()
local idx = active_tab_idx(mux_win)
local tab = mux_win:spawn_tab({})
win:perform_action(wezterm.action.MoveTab(idx+1), pane)
end
),
},
-- { key = 'u', mods = 'SHIFT|CTRL', action = act.CharSelect{ copy_on_select = true, copy_to = 'ClipboardAndPrimarySelection' } },
{ key = 'y', mods = 'CTRL', action = act.PasteFrom 'Clipboard' },
{ key = 'w', mods = 'SHIFT|CTRL', action = act.CloseCurrentTab{ confirm = true } },
{ key = 'x', mods = 'SHIFT|CTRL', action = act.ActivateCopyMode },
{ key = 'z', mods = 'SHIFT|CTRL', action = act.TogglePaneZoomState },
{ key = 'phys:Space', mods = 'SHIFT|CTRL', action = act.QuickSelect },
{ key = 'f', mods = 'SHIFT|CTRL', action = act.Search 'CurrentSelectionOrEmptyString' },
{ key = 'w', mods = 'ALT', action = act.EmitEvent 'CopyToHackForDte' },
{ key = 's', mods = 'CTRL', action = act.EmitEvent 'SearchHackForDte' },
{ key = '<', mods = 'SHIFT|ALT', action = act.EmitEvent 'ScrollToTopForDte' },
{ key = '>', mods = 'SHIFT|ALT', action = act.EmitEvent 'ScrollToBottomForDte' },
{
key = 'v', mods = 'CTRL',
action = wezterm.action_callback(
function(window, pane)
ScrollByPageForDte(window, pane, 1, {key = 'v', mods = 'CTRL'})
end
)
},
{
key = 'v', mods = 'ALT',
action = wezterm.action_callback(
function(window, pane)
ScrollByPageForDte(window, pane, -1, {key = 'v', mods = 'ALT'})
end
)
},
{
key = 'p', mods = 'ALT',
action = wezterm.action_callback(
function(window, pane)
ScrollByLineForDte(window, pane, -2, {key = 'p', mods = 'ALT'})
end
)
},
{
key = 'n', mods = 'ALT',
action = wezterm.action_callback(
function(window, pane)
ScrollByLineForDte(window, pane, 2, {key = 'n', mods = 'ALT'})
end
)
},
-- { key = 'LeftArrow', mods = 'SHIFT|CTRL', action = act.ActivatePaneDirection 'Left' },
-- { key = 'LeftArrow', mods = 'SHIFT|ALT|CTRL', action = act.AdjustPaneSize{ 'Left', 1 } },
-- { key = 'RightArrow', mods = 'SHIFT|CTRL', action = act.ActivatePaneDirection 'Right' },
-- { key = 'RightArrow', mods = 'SHIFT|ALT|CTRL', action = act.AdjustPaneSize{ 'Right', 1 } },
-- { key = 'UpArrow', mods = 'SHIFT|CTRL', action = act.ActivatePaneDirection 'Up' },
-- { key = 'UpArrow', mods = 'SHIFT|ALT|CTRL', action = act.AdjustPaneSize{ 'Up', 1 } },
-- { key = 'DownArrow', mods = 'SHIFT|CTRL', action = act.ActivatePaneDirection 'Down' },
-- { key = 'DownArrow', mods = 'SHIFT|ALT|CTRL', action = act.AdjustPaneSize{ 'Down', 1 } },
-- { key = 'Insert', mods = 'SHIFT', action = act.PasteFrom 'PrimarySelection' },
-- { key = 'Insert', mods = 'CTRL', action = act.CopyTo 'PrimarySelection' },
}
-- Show which key table is active in the status area
wezterm.on(
'update-status',
function(window, pane)
local name = window:active_key_table()
if name then
name = 'TABLE: ' .. name
end
window:set_right_status(name or '')
end
)
-- key_index = #config.keys
-- wezterm.on(
-- 'update-status',
-- function(window, pane)
-- local info = pane:get_foreground_process_info()
-- if info ~= nil and info.name ~= 'dte' then
-- key1 = { key = 'w', mods = 'ALT', action = act.CopyTo 'Clipboard' }
-- last_key = config.keys[key_index+1]
-- if last_key ~= nil and last_key.key == key1.key and last_key.mods == key1.mods then
-- return
-- end
-- table.insert(config.keys, key1)
-- end
-- end
-- )
wezterm.on(
'CopyToHackForDte',
function(window, pane)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.CopyTo 'Clipboard', pane)
else
window:perform_action(act.SendKey { key = 'w', mods = 'ALT' }, pane)
end
end
)
wezterm.on(
'SearchHackForDte',
function(window, pane)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.Search 'CurrentSelectionOrEmptyString', pane)
else
window:perform_action(act.SendKey { key = 's', mods = 'CTRL' }, pane)
end
end
)
wezterm.on(
'ScrollToTopForDte',
function(window, pane)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.ScrollToTop, pane)
else
window:perform_action(act.SendKey { key = '<', mods = 'SHIFT|ALT' }, pane)
end
end
)
wezterm.on(
'ScrollToBottomForDte',
function(window, pane)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.ScrollToBottom, pane)
else
window:perform_action(act.SendKey { key = '>', mods = 'SHIFT|ALT' }, pane)
end
end
)
function ScrollByPageForDte(window, pane, page_number, key)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.ScrollByPage(page_number), pane)
else
window:perform_action(act.SendKey(key), pane)
end
end
function ScrollByLineForDte(window, pane, line_number, key)
local info = pane:get_foreground_process_info()
if info ~= nil and info.name ~= 'dte' then
window:perform_action(act.ScrollByLine(line_number), pane)
else
window:perform_action(act.SendKey(key), pane)
end
end
function active_tab_idx(mux_win)
for _, item in ipairs(mux_win:tabs_with_info()) do
-- wezterm.log_info('idx: ', idx, 'tab:', item)
if item.is_active then
return item.index
end
end
end
function maximum_when_startup()
-- start use maximum window mode. can be close use F10 (bind blow)
wezterm.on(
'gui-startup',
function(window)
local tab, pane, window = mux.spawn_window(cmd or {})
local gui_window = window:gui_window();
gui_window:perform_action(wezterm.action.ToggleFullScreen, pane)
end
)
end
-- maximum_when_startup()
config.key_tables = {
resize_pane = {
{ key = 'LeftArrow', action = act.AdjustPaneSize { 'Left', 5 } },
{ key = 'RightArrow', action = act.AdjustPaneSize { 'Right', 5 } },
{ key = 'UpArrow', action = act.AdjustPaneSize { 'Up', 5 } },
{ key = 'DownArrow', action = act.AdjustPaneSize { 'Down', 5 } },
-- Cancel the mode by pressing escape
{ key = 'Escape', action = 'PopKeyTable' },
},
search_mode = {
{ key = 'Enter', mods = 'NONE', action = act.CopyMode 'PriorMatch' },
{ key = 'Escape', mods = 'NONE', action = act.CopyMode 'Close' },
{ key = 'g', mods = 'CTRL', action = act.CopyMode 'Close' },
{ key = 'n', mods = 'CTRL', action = act.CopyMode 'NextMatch' },
{ key = 'p', mods = 'CTRL', action = act.CopyMode 'PriorMatch' },
{ key = 'r', mods = 'CTRL', action = act.CopyMode 'CycleMatchType' },
{ key = 'u', mods = 'CTRL', action = act.CopyMode 'ClearPattern' },
{ key = 'PageUp', mods = 'NONE', action = act.CopyMode 'PriorMatchPage' },
{ key = 'PageDown', mods = 'NONE', action = act.CopyMode 'NextMatchPage' },
{ key = 'UpArrow', mods = 'NONE', action = act.CopyMode 'PriorMatch' },
{ key = 'DownArrow', mods = 'NONE', action = act.CopyMode 'NextMatch' },
},
}
config.mouse_bindings = {
{
event = { Up = { streak = 1, button = 'Left' } },
mods = 'NONE',
action = act.Nop,
},
}
-- and finally, return the configuration to wezterm
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment