Set vim file type for files without extension, only

How to configure file type and / or syntax for files that do not have a vim extension?

Note

This is a duplicate of the default vim syntax for files without an extension . I ask again, because no one answered it correctly, in my opinion.

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 files without extension.

+5
source share
4 answers

You can create an autocommand that checks if the file name contains ., and if not, switches to the specified syntax:

autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif

perl , , .

+8
1) Hit escape to make sure you're in normal mode
2) Type ":set syntax=java" (or equivalent language)
3) :set filetype=FILETYPE, where FILETYPE is the filetype.

, :

autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif
+3

:help ftdetect vim , .

:

~/.vim/ftdetect, , . myfiletype.vim.

au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=myfiletype | end

, vim myfiletype. , , , setfiletype myfiletype set ft=myfiletype.

~/.vim/syntax/myfiletype.vim. vim, . , autocommand myfiletype.

au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=html | end

html, html.

+3

In the end, I chose this for my C ++ configuration:

let s:reserved = '^NERD_tree\|^GoToFile$'
au BufNewFile,BufRead *
\ if expand('%:e') =~ '^\(h\|hh\|hxx\|hpp\|ii\|ixx\|ipp\|inl\|txx\|tpp\|tpl\|cc\|cxx\|cpp\)$' ||
\    expand('%:t') !~ '\.\|'.s:reserved && expand('%:t') =~ '[a-z]'                            |
\   if &ft != 'cpp'                                                                            |
\     set ft=cpp                                                                               |
\   endif                                                                                      |
\   set syntax=cpp11                                                                           |
\   call CSyntaxAfter()                                                                        |
\ endif

Instead of checking for a missing .tail. This avoids the fact that several cpp files will not be installed as the COMMIT_EDITMSG, README, GoToFilefrom CommandT and NERD_tree*from NERD tree.

EDIT

I just gave up this idea.

0
source

All Articles