25

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?

5
  • Perhaps How do I navigate to topics in Vim's documentation? is useful ;-) Mar 10, 2016 at 21:03
  • 1
    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, 2016 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 Mar 10, 2016 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.
    – user72
    Mar 11, 2016 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, 2016 at 19:29

5 Answers 5

16

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>.
2
  • 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, 2018 at 13:23
  • On my machine, Ctrl-Left seems to do something that :verbose map <C-Left> does not state. It states "no mapping found". I tried :help CTRL-<Left>, :help CTRL-Left to no avail. In the latter two cases, it says E149: Sorry, no help for CTRL---
    – Tryer
    Dec 8, 2022 at 6:21
16

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

3

One line answer from @Martin answer that is very helpful in most of the cases:

:help <key>

Note: This command will open the help for all standard keys but not for your custom commands

:help gg
3
  • 1
    Welcome to Vi and Vim! In most cases, we encourage answers to not repeat information contained in another without extending it somehow (e.g., providing new insight, etc.).
    – D. Ben Knoble
    Feb 3, 2021 at 15:57
  • Thank you @Ben. OK I'll keep in mind, in this I just found that this one line only would solve 90% cases. Now I think I should have written it as comment.
    – Gagan
    Feb 4, 2021 at 3:49
  • I think it's good practice to 'elevate' comments to answers on Stack Exchange sites generally, even if they don't extend that info at all. Aug 26, 2021 at 17:17
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!

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

Just type key or combination of keys in command line mode and press cntrl+d

1
  • 4
    Welcome to Vi and Vim! Your answer was automatically flagged for low-quality, likely to due to its length. The answer would benefit from an edit to describe how this works to answer the question. (Side note: while not really a "policy," most folks here prefer to spell out keys in one of the more standard way, so <C-d>, C-d, or <kbd>Control</kbd>-<kbd>d</kbd>)
    – D. Ben Knoble
    Mar 3, 2021 at 15:15

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.