简体   繁体   中英

How to enable map command in vim?

How to enable map command only when filetype is 'perl'?

something like:

if(&ft=='perl')
  map ,pt <ESC>:%! perltidy<CR>
endif

but it doesn't work.

  1. Create the file ~/.vim/after/ftplugin/perl.vim .

  2. Put this line in that file:

     nnoremap <buffer> ,pt <ESC>:%! perltidy<CR> 

I have below lines in my .vimrc . the first one sets the "tab" unit for any "perl" filetype and the second one runs perltidy with _t keyboard shortcut. THis many not get you the exact answer but surely should get you some pointers:

autocmd FileType perl set tabstop=4|set shiftwidth=4|set expandtab|set softtabstop=4
nnoremap <silent> _t :%!perltidy -q<Enter>
vnoremap <silent> _t :!perltidy -q<Enter>

I have been using these setting in my .vimrc for last 5+ years. The credit goes to whoever came up with these commands. I did not make above commands.

I found the shorter way.

Add this line to your ~/.vimrc

au FileType perl nnoremap <buffer> _t <ESC>:%! perltidy<CR>

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