Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set vim filetype for files with no extension, only

Tags:

vim

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.

like image 932
pepper_chico Avatar asked Aug 09 '12 19:08

pepper_chico


2 Answers

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.

like image 138
Sebastian Paaske Tørholm Avatar answered Nov 15 '22 10:11

Sebastian Paaske Tørholm


: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.

like image 26
Geoff Reedy Avatar answered Nov 15 '22 08:11

Geoff Reedy