2

I am using vim 8.1. When I need to change the default filetype for .s extension to gas (which by default is asm), I first look for the default command to set filetype in /usr/share/vim/vim81/filetype.vim. It contains this line:

au BufNewFile,BufRead *.asm,*.[sS],*.[aA],*.mac,*.lst   call dist#ft#FTasm()

Then, I tried to overwrite that in my vimrc with this:

autocmd BufNewFile,BufRead *.s setfiletype=gas

But, it doesn't work. Filetype still asm when I open any *.s file.

Then, when I use this:

autocmd BufNewFile,BufRead *.s set ft=gas

It works.

So, what is the difference between set ft=<filetype> and setfiletype=<filetype>?

  • note that setfiletype=gas is not even a valid syntax, it would be setfiletype gas – Mass May 28 '18 at 16:30
  • yeah, definitely not valid XD – Mas Bagol May 30 '18 at 20:29
4

From the :setfiletype help:

:setf[iletype] [FALLBACK] {filetype}                    :setf :setfiletype
                     Set the 'filetype' option to {filetype}, but only if
                     not done yet in a sequence of (nested) autocommands.
                     This is short for:
                             :if !did_filetype()
                             :  setlocal filetype={filetype}
                             :endif
                     This command is used in a filetype.vim file to avoid
                     setting the 'filetype' option twice, causing different
                     settings and syntax files to be loaded.

                     When the optional FALLBACK argument is present, a
                     later :setfiletype command will override the
                     'filetype'.  This is to used for filetype detections
                     that are just a guess.  did_filetype() will return
                     false after this command.

So :setfiletype does not override existing file type, when :set ft does.

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.