Tip 571 Printable Monobook Previous Next

created September 30, 2003 · complexity basic · author mosh · version 5.7

See tip 804 for the correct way to do upward tag searches. There are several "local vimrc" plugins available which are probably a better solution for a directory-specific .vimrc file:

  • localvimrc which sources all (or configurable number of) .lvimrc files (configurable name) from the buffer's directory up to the root.
  • local_vimrc which sources all _vimrc_local.vim files from $HOME to current buffer's directory
  • local configuration which sources a .lvimrc file in the buffer's directory. Documentation is not clear whether/how far an upward search is performed.
  • perdirvimrc which sources files of several different names, starting at the root directory (or home directory, this is not made clear) and progressing to the current working directory (or the buffer's directory, this is not made clear either).
  • localrc which sources files of a configurable name in the directory of any file you are editing, optionally only for files of a configurable file extension. Some recursive searching is done but the documentation does not specify exactly how this works.

That said, here is a very simple solution:

let local_vimrc = ".vimrc"
let local_tags = "tags"
let local_path = "/"
let current_path = getcwd()
" If the current path is a child of $HOME directory, start from $HOME
if current_path =~ $HOME
    let local_path = $HOME . local_path
    let current_path = substitute(current_path, $HOME, '')
let path_parts = split(current_path, "/")
for path_part in path_parts
    let local_path = local_path . path_part . "/"
    if filereadable(local_path . local_vimrc)
        exe ":so " . local_path . local_vimrc
    if filereadable(local_path . local_tags)
        exe ":set tags+=" . local_path . local_tags
unlet local_vimrc local_tags local_path current_path path_parts   



There's a simpler way to do this for tags:

set tags+=tags;/

Check above tip. It probably should be entirely replaced with the comment above.

Where is this documented in the :help?

:help 'tags' and :help file-searching

Should we delete this tip? Its content is handled in two separate tips. --Fritzophrenic 15:28, May 3, 2011 (UTC)

I haven't had to worry about large trees of source for a while, and have forgotten what all the tags tricks do (although I've got some notes I took on it somewhere, and I'm sure this tip is the wrong approach). The current tip looks unhelpful to me. While mildly interesting, it is essentially an abuse of the system (do you really need a script which does what this appears to do?). I do not understand how VimTip727 has a way to handle local vimrc, but that doesn't matter. We could just replace this with a redirect to Single tags file for a source tree (and I would handle the associated change to the tip number). Is the first comment worth transferring to the target tip? JohnBeckett 07:19, May 4, 2011 (UTC)
You're right, we do not seem to have a "local vimrc" tip. No doubt the person who added the VimTip727 comment really meant script#727. I have added a list of scripts I found to the tip. I wonder if we might instead replace the tip with a redirect to a "list of local .vimrc scripts" page, but then we'd lose the "tags" portion. Actually some general information about upward search is useful for 'tags' as well as 'path' and some other options/functions. The upward search is already noted in Single tags file for a source tree. Maybe we should refer to that tip as a "see also" on the "list of scripts" page, as well as a link to :help file-searching. --Fritzophrenic 15:13, May 4, 2011 (UTC)