How to detect the file type in vimrc?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to detect the file type in vimrc?

Albert Berger
Greetings!

How does one detect the file type in .vimrc? AFAIU, 'filetype' setting is
not yet set when .vimrc is processed. The following check doesn't
work in .vimrc:

if &filetype==cpp
...

I want to distinguish between 'c' and 'cpp' for *.h files in which
the file type is specified in the modline. Is there a function which
can be called for reading the modline? I know that one can use autocmd,
when the file type read from the modline is already set, but is there a
way to get the modline variables in .vimrc?

Thanks,
Albert.

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to detect the file type in vimrc?

Benjamin Fritz
On Tuesday, April 5, 2016 at 2:44:23 PM UTC-5, Albert Berger wrote:

> Greetings!
>
> How does one detect the file type in .vimrc? AFAIU, 'filetype' setting is
> not yet set when .vimrc is processed. The following check doesn't
> work in .vimrc:
>
> if &filetype==cpp
> ...
>
> I want to distinguish between 'c' and 'cpp' for *.h files in which
> the file type is specified in the modline. Is there a function which
> can be called for reading the modline? I know that one can use autocmd,
> when the file type read from the modline is already set, but is there a
> way to get the modline variables in .vimrc?
>
> Thanks,
> Albert.
No, you'll need to use an autocmd. In which case you may as well use a FileType autocmd. The modeline, like the filetype, has not been processed yet while reading the .vimrc.

Indeed, I'm pretty sure the file itself hasn't been read yet when reading the .vimrc, but I may be wrong about that.

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to detect the file type in vimrc?

Albert Berger
On Wed, Apr 06, 2016 at 10:21:42PM -0700, Ben Fritz wrote:

> On Tuesday, April 5, 2016 at 2:44:23 PM UTC-5, Albert Berger wrote:
> > Greetings!
> >
> > How does one detect the file type in .vimrc? AFAIU, 'filetype' setting is
> > not yet set when .vimrc is processed. The following check doesn't
> > work in .vimrc:
> >
> > if &filetype==cpp
> > ...
> >
> > I want to distinguish between 'c' and 'cpp' for *.h files in which
> > the file type is specified in the modline. Is there a function which
> > can be called for reading the modline? I know that one can use autocmd,
> > when the file type read from the modline is already set, but is there a
> > way to get the modline variables in .vimrc?
> >
> > Thanks,
> > Albert.
>
> No, you'll need to use an autocmd. In which case you may as well use a FileType autocmd. The modeline, like the filetype, has not been processed yet while reading the .vimrc.
>
> Indeed, I'm pretty sure the file itself hasn't been read yet when reading the .vimrc, but I may be wrong about that.
>

Looks like this is the case. Will use FileType autocmd. Thanks.


Albert.

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to detect the file type in vimrc?

Charles Campbell
Albert Berger wrote:

> On Wed, Apr 06, 2016 at 10:21:42PM -0700, Ben Fritz wrote:
>> On Tuesday, April 5, 2016 at 2:44:23 PM UTC-5, Albert Berger wrote:
>>> Greetings!
>>>
>>> How does one detect the file type in .vimrc? AFAIU, 'filetype' setting is
>>> not yet set when .vimrc is processed. The following check doesn't
>>> work in .vimrc:
>>>
>>> if &filetype==cpp
>>> ...
>>>
>>> I want to distinguish between 'c' and 'cpp' for *.h files in which
>>> the file type is specified in the modline. Is there a function which
>>> can be called for reading the modline? I know that one can use autocmd,
>>> when the file type read from the modline is already set, but is there a
>>> way to get the modline variables in .vimrc?
>>>
>>> Thanks,
>>> Albert.
>> No, you'll need to use an autocmd. In which case you may as well use a FileType autocmd. The modeline, like the filetype, has not been processed yet while reading the .vimrc.
>>
>> Indeed, I'm pretty sure the file itself hasn't been read yet when reading the .vimrc, but I may be wrong about that.
>>
> Looks like this is the case. Will use FileType autocmd. Thanks.
>
Alternatively you may use a ftplugin file:  ~/.vim/ftplugin/cpp.vim  and
put whatever you want in there.

Regards,
 C Campbell

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to detect the file type in vimrc?

Luc Hermitte
In reply to this post by Benjamin Fritz
Hi,

> Indeed, I'm pretty sure the file itself hasn't been read yet when
> reading the .vimrc, but I may be wrong about that.

Worse. When you open a second or a third file, the .vimrc isn't read anymore.

.vimrc settings are meant to be global settings that can apply to anything.

When we want dedicated settings, this is best done in ftplugins (when the variability is over the filetype). If it's over projects/directories, check for one of the local_vimrc plugins.
Otherwise, there are indeed autocommands, but they'll clutter the .vimrc with non global stuff.

--
Luc Hermitte

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to detect the file type in vimrc?

Albert Berger
In reply to this post by Charles Campbell
On Thu, Apr 07, 2016 at 11:46:03AM -0400, Charles Campbell wrote:

> Albert Berger wrote:
> > On Wed, Apr 06, 2016 at 10:21:42PM -0700, Ben Fritz wrote:
> >> On Tuesday, April 5, 2016 at 2:44:23 PM UTC-5, Albert Berger wrote:
> >>> Greetings!
> >>>
> >>> How does one detect the file type in .vimrc? AFAIU, 'filetype' setting is
> >>> not yet set when .vimrc is processed. The following check doesn't
> >>> work in .vimrc:
> >>>
> >>> if &filetype==cpp
> >>> ...
> >>>
> >>> I want to distinguish between 'c' and 'cpp' for *.h files in which
> >>> the file type is specified in the modline. Is there a function which
> >>> can be called for reading the modline? I know that one can use autocmd,
> >>> when the file type read from the modline is already set, but is there a
> >>> way to get the modline variables in .vimrc?
> >>>
> >>> Thanks,
> >>> Albert.
> >> No, you'll need to use an autocmd. In which case you may as well use a FileType autocmd. The modeline, like the filetype, has not been processed yet while reading the .vimrc.
> >>
> >> Indeed, I'm pretty sure the file itself hasn't been read yet when reading the .vimrc, but I may be wrong about that.
> >>
> > Looks like this is the case. Will use FileType autocmd. Thanks.
> >
> Alternatively you may use a ftplugin file:  ~/.vim/ftplugin/cpp.vim  and
> put whatever you want in there.
>
> Regards,
>  C Campbell
>
This is even more convenient. Thanks for the advise.


Regards,
Albert.

--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.