manolius
manolius

Reputation: 395

check if map exists in vimScript

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

Upvotes: 3

Views: 769

Answers (1)

Sander Vanhove
Sander Vanhove

Reputation: 1115

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.

Upvotes: 8

Related Questions