Reputation: 35597
I'm trying to add a mapping to vimrc
so if I press \r
the current buffer will close without saving changes. I think :enew!
is what I'd like to map but the way I've done it is wrong:
" Use Leader-r for closing the buffer without saving changes, also in Insert mode
nnoremap <Leader>r :<C-U>enew!<CR>
vnoremap <Leader>r :<C-U>enew!<CR>gv
cnoremap <Leader>r <C-C>:enew!<CR>
inoremap <Leader>r <C-O>:enew!<CR>
How do I amend the above?
Edit: As pointed out by glts what I'd actually like to do is discard the current buffer without saving changes
Upvotes: 0
Views: 86
Reputation: 22734
As the help states, :enew!
discards changes made to the current buffer. Instead, use :hide enew
, or, as many users prefer, set hidden
in your vimrc and then just use :enew
.
Or maybe what you actually meant was how to delete the current buffer without saving the changes? In that case, use :bd!
instead of :hide enew
.
nnoremap <Leader>r :<C-U>hide enew<CR>
xnoremap <Leader>r :<C-U>hide enew<CR>
cnoremap <Leader>r <C-C>:hide enew<CR>
inoremap <Leader>r <Esc>:hide enew<CR>
Personally I would advise against making such "universal" mappings. A normal mode mapping should be enough since a single Esc will take you there anyway.
An advanced solution suggested by @ZyX is to squeeze these four lines into one with this bit of <C-\><C-N>
magic:
for a in ['n','x','c','i'] | exe a.'noremap <Leader>r <C-\><C-N>:hide enew<CR>' | endfor
Upvotes: 2