Is it safe to map a key to a custom escape sequence in iTerm?
Between these 2 pages (below) I figured it out:
- https://groups.google.com/forum/?fromgroups#!topic/iterm2-discuss/VEiCSc5LCIs
- http://vim.wikia.com/wiki/Mapping_fast_keycodes_in_terminal_Vim
You stay "safe" by using the key codes for unused Function keys. In iTerm2, I created "keyboard shortcuts" that map the desired key-combos to the escape sequences for Function keys F13 through F16. Then, in my vimrc, I set Vim's keycodes appropriately, mapped the desired key-combos to the right Function keys, then mapped the same key-combos to whatever I want. I'm not sure I fully understand the direction things are flowing in, but it all gets linked up and works. Here's the relevant portion of my vimrc:
" use some unused function key codes to
" make special key combos work in terminal
set <F13>=O2P
map <F13> <C-CR>
map! <F13> <C-CR>
set <F14>=O2Q
map <F14> <S-CR>
map! <F14> <S-CR>
set <F15>=O2R
map <F15> <C-Space>
map! <F15> <C-Space>
set <F16>=O2S
map <F16> <S-Space>
map! <F16> <S-Space>
" open above / below current line
inoremap <S-CR> <C-O>O
inoremap <C-CR> <C-O>o
" insert above / below current line
noremap <S-Space> mzO<Esc>j`z
noremap <C-Space> mzo<Esc>k`z