简体   繁体   中英

VIM: FileType specific mapping not working when defined in ftplugin

I am trying to set a mapping for FileType perl . The mapping is for the case when I forgot to use semicolon at the end of the line.

So first I tried adding in my .vimrc autocmd! FileType perl nnoremap <leader>; $a;<esc> autocmd! FileType perl nnoremap <leader>; $a;<esc> autocmd! FileType perl nnoremap <leader>; $a;<esc> and it worked fine but than I thought of using ftlugin/perl.vim .

So I added the below line in my corresponding ~/.vim/after/ftplugin/perl.vim

nnoremap <buffer> <leader>; $a;<esc>

but it didn't work.

Any idea why it is not working ?

My perl version is perl 5, version 14 .

Try putting the file in ~/.vim/ftplugin/perl.vim instead of ~/.vim/after/ftplugin/perl.vim . From :help after-directory :

  *after-directory* 4. In the "after" directory in the system-wide Vim directory. This is for the system administrator to overrule or add to the distributed defaults (rarely needed) 5. In the "after" directory in your home directory. This is for personal preferences to overrule or add to the distributed defaults or system-wide settings (rarely needed). 

From :help ftplugin :

If you do want to use the default plugin, but overrule one of the settings, you can write the different setting in a script: >

setlocal textwidth=70

Now write this in the "after" directory, so that it gets sourced after the distributed "vim.vim" ftplugin |after-directory|. For Unix this would be "~/.vim/after/ftplugin/vim.vim". Note that the default plugin will have set "b:did_ftplugin", but it is ignored here.

One thing I just noticed that was driving me crazy: <buffer> must be lowercase! Out of habit, I uppercase all of my <BRACKET> prefixes... and I had done the same for <BUFFER> . None of my ftplugin mappings worked and I couldn't figure it out... until I wasted hours trying different things, only to find that it must be lowercase <buffer> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM