5

I would like to define some mappings involving key combinations like <S-F1> or <A-d>.
For example something like this:

nnoremap <A-d> :echo "hello world!"<cr>

However, on my system, it doesn't work. I'm using guake as a terminal emulator and tmux as a terminal multiplexer.

I found a solution here. Basically the person says that you must do the following:

  • launch the utility cat inside your shell
  • hit your key combination (e.g. <A-d>), and read the output (e.g. ^[d)
  • add in your vimrc set {key combination}={cat output} (e.g. set <A-d>=^[d)
    The trick being not to type ^[ litteraly, but <C-v><escape> to insert a litteral escape character (whose caret notation is ^[)
  • finally define your mapping as usual

It works but I'm not sure why the first three steps are required on my system and what they do.

Here's my understanding of what happens when I hit a key on a keyboard.
A keycode is sent to the kernel (which can be read with the xev utility).
The kernel sends this keycode to the X server which uses a table to translate it into a keysym (a key symbol like A, aacute, F1...).
The keysym is sent to the gui program which has the focus.

So, when I hit <A-d>, the kernel receives 2 keycodes (64 for Alt_L and 40 for d) which are translated into 2 keysyms by the X server (0xffe9, Alt_L and 0x64, d). The combination of keysyms Alt_L and d is sent to guake, which sends it to tmux, which sends it to vim.

Inside the tmux configuration file I could map <A-d> to make it perform some action. So tmux understands this combination of keysyms. Why does vim not understand it like tmux ?

Why do I need to put set <A-d>=^[d in my vimrc ? And what's ^[d ? An escape sequence ?

I found 2 paragraphs in the help :help :set-termcap which treat this question, but I still don't understand. They talk about something called termcap. I read the wikipedia page, and basically what I understood is that termcap is a library as well as a database containing information for hundreds of terminals. It allows programs (like the shell or vim) to know how to use a terminal to display some text. Like for example:

  • how many screen columns are available
  • which string to send to move the cursor to an arbitrary position
  • how to scroll the screen

According to the help an instruction like set <A-d>=^[d allows you to override some value stored in the termcap database. Which value was stored in termcap, and why is it necessary to override it specifically for vim ?

3
  • 1
    To find out what Vim receives, I would skip the cat thing and get Vim to tell you directly: :set <A-d>=CTRL-V ALT-D You can then yank-paste the line from q: Jan 21, 2016 at 6:20
  • 1
    Curiously, Vim shows me ^[OD but cat shows me ^[[D, but both seem to work when using :set. Jan 21, 2016 at 6:21
  • @joeytwiddle In xfce4-terminal as well as guake, cat shows me ^[d. Vim shows me the same. But I think you're completely right, I should use Vim instead of cat to get a reliable information, depending on the terminal emulator and the Vim version there could be some discrepancies. Thanks for the advice !
    – saginaw
    Jan 21, 2016 at 10:26

1 Answer 1

2

Vim doesn't receive Alt_L+d. It receives exactly what you obtained in cat when you typed <A-d>: ^]d, where ^] is a literal <Esc>. Yes, Vim sees <Esc>d when you press <A-d>.

Basically, Vim is unable to recognize the Alt key as such, so you have to teach it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.