2

I am new at using vim daily. I am doing my best to setup an good environment. I work a lot with Django projects. And I want to simulate a project like setup when I edit a file inside a django project.

Let's asume this directory structure:

/projects
    /django-app1
       manage.py
       settings.py
       .project
    /django-app1
       manage.py
       settings.py
       .project

What I want is place a file called .project that contains vim commands that will execute if I open any file whitin the directory structure

Why? Here is my vimrc https://github.com/mariocesar/dotfiles/blob/master/.vimrc#L151 you can see that I am using a snippet to load the virtualenv if it's exist on the enviroment variables, it's very cool but I don't think is efficient to do that every time I open a file, or even better add some extra paths to sys.path, as I have projects with a site-packages directories.

So:

  • How Can I detect a specific named file, looking down the current directory when open a file with vim?
  • How Can I execute this file as a vim script?
Mario César
  • 3,699
  • 2
  • 27
  • 42

2 Answers2

1

When starting vim

I think

vim +'source **/.project' 

should do the trick nicely.

If you have a recent bash, you could also more directly

vim -S **/.project

When loading a python file

To act when opening/loading a certain file:

autocmd BufReadPost *.py source %:h/**/.project

Loads any .project file found in the subtree containing the file being opened

autocmd BufReadPost *.py source %:h/.project

Loads any .project file found in precisely the same directory containing the file being opened

To remove the above auto commands (remember: adding the same command twice will execute them twice)

autocmd! BufReadPost *.py 
sehe
  • 374,641
  • 47
  • 450
  • 633
  • added autocommand approach to act when files are being loaded into a buffer – sehe Aug 22 '11 at 13:38
  • It won't work if there are files in subdirectories. BTW, the best way to clear autocommands in to put them into a group, and to clear that group. – Luc Hermitte Aug 22 '11 at 15:01
  • "if there are files in subdirectories"? That would seem to be always true. Note how I used `%:h` to limit the search scope – sehe Aug 22 '11 at 15:08
  • One `:h` isn't enough. To correctly support subdirectories, we need to look for any (/the first) local vimrc, from the current directory to a root directory. – Luc Hermitte Aug 22 '11 at 15:17
  • Note how vim has backwards searching globs: http://vimdoc.sourceforge.net/htmldoc/editing.html#file-searching. I'm not sure the question was related to .vimrc's (I interpreted this as looking for `.project` files in the subdirectory tree as the question and diagram suggest) – sehe Aug 22 '11 at 15:27
  • This still doesn't permit to easily do upward `:source` on a filename. (the `.project` file is just a filename. In the end, it's a `.vimrc` local to a directory tree. If the OP just want a `.vimrc` local to one exact directory, he simply needs to set `'exrc'` on) – Luc Hermitte Aug 22 '11 at 15:41
  • 1
    Something like `source =findfile('.project',expand('').';')` should work however. – Luc Hermitte Aug 22 '11 at 15:56
  • @LucHermitte let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2738/discussion-between-sehe-and-luc-hermitte) – sehe Aug 22 '11 at 15:58
  • @Luc: Please, put your comment about `findfile` to your answer, so this idea could be upvoted. – ib. Aug 23 '11 at 03:31
  • @ib, this idea is the root of recent local_vimrc plugins, but it's far from being enough. – Luc Hermitte Aug 23 '11 at 08:56
  • Guys! thanks, I am learning a lot of your answers and comments, I am using a personal fork of vimrc_local right now https://github.com/mariocesar/vimrc_local.vim, just have to find something to avoid reading the .vimrc_local every time I open a new buffer. thanks again! – Mario César Aug 23 '11 at 16:04
  • @mario, if you want to fork a version, fork the v1.9 from gogglecode, not the old v1.3. Regarding the re-read, it's a feature. Just add inclusion-gates as explained in the doc(comments) -- mu-template helps to fill them automatically. – Luc Hermitte Aug 23 '11 at 16:19
  • @Luc thanks for the note about the inclusion-gates, I updated my repo with your version – Mario César Aug 23 '11 at 16:36
1

There exist several plugins named local_vimrc that fulfil your need. Here is mine.

BTW, your question is a duplicate of Vim: apply settings on files in directory


EDIT: Recent implementations of local_vimrc plugins use vim7 capabilities. Somehow, this is equivalent to:

source <c-r>=findfile('.local_vimrc',expand('<afile>').';')<cr>

(thanks sehe for reminding me of ';')

"Somehow", because, many important features are missing:

  • the possibility to source all files found, in the downward order.
  • the optional specification of a root directory pattern (like '$HOME\|/opt/projects/ for instance)
  • external paths like scp://, ftp://, http:// must be handled correctly (I haven't tested how they'd behave with findfile())
  • it should be possible to force the execution of the local vimrc(s) before a template-expander plugin triggers the expansion of a templare-file (the typical application is the generation of header-gates in C/C++ .h files)

Hence my preference for the full plugin way, and not just a one-liner that will irritate us from time to time.

NB: my plugin is very old, it's pre-vim7. As it works (recursively), I've never feel the need to rewrite it with the modern vim7 list manipulations functions.

Community
  • 1
  • 1
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83