Here’s a quick way to create iterm2 key mappings for use in fish shell
I wanted to map ⌥ + Left and ⌥ + Right to go backward and forward a word, respectively. I also wanted to have Option+Delete kill a word.
The main idea is that you need to have those key combinations generate unique control sequences that you manually bind to in your fish configuration: ~/.config/fish/config.fish
Here’s the setup in iterm2: Preferences -> Profiles -> Keys
Key combination: ⌥ + Delete
Action: Send Escape Sequence: [dw
I just totally made up [dw to symbolize delete word. The option square bracket is similar to the pre-defined sequence for ⌥ + Left and ⌥ + Right; so I just went with it.
By default, iterm2 prints out some garbage key sequence to the terminal when you do ⌥ + Left and ⌥ + Right, so I just manually bound those in fish (will show you how): [1;9D and [1;9C, respectively.
Here’s how to manually bind the keys in your fish config – add this function definition:
function fish_user_key_bindings bind e[1;9C forward-word bind e[1;9D backward-word bind e[dw backward-kill-wordend
Notice that the square bracket and the semicolon had to be escaped. We can use fish’s built in commands (ex: backward-word) to finish the binding. Here’s a list of the default fish bindings. The ‘e’ is an ascii control sequence for ‘escape’ which works well for us since we told iterm2 to output an escape sequence for deleting a word with ⌥ + Delete.
Restart your fish shell (‘exit’ then ‘fish’) and you can skip through words like a pro.