Skip to content

Instantly share code, notes, and snippets.

@Houl
Last active October 21, 2017 19:40
Show Gist options
  • Save Houl/2b355f3a4e2fc626efc79e4392f727ea to your computer and use it in GitHub Desktop.
Save Houl/2b355f3a4e2fc626efc79e4392f727ea to your computer and use it in GitHub Desktop.
Autocd to project's subdir
" Created: 2017 Oct 21
" Last Change: 2017 Oct 21
" Version: 0.1
" Author: Andy Wokula <[email protected]>
" License: Vim License, see :h license
" should be a full-qualified existing directory
if !exists("g:projects_dir")
let g:projects_dir = expand('$HOME/code/git')
endif
" enable at startup
if !exists("g:projects_autochdir")
let g:projects_autochdir = 1
endif
augroup AutocdProject
augroup END
com! -bar -bang AutocdProject :call s:AutocdProject(<bang>1)
func! s:AutocdProject(enable)
if a:enable
au! AutocdProject BufEnter * :call ChdirToSubProjectRoot()
else
au! AutocdProject
endif
endfunc
func! ChdirToSubProjectRoot()
let prjdir = g:projects_dir
let bufdir = expand('%:p:h')
let plen = strlen(prjdir)
let blen = strlen(bufdir)
if plen < blen && strpart(bufdir, 0, plen) ==# prjdir
exec 'cd '. prjdir
exec 'cd '. matchstr(expand('%'), '^[^\\/]*')
endif
endfunc
if g:projects_autochdir
AutocdProject
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment