How do I set up filetype and/or syntax for files that have no extension in vim?
Note
This is a duplicate of vim default syntax for files with no extension. I'm asking again because no one has answered it properly in my view.
I don't want to know how to set the default syntax for unrecognized file types, I want to know how to set it only for extensionless files.
You can create an autocommand that checks if the filename contains a .
, and if not, switches to a given syntax:
autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif
This one picks perl
as a default syntax, but you can simply use whichever is appropriate.
:help ftdetect
will show the part of the vim documentation about how to write filetype detection scripts.
Here's what I suggest for this case:
Create a file in ~/.vim/ftdetect
named after your filetype, e.g. myfiletype.vim
.
In this file put
au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=myfiletype | end
This will cause vim to set the filetype for files without any extension to myfiletype
. If you want it to only be used if no other filetype was detected use setfiletype myfiletype
instead of set ft=myfiletype
.
Then create the syntax file ~/.vim/syntax/myfiletype.vim
. This is just a normal vim syntax defintion file, nothing special. If you do not want to create your own filetype, just use the normal filetype in the autocommand instead of myfiletype. For example
au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=html | end
would set the html file type which would load the html syntax file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With