Vim: How to get rid of filetype-specific map

keyboard shortcutsvim

Edit: Working version.

I've mapped [[ and ]] to my own functions:

nmap <silent> ]] :let &tabstop += 1 <CR> :echo 'tabstop =' &tabstop <CR>
nmap <silent> [[ :let &tabstop -= &tabstop > 1 ? 1 : 0 <CR> :echo 'tabstop =' &tabstop <CR>

However, according to :verbose map [[ these are overridden by /usr/share/vim/vim72/ftplugin/php.vim. How do I get rid of the PHP mappings while keeping my own? I've tried

autocmd FileType php unmap! [[
autocmd FileType php unmap! ]]

according to the tutorial, but then I get error messages at Vim startup:

Error detected while processing FileType Auto commands for "php":
E31: No such mapping
E31: No such mapping
Press ENTER or type command to continue

Using nnoremap instead of nmap doesn't help.

Putting the mappings in ~/.vim/after/ftplugin/php.vim also didn't work. vim -V shows:

...
finished sourcing /usr/share/vim/vim72/ftplugin/php.vim
...
finished sourcing /home/user/.vim/after/ftplugin/php/php.vim

So it does find the supposed override.

Combining nnoremap and the "after" script doesn't work either.

Best Answer

Specific definitions (filetype-specific, project-specific, ...) shall always be declared local to the current buffer. This is accomplished with the <buffer> qualifier for mappings and abbreviations (commands use -b, and menus need a specific plugin to emulate this, variables are prefixed by b:, and settings shall be set (sic) with :setlocal).

Moreover to declare it (i.e. a ft specific definition) local to the buffer, you can use autocommand as you seem to be doing, however I'd recommend instead to use ftplugins. It's much more maintenable, and all the rules regarding the overriding of the various definitions (mappings, etc.) are already taken into account.

" => ftplugin/php/php_my_stuff.vim
" headers guards...
...
:nnoremap <silent> <buffer> ]] :setlocal ts+=1<cr>:set ts?<cr>
:nnoremap <silent> <buffer> [[ :setlocal ts-=1<cr>:set ts?<cr>

PS: SO, or SU would have been a better fit for such a question.

Related Question