I'm going to try explaining in a different way. Vim is an editor that knows how to execute Ex commands, including :set
.
It has a startup sequence that includes executing commands from certain files (mostly before any buffers are loaded and windows are opened).
It also has a filetype
option, which :help 'filetype'
tells is local to the current buffer. Since there is no indication that it also has a global value, you cannot set it globally. This means that :set filetype=…
always affects the current buffer.
In contrast, the number
option is local to the window and is thus inherited when windows are split (including when the very first window is opened).
The stuff at :help options
, especially, :help local-options
, explains this all in more detail, though I think it omits the case of certain buffer-local options that are not backed by a global option.
So consider the sequence of events when you have :set filetype=c
in your vimrc:
- Vim starts up and executes the command, but there is no current buffer.
- Vim opens the window showing the buffer of the file you edit.
It makes sense that the first command had no effect! Moreover, you (probably) don't want every file you open in Vim to have filetype=c
; check the list of filetypes in :edit $VIMRUNTIME/ftplugin
, and that's not even all of them!
You are probably more interested in the :filetype
command, which you can place in your vimrc as :filetype plugin indent on
. This enables auto-filetype detection, plugins, and indent settings.
As you found, you can customize detection with autocommands. The typical way to do so is with ~/.vim/filetype.vim
and ~/.vim/ftdetect.vim
(:help new-filetype
). Similarly, you can write your own ftplugins and indent scripts (I recommend ~/.vim/after/ftplugin/…
and similar).