Mapping key sequences --------------------- .. |end_cursor| replace:: :ref:`end_cursor` .. |imap| replace:: :py:obj:`imap` .. |MappingInfo| replace:: :py:obj:`MappingInfo` .. |map| replace:: :py:obj:`map` .. |nmap| replace:: :py:obj:`nmap` .. |omap| replace:: :py:obj:`omap` .. |start_cursor| replace:: :ref:`start_cursor` .. |vmode| replace:: :ref:`vmode` .. |xmap| replace:: :py:obj:`xmap` VPE provides the following mode specific functions that can be used to defined key mappings. .. hlist:: :columns: 4 - |imap| - |nmap| - |omap| - |xmap| These roughly correspond to the vim commands of the same name. Vim's other mapping commands are not supported because I have not come across any good reasons to use them within Python based scripts. Apart from creating mappings for different Vim modes, there is little difference between them. So most of the following discussion uses |nmap|. Key mappings are typically set up to invoke Python functions or methods. .. code-block:: python from vpe import mapping def on_key(info: mapping.MappingInfo): vpe.echo_msg(f'Key sequence {info.keys} was pressed', soon=True) mapping.nmap(keys='g', func=on_key) .. sidebar:: Argument soon=True Notice that the call to vpe.echo_msg uses the argument ``soon=True``. See :ref:`using_call_soon` to find out why. After the above is executed, a mapping will be set up for the *current* buffer such that the key sequence 'g', '' (in normal mode) will cause the message 'Key sequence g was pressed' to be displayed. Notice that the callback function ``on_key`` receives a |MappingInfo| object. This provides details about how and why the callback was invoked. This makes it easy to handle multiply key mappings in multiple modes, using just one function; which can simplify code in some circumstances. The information contained in the |MappingInfo| object is: .. _info_attrs: mode The mode in which the mapping was triggered (normal, visual, op-pending or insert). keys The sequence of keys that triggered the mapping. vmode The visual mode (character, line or block). Will be ``None`` when not applicable. start_cursor When mode=="visual", a tuple (line, column) of the selection start. Both values are 1-based. Will be (-1, -1) when not applicable. end_cursor When mode=="visual", a tuple (line, column) of the selection end. Both values are 1-based. Will be (-1, -1) when not applicable. Defaults and limitations ~~~~~~~~~~~~~~~~~~~~~~~~ The mapping functions impose some limitations and non-obvious defaults. The actual mapping command generated for the above example helps illustrate this: .. code-block:: vim :nnoremap g \ :silent call VPE_Call("154", "on_key") The other mapping functions produce broadly similar code; i.e. they typically invoke ``VPE_Call``. - The 'nnoremap' command is used. VPE always uses the noremap form to set up key mappings. No support for nested/recursive mapping is provided. I have not found this restriction to be a problem. - The option is always used. This helps avoid surprises. - The and options are used by default. These are chosen as suitable defaults for most scripting (admittedly based on my experience). These can be over-ridden using the buffer and silent keyword arguments: .. code-block:: python mapping.nmap(keys='g', func=on_key, buffer=False, silent=False) - The