I just installed a plugin for python which displays methods tree. So I would like to open the tree on startup, so not necessary to push the buttons everytime I open some .py file. But, I want this command is executed only for .py files. Here is how my vimrc piece looks like:

nmap <F8> :TagbarToggle<CR>

Is there a way to execute this command at startup only for .py files?

  • Howdy St. Antario. You might want to note that your accepted answer has many fewer upvotes than the alternative answer.
    – chicks
    Aug 31, 2017 at 17:59

2 Answers 2


Add in your .vimrc:

au BufEnter *.py :TagbarToggle<CR>


  • au is autocmd, execute the command automatically.

  • BufEnter is when you enter the file for first time.

  • *.py is for filetype python.

  • and the rest is the command to be executed
  • 2
    This will add an extra autocommand every time the .vimrc is sourced. To prevent this, the line should be surrounded with an augroup that deletes the command. See :help augroup-delete.
    – Rich
    Aug 25, 2017 at 10:30
  • :augroup testgroup : autocmd! : au BufEnter *.py :TagbarToggle<CR> augroup END is it ok?
    – kapil
    Aug 25, 2017 at 10:54
  • 1
    Looks good to me!
    – Rich
    Aug 25, 2017 at 11:03

In my opinion, the cleanest method of doing this is simply to add the command to a file in your .vim/vimfiles directory:


or for Windows:


This technique is documented in :help ftplugin-overrule (list item 3), although the context (changing settings) is slightly different.

It offers the following benefits over the autocommand technique described by kapil.

  1. Less code. You just write the Vimscript you want executed. You don't need to code an autocommand and surround it with augroup and autocommand! clear commands.

  2. Less overhead. Vim is already detecting the file type and checking for the existence of the after/ftplugin/filetype file: why add an additional autocommand that performs the same purpose?

  3. Cleaner .vimrc. I think it's better to have file-type-specific functionality separated out from my .vimrc. (Some disagree with this, though: see the disadvantages, below.)

It also has a couple of minor disadvantages:

  1. It only works if you are using Vim's filetype detection. (:help filetype)

    However, this is generally one of the first settings that people enable in their .vimrc, and I'd wager that almost all Vim users already have it switched on.

  2. Some people prefer to keep all their config in their .vimrc file.

  • 1
    +1 for ftplugin/<filetype>.vim, better separation of concerns :)
    – LEI
    Aug 25, 2017 at 12:13

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.