10

I know that running :map commands without arguments should give me a list of user-defined mappings, but, for example, when I press Ctrl-W Ctrl-I something happens, yet I have no idea what function was just happened as built-in mappings don't appear in the :map commands.

How do I find out what a key does?

  • Perhaps How do I navigate to topics in Vim's documentation? is useful ;-) – Martin Tournoij Mar 10 '16 at 21:03
  • Not really. In emacs, I can go Ctrl-H k press the key I want to know about and it will give me the binding information. Vim seems to lack that function. How do I find out what a particular key combination is mapped to? – Mad Wombat Mar 10 '16 at 21:26
  • Did you read the marked duplicate? Because that tells you how to query the Vim help system for this information (list item no. 8): :help CTRL-W_CTRL-I or :help CTRL-W_i – Martin Tournoij Mar 10 '16 at 21:29
  • 2
    I voted to re-open because the post-edit form of the question has a usefully different answer (":map <Keys> to print active mappings") than what's in the currently-linked duplicate about help navigation. – Josh Mar 11 '16 at 16:50
  • 1
    But the OP seems to know that, it's his first phrase. Would be nice for Vim to have a way to show (in a similar manner) all the non-mapped keys, i.e. what they do by default. – VanLaser Mar 11 '16 at 19:29
8

You can't.

Here's the C source code for what <C-w><C-i> does:

normal.c:

/*
 * This table contains one entry for every Normal or Visual mode command.
 * The order doesn't matter, init_normal_cmds() will create a sorted index.
 * It is faster when all keys from zero to '~' are present.
 */
static const struct nv_cmd
{
    int         cmd_char;       /* (first) command character */
    nv_func_T   cmd_func;       /* function for this command */
    short_u     cmd_flags;      /* NV_ flags */
    short       cmd_arg;        /* value for ca.arg */
} nv_cmds[] =
{

[...]

    {Ctrl_W,    nv_window,      0,                      0},
};

[...]

/*
 * CTRL-W: Window commands
 */
    static void
nv_window(cmdarg_T *cap)
{
    if (!checkclearop(cap->oap))
        do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
}

window.c:

/*
 * all CTRL-W window commands are handled here, called from normal_cmd().
 */
    void
do_window(
[..]
) {
    switch (nchar)
    {
[..]
    case 'i':                       /* Go to any match */
    case Ctrl_I:
                type = FIND_ANY;
                /* FALLTHROUGH */

[..]
    default:    beep_flush();
                break;

I'm not entirely sure where the type variable gets picked up, I didn't feel like looking that up, but the point is, that there is no real facility to say "this key is mapped to this functionality". It's basically just a struct which maps a char to a function, and the implementation of the second keystroke (<C-i>) is simply ad-hoc ugliness!

This is different from Emacs, where (I presume) everything is in Lisp, and your custom mappings are not any different from Emacs' default mappings. But in Vim you have "magic" mappings that are provided by the C code which map against C functions that are never exposed in Vim. I always say that Vim has the better idea, but Emacs has the better implementation ;-)

So the only way is to look it up in the help files. This is why I closed this as a duplicate of How do I navigate to topics in Vim's documentation since that's the only practical answer anyone can give. One could indeed argue that a more in-depth explanation to the question could be given (as I have just done), and had you argued that, instead of giving the (now-deleted) response, I would probably have said "you're right, sorry for the mistake" and re-opened it ;-)

At any rate, to briefly illustrate the conventions with some examples:

  • :help w − normal mode mapping for w (case-sensitive);
  • :help g8 − normal mode mapping for g8;
  • :help v_o − visual mode mapping for o; other modes are c for command-line and i for insert;
  • :help CTRL-W − normal mode mapping for <C-w>;
  • :help i_CTRL-W − insert mode mapping for <C-w>;
  • :help CTRL-W_CTRL-I − normal mode mapping for <C-w><C-i>;
  • :help i_CTRL-G_<Down> − insert mode mapping for <C-g><Down>.
  • 1
    One important point about :help that seems always to be left out is what's called commandline completion in vim help using CTRL-D (I call it "search in help"). Type :h CTRL-W then press <CTRL-D>, and you'll get a list of all the commands containing the string "CTRL-W", e.g., CTRL-W, CTRL-W_^, CTRL-W-<Down>, .... Type :h g8<CTRL-D> and you'll see g8 and 8g8 as available help subjects. Without this, you have to type the help subject exactly and finding the exact string that works is difficult. – JESii Jul 23 '18 at 13:23
6

It's in the vim help files at :help index

If you want to know which keys aren't mapped by default, see :help map-which-keys

Note: the above are clickable links to the latest vim reference manual

2

I think you can get the mappings via the following commands

:nmap - Mapping on normal mode
:cmap - Mapping on command mode
:vmap - Mapping on visual mode

Hope it helps!

  • 2
    I don't think it will help because of the first few words of the first line of the question. – user859 Mar 23 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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