Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find what filetype is loaded in vim

Tags:

vim

People also ask

How to detect filetype in vim?

Detecting Potion Files Open factorial.pn in Vim and run the following command: :set filetype? Vim will display filetype= because it doesn't know what a .

How do I change the filetype in vim?

As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type> where <type> is something like perl , html , php , etc. There is another mechanism that can be used to control syntax highlighting called filetype , or ft for short.

What is filetype off in vim?

But if you do switch off filetype detection, the indent files will not be loaded either. This actually loads the file "indoff.vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files.

What is vim Ftplugin?

A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample. c has file type c ), or from the file contents.


:set filetype?


&filetype for script usage

Minimal example:

echo &filetype

More realistic usage example:

if &filetype ==# 'c' || &filetype ==# 'cpp'
  setlocal noexpandtab
endif

& syntax works for all options: https://vi.stackexchange.com/questions/2569/how-do-i-check-the-value-of-a-vim-option-in-vimscript


You can also add the filetype to your status line or window title using the %y and %Y items. See

:help 'statusline'
:help 'titlestring'

When I wanted to find the filetype it was because I was having the .ipy extension modifying an ipython script. The above Q&A worked great to see that the ipython extension was not being treated as python (somewhat obvious). Thus I wanted to force set the filetype, and I found this from the ever helfpul vim site. http://vim.wikia.com/wiki/Forcing_Syntax_Coloring_for_files_with_odd_extensions

Adding the below to your .vimrc works

au BufRead,BufNewFile *.ipy set filetype=python

Since you are asking/searching this question in the first place, I assume you do this because your vim somehow does not highlight the proper syntax for you automatically, and you are trying to figure out why, and how to fix it.

The command :set filetype? in the chosen answer basically asks vim "hey what type do you think the current file is". And if vim is not displaying proper color/highlighting, most likely its answer to you is displaying this filetype= at the bottom of your console, meaning vim does not know the file type. This is only halfway of your quest.

Logically, your next step will be telling vim "just highlight it using (for example) ps1 syntax". You do so by typing :set filetype=ps1, now vim will highlight the current file using ps1 style (providing that you already download the proper powershell syntax file and put it into your ~/.vim/syntax).

At this point, you will probably want auto-syntax-highlight from now on. The solution is to put a corresponding detection script into your ~/.vim/ftdetect folder. For example, if you want to highlight all PowerShell scripts .ps1, .psm1, etc., you put this file into your ~/.vim/ftdetect

Now that is a happy ending.