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.


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.

  • This solution actually worked! Just added <buffer> to the mapping lines in ˜/.vim/after/ftplugin/php.vim and that was it. – l0b0 Jul 2 '12 at 11:14
  • Mine has <buffer>, but if I look at :scriptnames, The file /usr/share/vim/vim80/ftplugin/sql.vim comes right after my ftplugin file and overwrites my mappings. Any way to make mine not get overwritten? – Jay K Jan 25 '17 at 23:33
  • @JayK Set b:did_filetype, IIRC, to prevent the default ftplugin from being loaded. – Luc Hermitte Jan 25 '17 at 23:48
  • @LucHermitte Actually, I found that putting the file in .vim/after/ftplugin/ did the trick for me. From unix.stackexchange.com/a/41749/159250 – Jay K Jan 26 '17 at 15:46

I'd try to use hints for overruling the default plugin checks described here. To sum it up: drop your mappings plugin file late in the runpath (e.g. ~/.vim/after/ftplugin/php.vim) to override the global mappings.

  • That didn't work; please see the updated question. – l0b0 Jun 28 '12 at 7:54

did you try to use nnoremap instead of nmap in your .vimrc

nnoremap <silent> ]] :let &tabstop += 1 <CR> :echo 'tabstop =' &tabstop <CR>
nnoremap <silent> [[ :let &tabstop -= &tabstop > 1 ? 1 : 0 <CR> :echo 'tabstop =' &tabstop <CR>
  • Doesn't work. It still uses the PHP shortcuts. – l0b0 Jun 28 '12 at 7:19
  • hmm - did you try to do it manually after the plugin was loaded. – epsilonhalbe Jun 28 '12 at 9:01
  • Yep, tried copying them after : in a Vim session, and no change. – l0b0 Jun 28 '12 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.