From 48e84878013d810b49ee2be7f545b517d915b451 Mon Sep 17 00:00:00 2001 From: surtur Date: Thu, 11 Aug 2022 12:41:49 +0200 Subject: [PATCH] vimrc: add src-hdr file switching func --- .vim/vimrc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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