As I am setting .vimrc, I found these two code blocks have the same functionality.

au Filetype python set
     \ tabstop=4
     \ softtabstop=4
     \ shiftwidth=4
     \ textwidth=79


au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ set softtabstop=4
    \ set shiftwidth=4
    \ set textwidth=79

I reckon that there is a very subtle difference between them, but cannot figure it out. Like, vim interprets Filetype when opening the file etc.

  • 1
    BTWBTW, au is autocmd
    – barlop
    Commented Jan 12, 2020 at 23:01

2 Answers 2


Your intuition is correct: the two autocommands are very similar.

The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.

However, try running the following commands with each of the two autocommands in place:

:w new.py

You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.

However, in my opinion, there is an even better solution. Create a file in the location:


And enter the contents:

set tabstop=4
set softtabstop=4
set shiftwidth=4
set textwidth=79

If you don't already have filetype plugins enabled, you will need to do so by adding plugin to the filetype command in your vimrc file:

filetype plugin on

This will have the same effect, no autocommands required!

  • 5
    @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
    – muru
    Commented Dec 11, 2018 at 2:01
  • For anyone wondering, here's an explanation for what the ~/.vim/after directory is for, from the Learn Vimscript the Hard Way - Plugin Layout chapter: "Files in this directory will be loaded every time Vim starts, but after the files in ~/.vim/plugin/."
    – icc97
    Commented Jan 9, 2023 at 19:15

The second approach is needed in case Vim don't know the file type.

For tab-separated values I use this on my vimrc:

au BufNewFile,BufRead *.tsv set ft=tsv

And then use FileType as normal. As in the case of using filetype plugin files.

  • 1
    You could also you an ftdetect script for this, though the line of code would be almost identical.
    – D. Ben Knoble
    Commented Jul 23, 2021 at 16:20
  • @D.BenKnoble. Thanks. I didn't know.
    – Daniel
    Commented Jul 23, 2021 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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