user826955
user826955

Reputation: 3206

How can I get the terminal key code in vim

I have an existing .vimrc which was created on macintosh computer and contains mappings like this:

"------------------------------------------------------------
" keyboard shortcuts
"------------------------------------------------------------

nmap <S-Tab> :wincmd W<CR>
nmap <S-q> :bp\|bd #<CR>
nmap « :bp\|bd #<CR>
nmap ¢ :sp<CR>
nmap ¶ :vsp\|wincmd w<CR>
nmap :Q :qa<CR>
nmap :W :wqa<CR>
nmap ≠ :q<CR>
nmap ESC[1;9B 15j
nmap ESC[1;9A 15k
nmap ^? :w<CR>
nmap ø :edit
nmap ESC[1;5B 15j
nmap ESC[1;5A 15k
vmap ESC[1;5B 15j
vmap ESC[1;5A 15k
nmap ^\ :edit
nmap ^] :wincmd w<CR>
nmap <F9> :w<CR>:Make<CR><CR>
imap <F9> <ESC>:w<CR>:Make<CR>
nmap <C-n> :cnext<CR>
nmap <C-p> :cprev<CR>
nmap <F5> :Grep
nmap @ g~w w
nmap <F10> :ConqueGdb
nmap ^A :sav %:h/filename
nmap ESC[1;2D :NERDTreeToggle<CR>
nmap ESC[1;2B :sp\|wincmd w<CR>
nmap ESC[1;2C :vsp\|wincmd w<CR>
nmap ESC[1;5D :q<CR>
nmap ESC[1;5F :BuffergatorToggle<CR>

nmap ESC[1;6D w
nmap ESC[1;6B b

Now I have transferred this .vimrc to a new linux computer (debian-based), but half of the shortcuts don't work anymore. I guess thats because the mac interpretes some of the keystrokes differently as compared to the linux computer (both computers do have vim v8.1).

When creating shortcuts, I remember that there was a function in vim which would print out the key codes when you press the keys, however that was about 10 years ago and I don't remember it, and I can't find via google.
To be precise, it was like "press XYZ in vim, then press e.g. CTRL+S, then vim would print the corresponding key code to be used in the mappings".

How can I do this now, in order to make those shortcuts work on linux also?

Upvotes: 0

Views: 2320

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172520

You're looking for :help i_CTRL-V:

For special keys, the terminal code is inserted.

For example, in my Gnome Terminal pressing <C-V><Right> gives ^[OC and <C-V><F12> gives ^[[24~.

In general, you should prefer Vim's key-notation whenever possible. If you do need to insert specific escape codes and need to support multiple platforms with a single ~/.vimrc, you can wrap the mapping definitions in conditions like if has('mac') or if $TERM ==# 'screen256-color'.

Upvotes: 3

Related Questions