Skip to content

Instantly share code, notes, and snippets.

@gfixler
Last active December 16, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfixler/5368404 to your computer and use it in GitHub Desktop.
Save gfixler/5368404 to your computer and use it in GitHub Desktop.
ThatOtherFile - Stateless buffer toggling in Vim, via basename. Toggle between proj/file.ext and proj/<basename>/<basename>_file.ext
function! ThatOtherFile (basename)
" Toggle between ./file.ext and ./<basename>/<basename>_file.ext
" Prompts to create missing <basename> directory if absent.
let path = expand('%:p:h')
let filename = expand('%:p:t')
let opath = path[-len(a:basename):]
let ofilename = filename[:len(a:basename)]
if opath == a:basename
if ofilename == a:basename.'_'
let opath = path[:-len(a:basename) - 1]
let ofilename = filename[len(a:basename) + 1:]
"echo 'using: '.opath.ofilename
exe 'edit '.opath.ofilename
endif
else
let opath = path.'/'.a:basename
let ofilename = a:basename.'_'.filename
"echo 'using: '.opath.'/'.ofilename
if !isdirectory(opath)
if input('Create path "'.opath.'"? (yN): ') == 'y'
call mkdir(opath, 'p')
if !isdirectory(opath)
throw 'unable to create directory "'.opath.'"'
endif
else
echo 'canceled'
return
endif
endif
silent exe 'edit '.opath.'/'.ofilename
endif
endfunction
" example file togglers
nnoremap \n :call ThatOtherFile('note')<CR>
nnoremap \u :call ThatOtherFile('test')<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment