Map to unused function keys in vim

One of the reasons that I used to use MacVim was that I could map to keyboard shortcuts like Ctrl-Shift-Enter and Command-Enter which didn’t work in the terminal. It is however easy to get it working in iTerm2 on OS X.

You can create such keyboard shortcuts by mapping them to an unused function key. In iTerm2 go to Preferences > Keys and press the little ‘+’ sign at the bottom of the dialog.

Hit the keyboard shortcut that you want to use and choose ‘Send Escape Sequence’ in the drop-down. In the input field that opens below enter the sequence you want to send.

Some of the different escape sequences that you can use can be seen bellow.

F13 = ^[O2P      F14 = ^[O2Q      F15 = ^[O2R      F16 = ^[O2S
F17 = ^[[15;2~ F18 = ^[[17;2~ F19 = ^[[18;2~ F20 = ^[[19;2~
F21 = ^[[20;2~ F22 = ^[[21;2~ F23 = ^[[23;2~ F24 = ^[[24;2~
F25 = ^[O5P F26 = ^[O5Q F27 = ^[O5R F28 = ^[O5S
F29 = ^[[15;5~ F30 = ^[[17;5~ F31 = ^[[18;5~ F32 = ^[[19;5~
F33 = ^[[20;5~ F34 = ^[[21;5~ F35 = ^[[23;5~ F36 = ^[[24;5~
F37 = ^[O6P F38 = ^[O6Q F39 = ^[O6R F40 = ^[O6S
F41 = ^[[15;6~ F42 = ^[[17;6~ F43 = ^[[18;6~ F44 = ^[[19;6~
F45 = ^[[20;6~ F46 = ^[[21;6~ F47 = ^[[23;6~ F48 = ^[[24;6~

Some might think that it’s bad to get keyboard shortcuts such as these into your muscle memory since they only work on your computer running your terminal. I agree to some degree but still do it. The examples below are for Ctrl-Enter, Ctrl-Shift-Enter and Shift-Enter.

“ Go to the end and add a semicolon
inoremap O2P <ESC>A;
“ Add without moving the cursor and leaving insert mode
inoremap O2R <ESC>maA;<ESC>`aa
“ Add a new line below current line and go to it
inoremap O2Q <ESC>o

The image in the header was created by Laurent Gregoire