Vim tip: “polymorphic” key bindingsPosted: June 2, 2015
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 <leader>t :tabnew<cr>
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 <leader>t :tabnew<cr> vmap <leader>t :call Foo()<cr>
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 http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29.
Remember to check “:help map” for a list of all mode mappings.