Monday, January 30, 2012

Re: Set 'filetype' by filename extensions

On Tue, Jan 31, 2012 at 15:32, Christian Brabandt <cblists@256bit.org> wrote:
On Tue, January 31, 2012 8:16 am, Clark J. Wang wrote:
> On Tue, Jan 31, 2012 at 15:11, Christian Brabandt
> <cblists@256bit.org>wrote:
>
>> On Tue, January 31, 2012 8:05 am, Clark J. Wang wrote:
>> > I have some files named in the *.kshlib format which are ksh scripts
>> but
>> > vim always recongnized them as "ft=conf". So how can I force vim to
>> > consider those files as "ft=sh"?
>> >
>> > I tried following in vimrc but it did not work:
>> >
>> > autocmd BufReadPost *
>> >         \ if bufname('%') =~ '^.*\.kshlib$' |
>> >         \   exe 'normal set ft=sh' |
>> >         \ endif
>> >
>>
>> :h new-filetype
>>
>
> Thanks I see the example "au BufRead,BufNewFile *.mine set filetype=mine"
> and that works for me. But what's wrong with my original post though it's
> a
> bit complicated?
>

It looks weird.

The whole point of the BufReadPost autocommand is to match
against a file name, in your case you use the wildchar * to match against
any file pattern, but within the autocommand, you only want to match
against .kshlib.

Secondly, your exe 'normal set ft=sh' is wrong, you missed the
colon and additionally you don't need neither the 'exe' nor the 'normal
command so this statement can be simplified to :set ft=sh.

Thanks.

After all your whole autocommand can then be simplified to
au BufReadPost *.kshlib :set ft=sh
which is basically the same autocommand, that is given below
:h new-filetype, except that you are still missing the BufNewFile
autocommand.

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: