Automatically install multiple file types in `filetype` if the file has multiple extensions

I often develop Ruby on Rails. With the recent inclusion of Tilt in RoR 3, we have file extensions such as .scss.erb . How can I do filetype = scss.erb in this case automatically, and the same for each file with several extensions?

Edit: in this case there should be scss.eruby , since the erb extension is erb by default.

Edit: if this was not clear, I am looking for a way to make this work dynamically for all files with several extensions. For example, the file foo.js.html should be of the file type js.html .

Edit again: Prince Gulash's response does not accept the default file type for a specific extension.

+4
source share
1 answer

In your vimrc:

 autocmd BufRead,BufNewFile *.scss.erb setlocal filetype=scss.eruby 

(see :help ftdetect , section 2).

EDIT

To set the file type for four extensions, this works for me:

 autocmd BufRead,BufNewFile *.*.* \ sil exe "setlocal filetype=" . substitute(expand("%"),"^[^.]*\.","",1) 

The substitute command builds the file type by simply removing all text from the file name before the first . . Could be a more complicated way ...

CHANGE AGAIN

Here is another try. MultiExtensionFiletype() is a function that uses the default file type for the last part of the extension and prefixes it with the first part of the extension (i.e. the part placed between the dots).

 function MultiExtensionFiletype() let ft_default=&filetype let ft_prefix=substitute(matchstr(expand('%'),'\..\+\.'),'\.','','g') sil exe "set filetype=" . ft_prefix . "." . ft_default endfunction 

The function must be called in the BufReadPost event, so the original file type is set by ignoring multiple extensions.

 autocmd BufReadPost *.*.* call MultiExtensionFiletype() 

Hope this answer comes down to something useful!

+15
source

Source: https://habr.com/ru/post/1385082/


All Articles