1

My .vimrc includes the following lines:

if filereadable("my_source")
    :source my_source
endif

That is, if the file 'my_source' exists in the folder where I open vim and it is readable, it is loaded automatically.

However, if I am editing a file in that folder and open another file (also in that same folder) via :tabe, the source file 'my_source' is not loaded in the latter. Why? How can I have it automatically loaded when I open a new tab?

Godoy
  • 69
  • 6

2 Answers2

2

You need to use an autocommand to accomplish this, as your .vimrc is only sourced once on startup. As their name would suggest, autocmds allow for defining a series of commands that should occur automatically when certain events are fired.

I recommend you read about them in Vim's help pages, as they are a ubiquitous and incredibly useful component. In the meantime, this should cover your needs adequately:

augroup MyAutoCmds
    autocmd!
    autocmd BufNewFile,BufRead,VimEnter *
      \ if filereadable('my_source') | source my_source | endif
augroup END

I highly recommend that you read this and this on why augroup is used here, and why you should be sure to use it yourself. In short, the main reason is to act as a guard when you re-source your .vimrc, otherwise the autocommands would be defined multiple times, and thus run multiple times!

ZeroKnight
  • 1,081
  • 7
  • 22
1

the statements in .vimrc are executed only once. You could create an autocommand for this matter.

Find details in :help autocommand

Naumann
  • 2,759
  • 1
  • 11
  • 16