I frequently use CTRL-6 to jump to the previously edited file in vim.

When I use it in a file that I have modified, but not saved, vim will give me the error

No write since last change (use ! to override)

Is the a way to add ! to my hotkey in those cases? Or is :e! # my only option?


Add nnoremap <C-^> :e! #<CR> to your .vimrc file to remap Ctrl+^ (unfavorable).

However, if you do so switching to the previous file will result in losing unsaved changes made to the recent file. Therefore I recommend one of the following alternatives:

  • Add set hidden to your .vimrc file to not unload a buffer if it gets abandoned, see VIM's online help :help hidden (preferred solution).

  • Enable autowrite by adding set autowrite to your .vimrc (feasible alternative).

  • 1
    Your last two recommendations are good (in particular, set hidden is what I'd recommend here.) Your first one is not. May I suggest you rephrase your answer to focus on set hidden first, set autowrite second (not as good, since you may have planned to discard the changes on that buffer.) And I'd suggest just dropping the :e! one, or just mention it with a warning that it'll lose data most probably. – filbranden Sep 28 '19 at 15:04
  • 1
    @filbranden I entirely agree, but since it was explicitly asked how to remap Ctrl+^ the first paragraph contains the (only) correct answer. Instead of rephrasing my answer, I added bold text to indicate which solution should be preferred. Together with your comment this should be sufficient. If you disagree, feel free to edit my answer. – dirdi Sep 28 '19 at 19:12
  • Thanks. I take it there is no way to apply the "force" mechanism of ! to, say, the most recent hotkey command entered? The same way I would do with sudo !!in bash? – LokiRagnarok Oct 1 '19 at 7:59
  • 1
    The first (marked unfavorable) solution suppress the warning for the Ctrl+^ hotkey. Of course you could remap other keys in a similar way to suppress other warnings, too. But bear in mind that those warnings are there for a reason, as pointed out by @filbranden. – dirdi Oct 1 '19 at 8:31

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.