Search code examples
vimvim-plugin

Vimscript - Is it possible to overload mappings?


Not to remap but keep the existing mapping as it is but add additional functionality. For example overload hjkl keys do something extra in additional to moving the cursor.


Solution

  • hjkl are not mappings; they are built-in normal mode commands.

    If you want to override them you will need to create custom mappings. For example this mapping will center the current line each time you press j (this is pretty much useless but that will work as an example):

    nmap j jzz
    

    Note that j is now completely overridden for normal mode. The only way to use the original j is now either to remove that mapping definitively:

    unmap j
    

    or use the original j temporarily via the :normal command:

    :normal! j
    

    Alternatively, you could create a completely separate mapping like:

    nmap <Space>j jzz
    

    and have the regular j and your customized j (<Space>j in this case) available at all time. Respecting the built-in behavior while still extending it.