Paul Carey
Paul Carey

Reputation: 1798

How do I unmap only when a mapping exists in Vim

As a follow-on to the accepted answer in How do I override gf with vim-rails, editing the same file multiple times results in the following error:

Error detected while processing User Auto commands for "Rails.javascript.coffee*":
E31 : No such mapping

The offending line in my vimrc is

:autocmd User Rails.javascript.coffee* nunmap <buffer> gf

How can I avoid this error by unmapping only if the mapping already exists?

(Note: Editing the same file multiple times may seem like a strange thing to do, but I believe it happens as a side effect of searching with ack.vim or ag.vim)

Upvotes: 28

Views: 3677

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172530

You could check for the mapping with if ! empty(maparg('gf', 'n')) ..., but the usual solution is to just suppress the error by prepending silent! before the nunmap <buffer> gf command.

Upvotes: 44

Related Questions