Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Created March 18, 2009 13:03
Show Gist options
  • Save ubermuda/81105 to your computer and use it in GitHub Desktop.
Save ubermuda/81105 to your computer and use it in GitHub Desktop.
"-------------------------------------------------------------------------------
" Description: Finds and cd to the symfony root of the project
" Copyright: Copyright (C) 2009 Geoffrey Bachelet
" Maintainer: Geoffrey Bachelet
" Version: 1.0
"-------------------------------------------------------------------------------
if exists('find_symfony_root_loaded')
finish
endif
let find_symfony_root_loaded = 1
if ! exists('find_symfony_root_symfony_executable')
let find_symfony_root_symfony_executable = 'symfony'
endif
" root detection when opening a new vim seems to work
" only if these two events are bound. not sure why.
autocmd BufWinEnter,BufRead * call FindSymfonyRoot()
function FindSymfonyRoot()
let l:cwd = GetAbsoluteDirname(@%)
let l:symfony_root = findfile(g:find_symfony_root_symfony_executable, l:cwd.';')
let l:symfony_root = GetAbsoluteDirname(l:symfony_root)
if strlen(l:symfony_root) != 0
execute 'cd '.l:symfony_root
endif
endfunction
function GetAbsoluteDirname(path)
let l:path = a:path
" gets the dirname
if !isdirectory(l:path)
let l:path = strpart(l:path, 0, strridx(l:path, '/'))
endif
" makes it absolute
if match(l:path, '/') != 0
let l:path = getcwd().'/'.l:path
endif
return l:path
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment