Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unmap only when a mapping exists in Vim

Tags:

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)

like image 303
Paul Carey Avatar asked Apr 25 '13 14:04

Paul Carey


1 Answers

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.

like image 105
Ingo Karkat Avatar answered Oct 26 '22 13:10

Ingo Karkat