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 ?
cat
thing and get Vim to tell you directly::set <A-d>=CTRL-V ALT-D
You can then yank-paste the line fromq:
^[OD
but cat shows me^[[D
, but both seem to work when using:set
.cat
shows me^[d
. Vim shows me the same. But I think you're completely right, I should use Vim instead ofcat
to get a reliable information, depending on the terminal emulator and the Vim version there could be some discrepancies. Thanks for the advice !