iTerm2:如何为下拉菜单外的操作设置自定义快捷键?
Great question! When you want to replace iTerm2's default word-deletion shortcuts (and the Action menu doesn't list the exact "delete previous/next word" options directly), using hex codes tied to standard terminal control sequences is the way to go. Let's walk through how to find these codes and set them up.
Finding the Right Hex Codes for Your Actions
First, these word-deletion actions rely on standard terminal control sequences that most shells (like bash/zsh) recognize. You can either use the pre-known codes below, or find any sequence yourself using a simple terminal trick:
1. Pre-Known Hex Codes for Word Deletion
Here are the exact codes you need for your two desired actions:
- Delete cursor previous word: Two common sequences work here:
0x17(corresponds to the standardCtrl+Wshortcut, which most shells use to delete the word before the cursor)0x1b 0x7f(corresponds toAlt+Backspace, another widely used shortcut for this action)
- Delete cursor next word:
0x1b 0x64(corresponds toAlt+D, the standard shortcut for deleting the word after the cursor)
2. How to Look Up Any Shortcut's Hex Code
If you ever need to find the hex code for another terminal action, use the cat -v command:
- Open your terminal and run
cat -v - Press the shortcut you want to investigate (e.g., the default
Alt+Dfor deleting the next word) - You'll see a human-readable escape sequence (like
^[dforAlt+D) - Convert this sequence to hex:
^[is the ESC character, which is0x1b- Regular letters (like
d) use their ASCII hex value (0x64for 'd') - Control characters (like
^W) are calculated by subtracting 64 from the ASCII value of the uppercase letter:Wis ASCII 87, so 87-64=23, which is0x17
Setting Up the Custom Shortcuts in iTerm2
Once you have the hex codes, here's how to map them to your preferred shortcuts:
- Open iTerm2 → go to
Preferences→Keys - Click the
+button at the bottom to add a new shortcut - Press your desired custom shortcut (e.g.,
Ctrl+Option+Backspacefor deleting the previous word) - From the
Actiondropdown menu, select Send Hex Code - Paste the corresponding hex code into the input field (e.g.,
0x17for the previous word) - Click
OKto save the mapping - Repeat the process for the "delete next word" action with its hex code (
0x1b 0x64)
Are There Official Hex-Action Mappings?
iTerm2 doesn't have a dedicated public mapping table for these codes because they follow standard ANSI/POSIX terminal control sequences. Most common line-editing actions use sequences that are universally recognized by shells like bash, zsh, and fish. The cat -v method above is the most reliable way to look up any sequence you need.
内容的提问来源于stack exchange,提问作者samthegolden

