Take the 2-minute tour ×

How do I get the current filetype in a vimscript, say in .vimrc or a regular plugin script? The line

echo &filetype

returns an empty string.

share|improve this question
Show us where you use it. In .vimrc, no buffers are loaded yet, so it's empty. –  Ingo Karkat Mar 1 '13 at 10:54
I need it in .vim/bundles/vim-autoformat/plugin/autoformat.vim. –  Chiel92 Mar 1 '13 at 11:37
While sourcing (same as with .vimrc, it's too early), or inside a function / mapping / command?! –  Ingo Karkat Mar 1 '13 at 11:57
While sourcing indeed: let s:formatprgvarname = "g:formatprg_".&filetype –  Chiel92 Mar 1 '13 at 12:13
add comment

2 Answers

up vote 2 down vote accepted

You need to consider the timing of your plugin. When your plugin script is sourced during the startup of Vim, no buffer has yet been loaded, so &filetype is empty. Therefore, something like this

let s:formatprgvarname = "g:formatprg_".&filetype

does not work! (For a filetype plugin (in ~/.vim/ftplugin/), this is different; those are sourced only when the filetype has been detected. But as I understand you want a general-purpose plugin that considers the current filetype.)

Instead, do away with the script-local variable s:formatprgvarname and resolve &filetype at the point of action; i.e. when your autoformat functionality is triggered (by mapping or custom command). If you have no such trigger, you can hook into the FileType event and set a (preferably buffer-local) variable then:

autocmd FileType * let b:formatprgvarname = "g:formatprg_".&filetype
share|improve this answer
Is the FileType event also triggered when switching to a buffer with a different filetype? –  Chiel92 Mar 1 '13 at 12:32
No, only when it is initially detected. –  Ingo Karkat Mar 1 '13 at 12:45
add comment

In a function:

let current_filetype = &filetype

On the command line:

:echo &filetype

&filetype is empty when there's no filetype (obviously). If you try to use it when there's no buffer loaded you'll just get nothing.


&filetype is only useful when you need to know the current filetype in a function executed at runtime when you are editing a buffer.

&filetype is only set when a buffer has been determined to be of some filetype. When it is executed, a script (vimrc, ftplugin, whatever) doesn't go through what it would go when it is edited: no filetype checking, no &filetype.

An example would be a function that displays the current file in a different external app depending on its filetype. Using &filetype in any other context doesn't make sense because it will be empty.

share|improve this answer
&filetype appears to be empty in the pluginscript for all filetypes like vim and python. (However, in-vim running :echo &filetype yields the correct filetype.) –  Chiel92 Mar 1 '13 at 10:44
&filetype works at runtime, not in your script. You use &filetype in a function that is executed at runtime. –  romainl Mar 1 '13 at 10:56
@Chiel92, see my edit. –  romainl Mar 1 '13 at 11:06
Thank you for your explanation. Is there a way to get hold of it in my script? If not, I will have to change the design. –  Chiel92 Mar 1 '13 at 11:45
Did you read my answer? There's no &filetype when a *.vim script is evaluated because it simply is not relevant at that time. The only situation where you might want to know the filetype of a buffer is when you run a function triggered by an autocmd or a :Command or a mapping. In such a situation, just use the first snippet in my answer. If you want more help you'll have to explain what you are after and possibly show some code. –  romainl Mar 1 '13 at 12:59
add comment

Your Answer


