Vim tip: "polymorphic" key bindings

Post by Nico Brailovsky @ 2015-06-02 | Permalink | 1 comments | Leave a comment

If you use vim daily, you probably have a bunch of maps for your most common tasks. You should also remember you can assign the same key to do different things according to which mode you are in. For example, let's say you have a mapping to open a new tab:

map t :tabnew

You can also map t to open a new tab using the selected text as a filename. You just need to define two mappings:

nmap t :tabnew
vmap t :call Foo()

nmap stands for normal (mode) map, vmap for visual. How to get the text under the cursor is a bit more complex and out of scope for this vim tip, but you might want to check this link.

Remember to check ":help map" for a list of all mode mappings.


In reply to this post, Simple vim plugin III: a polymorphic project greper | An infinite monkey - Nicolas Brailovsky's blog commented @ 2016-11-30T08:02:07.000+01:00:

[…] grep to vim. We can improve it a little bit with very simple changes. Using this tip to have different key binding for different modes we can do something a bit smarter. Let's create two functions, one for normal mode that […]

Original published here.