1

To display linenumbers, I can do: :set number in the edited file (that works) or insert set number in the .vimrc (that works too).

To change the filetype, I can do: :set filetype=c in the edited file (that works) but when I insert: set filetype=c in the .vimrc it does not work.

I have to use au[tocmd], inserting

au Bufread,BufNewFile *.mc set filetype=c

in the .vimrc.

My question is: Why do I have to use autocmd for filetype, and cannot just put set filetype=c in the .vimrc?

With the linenumbers I do not have to use an autocmd.

2 Answers 2

1

The reason is that .vimrc is global to all your buffers and the filetype is local to each buffer.

When the .vimrc file is read the buffer is not yet.

The autocmd is defined to automatically set the file type when the buffer will be read (BufNewFile or BufRead event).

For :set number it is a default option that is copied when the buffer is read or created to its local version (that you can change using :setlocal number)

2
  • Thanks for the answer, there seem to be indeed be different kinds of options, some of which you can default in the .vimrc file, some not. Or it is just such that, in the case of filetype, vim FIRSTLY looks at the .vimrc file, and THEN does an automatic type assessment, so that the value in the .vimrc file is in the end never considered. Jan 31 at 14:52
  • The filetype is particular option that can't be defined globally as number it can only be defined for a particular buffer. If the answer answer your question don't forget to validate the question using the v button next to the arrow voting buttons it allow the question to rest :-) Jan 31 at 14:57
1

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:

  1. Vim starts up and executes the command, but there is no current buffer.
  2. 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).

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.