0

I am editing a source code in language1.

It doesn't have a built-in syntax highliting configuration into my vim.

However, its syntax is very similar to language2, which has. I think, syntax highliting the sources as language2 would be pretty okay for me. Thus, I tried to set up the syntax highliting of language2 to the files with extensions of language1.

Checking the syntax highliting configuration of my distribution-provided vim (filetype.vim), I found the line

au BufNewFile,BufRead *.<ext2>  setf language2

Thus, I inserted a line

au BufNewFile,BufRead *.<ext1>  setf language2

into my local .vimrc.

However, nothing happened. My source files - in language1 - are still un-syntax highlited.

Why is it so? How could I make it working?

  • In my specific case: I am trying to syntax hightlig Typescript files (.ts) as if they would be Javascript, on a vim provided by debian/ubuntu. This is why I am trying to avoid to overwrite the distro-provided /usr/share/vim/vim74/filetype.vim. However, I tried to make the question as general as possible (I think not the question and also not the answer won't be specific to my environment). – peterh Feb 18 '18 at 22:55
  • If I overwrite /usr/share/vim/vim74/filetype.vim, then it works, but I am trying to use a local config, as overwriting distro-provided file is a bad thing on debian. – peterh Feb 18 '18 at 22:57
  • Does set filetype=language2 work (i.e., not using setf)? – Mass Feb 18 '18 at 23:23
  • @Mass Wow! Yes, it works. But why the au command didn't work in my local vimrc? – peterh Feb 18 '18 at 23:31
  • Did you try au BufNewFile,BufRead *.<ext1> set filetype=language2 in your vimrc already? The reason set filetype works and setf doesn't is because vim already knows .ts as having filetype=xml. setf will not change the filetype if it's already set (I will add an answer if the code works in your vimrc). – Mass Feb 18 '18 at 23:34
2

A quick solution is to just replace setf with set filetype in the line in your vimrc:

au BufNewFile,BufRead *.<ext1>  set filetype=language2

This is explained in :help ftdetect but it is quite difficult understand. In this specific case the reason is very simple- that vim has already used setf to set a file type for ext1.

Suppose ext1=ts. In /usr/share/vim/vim80/filetype.vim, we see,

" Qt Linguist translation source and Qt User Interface Files are XML
au BufNewFile,BufRead *.ts,*.ui         setf xml

This means any following setf issued in autocmds will fail:

:setf[iletype] [FALLBACK] {filetype}            *:setf* *:setfiletype*
        Set the 'filetype' option to {filetype}, but only if
        not done yet in a sequence of (nested) autocommands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.