In order to open my vimrc in a split window only if the current buffer isn't really empty, I have the following mappings:
let vimrc=expand('<sfile>:p')
nnoremap ,vu :source <C-R>=vimrc<CR><CR>
nnoremap ,ve :call <sid>OpenVimrc()<cr>
function! s:OpenVimrc()
if (0==strlen(bufname('%'))) && (1==line('$')) && (0==strlen(getline('$')))
" edit in place
exe "e ".g:vimrc
else
exe "sp ".g:vimrc
endif
endfunction
NB: As my vimrc isn't named .vimrc nor _vimrc (in order to simplify it's portability to Windows machines), and as it is under a repository managed by the plugin manager I use, I cannot really use $MYVIMRC. I have another variable computed on-the-fly.
BTW: I don't want it to be automatically loaded whenever I save it. I'd rather be explicit.
For saving I already have a generic nnoremap <f2> :update<cr>, we don't need anything specific to the vimrc.
autocmd BufWritePostrun If I leave the buffer usingq!? If not, it's perfect for me. – Adam Matan Aug 02 '17 at 10:36Post) a buffer is written on disk (BufWrite). If you want to source the file when leaving with:q!you'll need to add create an autocommand with the eventBufLeave. Note that your autocommand should also write the buffer otherwise you will quite without save thus your changes will be lost and you will source the previous version of your file. See:h autocmd-eventsfor the different autocommand events available. – statox Aug 02 '17 at 11:57