Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if map exists in vimScript

Tags:

vim

I am trying to check if a mapping exists, and if true, unmap it. If I try to do it without checking it runs into error and exits. I guess I can also try to catch the error and add exception, but I can't find how to do it neither.

I tried:

if exists("map jjj")
    unmap jjj
endif

But it returns that map jjj exists (even if not true) because if I run in normal mode

:map jjj

it returns n j * gj. That is, it returns j map exists.

I don't have further ideas

like image 308
manolius Avatar asked Apr 18 '26 10:04

manolius


1 Answers

You can use maparg("jjj"), from the :help maparg:

maparg({name} [, {mode} [, {abbr} [, {dict}]]])         *maparg()*

        When {dict} is omitted or zero: Return the rhs of mapping
        {name} in mode {mode}.  The returned String has special
        characters translated like in the output of the ":map" command
        listing.


        When there is no mapping for {name}, an empty String is
        returned.  When the mapping for {name} is empty, then "<Nop>"
        is returned.

It will exactly match the {name} of the mapping.

like image 89
Sander Vanhove Avatar answered Apr 21 '26 02:04

Sander Vanhove



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!