0

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.

abra
  • 585
  • 1
  • 5
  • 12

3 Answers3

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

  2. Put this line in that file:

    nnoremap <buffer> ,pt <ESC>:%! perltidy<CR>
    
romainl
  • 186,200
  • 21
  • 280
  • 313
0

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.

slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123
0

I found the shorter way.

Add this line to your ~/.vimrc

au FileType perl nnoremap <buffer> _t <ESC>:%! perltidy<CR>
abra
  • 585
  • 1
  • 5
  • 12