diff --git a/.vim/vimrc b/.vim/vimrc index 01a4bb8..c28b993 100644 --- a/.vim/vimrc +++ b/.vim/vimrc @@ -971,6 +971,34 @@ augroup AutoMkdir augroup END +" as per https://stackoverflow.com/a/38775966 +function! HeaderToggle() " bang for overwrite when saving vimrc +let file_path = expand('%') +let file_name = expand('%<') +let extension = split(file_path, '\.')[-1] " '\.' is how you really split on dot +let err_msg = 'There is no file ' + +if extension ==# 'cpp' + let next_file = join([file_name, '.h'], '') + + if filereadable(next_file) + :e %<.h + else + echo join([err_msg, next_file], '') + endif +elseif extension ==# 'h' + let next_file = join([file_name, '.cpp'], '') + + if filereadable(next_file) + :e %<.cpp + else + echo join([err_msg, next_file], '') + endif +endif +endfunction +nnoremap hh :call HeaderToggle() + + nnoremap ve :e $MYVIMRC nnoremap vr :source $MYVIMRC