1

I have a following very simple .vimrc file based on an example in Practical Vim book:

set nocompatible
set hidden
if has("autocmd")
  filetype on
  autocmd FileType sh setlocal ts=2 sts=2 sw=2 expandtab
endif

As I understand, if file is a shell script file, then FileType event happens, if condition is started and setlocal ts=2 sts=2 sw=2 expandtab command is executed? However, what is the point of keeping the filetype on(enables file type detection) inside the if statement? Shouldn't this be outside and before the if statement?

1
  • 4
    :filetype needs a version of Vim compiled with autocmd to work. – Sato Katsura Aug 29 '16 at 18:23
3

However, what is the point of keeping the filetype on(enables file type detection) inside the if statement? Shouldn't this be outside and before the if statement?

The point is to avoid parse errors if filetype isn't available.

if(has("autocmd")) is testing if the version of vim parsing the .vimrc was compiled with autocommand support. :filetype isn't available unless autocommand support has been compiled in, so trying to call it outside the body of that if statement on those version of vim would produce errors.

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.