2

I am trying to improve my vim skills lately, and therefore I edit my .vimrc a lot.

Following How do I reload my vimrc without leaving Vim?, I wonder if there is a convenient way to define a keymap for opening .vimrc, and loading its code after it has been saved?

3

You can use this to open it easily:

nnoremap <F3> :e $MYVIMRC<CR>

And replace <F3> by the key you want.

Or use a Capital mark which is persistent accross buffers and vim sessions (this is what I use):

  • Open your vimrc in a buffer
  • Press mshiftV to set the mark (or any capital letter)
  • When you want to open your vimrc again simply press 'shiftV (or any letter you used to set the mark)

To automatically source it after it is saved you can use an autocommand this way you don't have to define a mapping and no matter the way you save your buffer the source command will be executed:

autocmd BufWritePost .vimrc source $MYVIMRC

But I don't think that's a good idea, a mapping to source it like the one in the question you linked seems better since you source the file only when you want to.

  • Will the autocmd BufWritePost run If I leave the buffer using q!? If not, it's perfect for me. – Adam Matan Aug 2 '17 at 10:36
  • @AdamMatan Nope it is executed after (Post) 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 event BufLeave. 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-events for the different autocommand events available. – statox Aug 2 '17 at 11:57
3

I use below configuration for open and edit and reload my dot vimrc file:

let mapleader=","

set my mapleader to "," if you want to know more about mapleader enter :h mapleader in Vim

map <leader>vm :vsp $MYVIMRC<CR>

Open my vimrc file in vertical tab whenever I pressed ,vm

map <leader>sv :source $MYVIMRC<CR>

Reload my vimrc file whenever I pressed ,sv

nnoremap <leader>w :w!<CR>

Save my current file with ,w instead of typing :w every time.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.