Contact

Dennis Kaarsemaker

Vim trick: detect filetype after typing #!shbang line

Last update on June 22, 2014.

Vim can detect syntax based on filename or on shbang lines, but if you're creating for example a new perl script and you don't want to use the .pl extension, vim will not automatically detect that after you type #!/usr/bin/perl. Instead, you manyally have to type :filetype detect. There must be a better way!

But of course there is a better way, there's vimscript!

function! RedetectFiletype()
    if getpos(".")[1] == 2 && getline(1) =~ '^#!'
        filetype detect
    endif
endfunction
inoremap <CR> <CR>:call RedetectFiletype()<CR>i<BS><CR>

So if you write a new shbang and hit enter, vim will take the hint and try to guess what kind of syntax you will be using.

Previous entry

Comments

Post your comment

Calendar

May 2014
SunMonTueWedThuFriSat
November 2013  
    123
45678910
11121314151617
18192021222324
25262728293031