Set filetype for specific file?

14 views
Skip to first unread message

Ven Tadipatri

unread,
Sep 30, 2019, 1:44:31 PM9/30/19
to vim...@googlegroups.com
Is there a way to set the file type permanently for a specific file
(not based on extension)?
For example, if I have a file ~/dir/myfile.properties, I'd like to
always set the filetype to be sql, without having to run :set
filetype=sql every time I open the file.

Thanks,
Ven

Gary Johnson

unread,
Sep 30, 2019, 1:58:53 PM9/30/19
to vim...@googlegroups.com
The easiest way is to create a file in your ~/.vim/ftdetect
directory. I don't think the name matters except perhaps for ending
in .vim. Put this line in that file.

au BufRead,BufNewFile ~/dir/myfile.properties set filetype=sql

For more on this, see:

:help new-filetype

Regards,
Gary

Pete Doherty

unread,
Sep 30, 2019, 7:04:11 PM9/30/19
to vim_use
Have you tried something like the following?

`autocmd BufNewFile,BufRead ~/foo.bar set filetype=myfiletype`

Tony Mechelynck

unread,
Sep 30, 2019, 7:19:17 PM9/30/19
to Ven Tadipatri, vim_use
See :help modeline

For instance, add near the top or bottom of the file a line with either

-- vim: ft=sql
(modeline form 1, which goes to the end of the line)

or

/* vim: set ft=sql :*/
(modeline form 2, which can be followed by a colon and then anything,
such as a comment terminator)

as an SQL comment, and optionally with additional options to be set
for that file as with :setlocal. Anything is allowed before the "
vim:" modeline leader; usually a comment leader for the current
language is placed there.

Best regards,
Tony.
Reply all
Reply to author
Forward
0 new messages