# [[Emacs]] in [[Vim]]
## Steps
### [[iTerm2]] Config
Instructions in `init.vim`:
```
" ITERM2 MAPPING SETUP:
" - Remove conflicting iTerm2 profile mappings from ALL profiles:
" - Preferences > Profiles > Keys
" - Remove Option + Up/Down/Left/Right
" - Remove Shift + Up/Down/Left/Right
" - Add the iTerm2 key bindings
" - Prefs > Keys > Key Bindings 'Send Text with vim Special Chars'
" - <M-t><M-g><M-i><M-o> becomes \<M-t>\<M-g>\<M-i>\<M-o>
" - Import from maxfangx.itermkeymap in dotfiles to save time
" - See Obsidian notes: 'Emacs in Vim', 'iTerm2', `zsh`, `Oh My Zsh`
"
" Adding New Mappings:
" - iTerm2 maps macOS keys to meta keys not used by zsh or Oh My Zsh
" - Vim maps meta keys to corresponding functions
" - Since there are more shortcuts than available meta keys, the meta keys
" are combined in different permutations, achieving up to n! combinations
" - Available Meta+keys for permuting: gijknoptvyz
" - All other Meta+keys used in zsh, Oh My Zsh, or a vim plugin
" - Reserved for later: jknpz
" - Ctrl+z to undo, Ctrl+shift+z to redo?
" - Used: giot
" - Unused: vy
" - iTerm2 is also capable of mapping macOS keys to hex, octals, control
" keys, form feed, and others... see docs for more information:
" https://iterm2.com/documentation-preferences-profiles-keys.h
```
[[iTerm2]] Config:
![[iTerm2#Config]]
## Notes
### Meta key mappings in [[Vim]]
Used in insert mode `:imap`: `n, p, b, e`
Used in normal mode `:nmap`: `n, p`
### Meta key mappings in [[zsh]] / [[Oh My Zsh]]
- e.g. Can set to `\<M-key>` with `Send Text with "vim" Special Characters`
- Note - the key interferences appear to be coming from either [[zsh]] or [[Oh My Zsh]] - see maps with `bindkey -l` and show bindings with `bindkey -M <map>`
- See default [[Oh My Zsh]] key bindings: https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/key-bindings.zsh
Used:
- `a-d, h, l, m, q, r, s, u, w, x,`
Available: (should show a notification bell)
- `e` (but it's remapped in vim)
- `g, i, j, k, n, o, p, t, v, y, z`
## Relevant
### [[iTerm2]] Docs: Send Text with "vim" Special Characters
![[iTerm2#Docs Send Text with vim Special Characters]]
### List of [[Oh My Zsh]] key bindings
https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/key-bindings.zsh
### [[zsh]] bindings
![[zsh#Bindings]]
#### See current keymaps:
```bash
$ bindkey -l
.safe
command
emacs
isearch
listscroll
main
menuselect
vicmd
viins
viopp
visual
```
#### See bindings defined in a keymap:
```bash
bindkey -M <map>
```
## Components used in current solution
### Piece 1: vim-rsi
https://github.com/tpope/vim-rsi
- One letter modifications in insert mode
### Piece 2: Rebind using unused combinations
"Mapping Ctrl / Shift / Alt keys in any combination"
https://www.reddit.com/r/neovim/comments/73b3fc/how_to_map_ctrlshiftalt_keys_in_terminal_neovim/
Their solution: Rebind all the keys in Xresources using a prefix of F13-F18 keys as a namespace
- I could certainly do this using [[iTerm2]]-specific settings in [[Karabiner]]
### Last piece: [[iTerm2]] mappings
Thanks to [this answer](https://superuser.com/a/922022) which recommended setting key bindings in the terminal application