Last active
December 16, 2015 03:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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