How do I unmap only when a mapping exists in Vim

vim

Solution

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.

Problem

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)

Original source