/ wezterm / config / keymaps.lua
keymaps.lua
  1  local backdrops = require('extensions.backdrops')
  2  local platform = require('extensions.platform')()
  3  local wezterm = require('wezterm')
  4  local act = wezterm.action
  5  
  6  local mod = {}
  7  local keyboard = {}
  8  if platform.is_mac then
  9    mod.NONE = 'NONE'
 10    mod.CMD = 'SUPER'
 11    mod.CMD_REV = 'SUPER|SHIFT'
 12    mod.CTRL = 'CTRL'
 13    mod.CTRL_REV = 'CTRL|SHIFT'
 14    mod.LEADER = 'ALT'
 15    keyboard.COMPOSED_KEYS = true -- when curly brackets are opt + shift + 8/9, not ctrl
 16  else
 17    mod.NONE = 'NONE'
 18    mod.CMD = 'ALT' -- to avoid conflicting with other key shortcuts
 19    mod.CMD_REV = 'ALT|SHIFT'
 20    mod.CTRL = 'CTRL'
 21    mod.CTRL_REV = 'CTRL|SHIFT'
 22    mod.LEADER = 'ALT|CTRL'
 23    keyboard.COMPOSED_KEYS = false
 24  end
 25  
 26  local keys = {
 27    -- misc/useful --
 28    {
 29      mods = mod.CMD,
 30      key = 'p',
 31      action = act.ActivateCommandPalette,
 32    },
 33    {
 34      key = 'F2',
 35      mods = mod.CTRL,
 36      action = act.ShowLauncherArgs({ flags = 'FUZZY|TABS|WORKSPACES' }),
 37    },
 38    {
 39      key = 'F3',
 40      mods = mod.CTRL,
 41      action = act.PromptInputLine({
 42        description = wezterm.format({
 43          { Attribute = { Intensity = 'Bold' } },
 44          { Foreground = { AnsiColor = 'Fuchsia' } },
 45          { Text = 'Enter name for new workspace' },
 46        }),
 47        action = wezterm.action_callback(function(window, pane, line)
 48          -- line will be `nil` if they hit escape without entering anything
 49          -- An empty string if they just hit enter
 50          -- Or the actual line of text they wrote
 51          if line then
 52            window:perform_action(
 53              act.SwitchToWorkspace({
 54                name = line,
 55              }),
 56              pane
 57            )
 58          end
 59        end),
 60      }),
 61    },
 62    {
 63      key = 'F4',
 64      mods = mod.CTRL,
 65      action = act.ShowLauncher,
 66    },
 67    {
 68      key = 'F5',
 69      mods = mod.CTRL,
 70      action = 'ActivateCopyMode',
 71    },
 72    {
 73      key = 'F11',
 74      mods = mod.CTRL,
 75      action = act.ToggleFullScreen,
 76    },
 77    {
 78      key = 'F12',
 79      mods = mod.CTRL,
 80      action = act.ShowDebugOverlay,
 81    },
 82    {
 83      key = 'f',
 84      mods = mod.CMD,
 85      action = act.Search({ CaseInSensitiveString = '' }),
 86    },
 87    {
 88      key = 'u',
 89      mods = mod.CMD,
 90      action = wezterm.action.QuickSelectArgs({
 91        label = 'open url',
 92        patterns = {
 93          '\\((https?://\\S+)\\)',
 94          '\\[(https?://\\S+)\\]',
 95          '\\{(https?://\\S+)\\}',
 96          '<(https?://\\S+)>',
 97          '\\bhttps?://\\S+[)/a-zA-Z0-9-]+',
 98        },
 99        action = wezterm.action_callback(function(window, pane)
100          local url = window:get_selection_text_for_pane(pane)
101          wezterm.log_info('opening: ' .. url)
102          wezterm.open_with(url)
103        end),
104      }),
105    },
106  
107    -- cursor movement --
108    { key = 'LeftArrow', mods = mod.CMD, action = act.SendString('\x1bOH') },
109    { key = 'RightArrow', mods = mod.CMD, action = act.SendString('\x1bOF') },
110    { key = 'Backspace', mods = mod.CMD, action = act.SendString('\x15') },
111  
112    -- copy/paste --
113    { key = 'c', mods = mod.CMD, action = act.CopyTo('Clipboard') },
114    { key = 'v', mods = mod.CMD, action = act.PasteFrom('Clipboard') },
115    { key = 'k', mods = mod.CTRL, action = act.ClearScrollback('ScrollbackAndViewport') },
116  
117    -- tabs --
118    -- tabs: spawn+close
119    { key = 't', mods = mod.CMD, action = act.SpawnTab('DefaultDomain') },
120    { key = 'w', mods = mod.CMD, action = act.CloseCurrentPane({ confirm = false }) },
121  
122    -- tabs: navigation
123    { key = 'LeftArrow', mods = mod.CMD, action = act.ActivateTabRelative(-1) },
124    { key = 'RightArrow', mods = mod.CMD, action = act.ActivateTabRelative(1) },
125    { key = 'LeftArrow', mods = mod.CMD_REV, action = act.MoveTabRelative(-1) },
126    { key = 'RightArrow', mods = mod.CMD_REV, action = act.MoveTabRelative(1) },
127  
128    -- tabs: renaming
129    { -- TODO: still needs work: formatting, detection, hovering, etc...
130      key = 'r',
131      mods = mod.CMD,
132      action = act.PromptInputLine({
133        description = 'Enter new name for tab',
134        action = wezterm.action_callback(function(window, _, line)
135          if line then
136            window:active_tab():set_title(line)
137          end
138        end),
139      }),
140    },
141  
142    -- background controls --
143    {
144      key = [[,]],
145      mods = mod.CMD,
146      action = wezterm.action_callback(function(window, _)
147        backdrops:cycle_back(window)
148      end),
149    },
150    {
151      key = [[.]],
152      mods = mod.CMD,
153      action = wezterm.action_callback(function(window, _)
154        backdrops:cycle_forward(window)
155      end),
156    },
157    {
158      key = [[']],
159      mods = mod.CMD,
160      action = act.InputSelector({
161        title = 'Select Background',
162        choices = backdrops:choices(),
163        fuzzy = true,
164        fuzzy_description = 'Select Background: ',
165        action = wezterm.action_callback(function(window, _, idx)
166          ---@diagnostic disable-next-line: param-type-mismatch
167          backdrops:set_img(window, tonumber(idx))
168        end),
169      }),
170    },
171  
172    -- font --
173    -- font: resize with single stroke
174    { mods = mod.CMD, key = '+', action = act.IncreaseFontSize },
175    { mods = mod.CMD, key = '-', action = act.DecreaseFontSize },
176    -- font: resize interactively
177    -- Modes
178    {
179      mods = mod.LEADER,
180      key = 'e',
181      action = act.ActivateKeyTable({
182        name = 'execute_mode',
183        one_shot = false,
184      }),
185    },
186  
187    {
188      key = 'r',
189      mods = mod.LEADER,
190      action = act.ActivateKeyTable({
191        name = 'resize_font',
192        one_shot = false,
193        timemout_miliseconds = 10000,
194      }),
195    },
196  }
197  
198  local key_tables = {
199    resize_font = {
200      { key = 'k', action = act.IncreaseFontSize },
201      { key = 'j', action = act.DecreaseFontSize },
202      { key = 'r', action = act.ResetFontSize },
203      { key = 'Escape', action = 'PopKeyTable' },
204      { key = 'q', action = 'PopKeyTable' },
205    },
206    execute_mode = {
207      {
208        key = 'l',
209        action = act.EmitEvent('trigger-lazygit'),
210      },
211      -- Cancel the mode by pressing escape
212      { key = 'q', action = 'PopKeyTable' },
213      { key = 'Escape', action = 'PopKeyTable' },
214    },
215  }
216  
217  local mouse_bindings = {
218    -- Ctrl-click will open the link under the mouse cursor
219    {
220      event = { Up = { streak = 1, button = 'Left' } },
221      mods = mod.CTRL,
222      action = act.OpenLinkAtMouseCursor,
223    },
224  }
225  
226  return {
227    disable_default_key_bindings = true,
228    -- leader = { key = 'a', mods = mod.LEADER },
229    keys = keys,
230    key_tables = key_tables,
231    mouse_bindings = mouse_bindings,
232    send_composed_key_when_left_alt_is_pressed = keyboard.COMPOSED_KEYS,
233    send_composed_key_when_right_alt_is_pressed = keyboard.COMPOSED_KEYS,
234  }