Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see what keys vim thinks I'm hitting?

Tags:

vim

Is there any way to get a xev-like mode where I can hit keys and key combos and vim will print out what keys or characters it thinks I'm pressing?

Specific related problem: I have key bindings which work in MacVim and GVim but they don't work in a terminal-vim, which I use on Linux over SSH inside a screen. I've come to the conclusion that the reason is because vim thinks the keys I'm pressing are different from how MacVim interprets them.

In my .vimrc:

map <M-,> :split<CR> " Horizontal split
map <M-.> :vsplit<CR> " Vertical split
map <M-/> :close<CR>

In my vim's :map (MacVim shows the same):

¯             :close<CR><Space>
®             :vsplit<CR> " Vertical split
¬             :split<CR> " Horizontal split

It works in MacVim and GVim, but doesn't work in any terminal-based vim. I've tried this in multiple terminals (OSX Terminal and Term2, KDE Terminal, Gnome Terminal, etc.). I've also witnessed this with other modifiers and key combos. It seems like vim is capturing the keystrokes, but it's interpreting them as something other than <M-,> for example.

I'd love to have a way to find out what vim thinks I'm pressing so I can write mappings accordingly.

like image 896
shazow Avatar asked Apr 29 '12 18:04

shazow


1 Answers

  1. i to switch to insert mode.
  2. <C-v>.
  3. <key> or combo.
  4. Vim prints the "raw" value of the key/combo you have pressed.

Here (Gnome terminal on Ubuntu 11.04), typing i<C-v>, followed by <Alt>, followed by , prints something that looks like ^[, which means "Escape,". The Alt key (which I believe is what you want to use for <Meta>) is neither recognised as <Meta> nor as <Alt>.

The immediate conclusion is that CLI Vim doesn't like <M-> (and many terminal emulators don't deal very well with it anyway). Some terminal emulators allow you to map the <Alt> key to Meta but it's not a perfect cross-platform solution (I use <Alt> a lot on Mac OS X to input special characters).

On this machine,

nnoremap ^[, :split<CR> " ^[ is <C-v><Esc>

does exactly what I think you want, though.

If you absolutely want to continue using <M-> shortcuts both in the GUI and in the CLI you'll need to maintain two separate sets of shortcuts. If you have dozens of custom mappings in your .vimrc, it will quickly become a little hard to manage.

That's why I think you should use mappings that work everywhere like:

:nnoremap <leader>, :split<CR>
:nnoremap <leader>. :vsplit<CR>
:nnoremap <leader>/ :close<CR>

The default <leader> key is \ but I have set it to ,.

Note that you don't need to use <leader>key, simply mapping ,,, ,. and ,/ works, too.

See :help <leader>.

like image 74
romainl Avatar answered Oct 26 '22 01:10

romainl