14

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>?

3
  • 3
    note that setfiletype=gas is not even a valid syntax, it would be setfiletype gas
    – Mass
    May 28, 2018 at 16:30
  • yeah, definitely not valid XD
    – Mas Bagol
    May 30, 2018 at 20:29
  • @Mass Just edited the question to fix that.
    – filbranden
    May 17, 2020 at 14:44

1 Answer 1

15

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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