2

Is there a tool or plugin for a learner which can display the keyboard shortcuts or abbreviated versions for commands after they are typed in?

If for instance I use : to get the command line and execute :tabnew the plugin should display whatever keyboard shortcut that is bound to that command for a few seconds or until the next command is typed in.

Take this question - https://stackoverflow.com/questions/6467634, the author wants to set a shortcut for the NERDTree command. I am not interested in using the shortcut itself. I want to type :NERDTree itself and get a notification that ,ne has been mapped to it. I don't want to break my workflow to look up the shortcut, ie I don't want to lookup the key mapping before hand, just to know it and use it if I find it convenient.

Assuming I am sitting at someone else desktop with their own customizations, to get a job done. It is not really practical for me to lookup their shortcuts when I know the full commands. I just use the full command and if the notification shows me that their customization is convenient for me then I use it.

It should work whether they are built in defaults, set by plugins or are user configured, or even if the commands have multiple alternatives.

This question is about a plugin which displays keyboard equivalents of commands typed in the command line, in realtime. It is meant to show the keyboard shortcut for a command after it has been used. It is not to lookup the keymapping for that command.

  • What do you mean by, "whatever keyboard shortcut that is bound?" – Tumbler41 Apr 26 '17 at 20:15
  • It means a set of keystrokes that implements the same action as the command typed on the command line – vfclists Apr 26 '17 at 20:55
  • This is the question I wanted to ask but asked it incorrectly in that question. It would be unfair to delete or cancel. The answer was correct based on what I wroite, that is why I ma – vfclists Apr 26 '17 at 21:33
  • @HerbWolf,statox I have amended the question to explain the difference, and I think you should read the question again. it is not about finding out what key mappings exist. It is to know what shortcuts exist for commands after they are used. I have highlighted the relevant differences. In bold. I failed to ask the question correctly the first time and I felt it would would be wrong to change it after it had been already answered but not as I intended. That is why I asking about a plugin because it is not something I expect to be built in. – vfclists Apr 27 '17 at 9:04
  • 1
    I voted to reopen. Whilst the questions are related, I wouldn't expect any of the answers to the linked question to answer this more specific question. (And the current answer to the linked question definitely doesn't.) – Rich Apr 27 '17 at 12:31
3

I came up with with a quick solution. It needs some improvements but you could take it as a base.

So your problem can be divided in two mains problems:

  • How can I get the mappings associated to a command?
  • How can I execute a function after each command I use?

To start with the second one we can use a quick workaround: We can simply remap <CR> in command mode, this way we can make it call a function each type we press it to execute our command.

This probably has some huge drawbacks that I don't know so far.

cnoremap <CR> <CR>:call ShowMappings()<CR>

With this code we say to vim "Call the function ShowMappings() each time <CR> is pressed".

Since we want to use the last executed command in our function, we can use getreg() to get the content of the : register which contains our command.

The function ShowMappings() will look like this:

function! ShowMappings()
    let cmd = getreg(':')

    echo "Mappings for " . cmd

endfunction

Now the part where we want to get the mappings associated to our command: The map command gives all of the available mappings. The filter command allows us to filter the output of another command. So this code will show all of the mappings including the command CtrlP:

filter /CtrlP/ map

Now all we need to do is to use it in our function:

function! ShowMappings()
    let cmd = getreg(':')

    echo "Mappings for " . cmd

    execute 'filter /' . cmd . '/ map'
endfunction

If you put everything together you get the following code:

function! ShowMappings()
    let cmd = getreg(':')

    echo "Mappings for " . cmd

    execute 'filter /' . cmd . '/ map'
endfunction

cnoremap <CR> <CR>:call ShowMappings()<CR>

Don't hesitate to use it and improve it to make your own plugin. If I get some time I'll try to make one.

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.