Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically save the current buffer when is going to edit a new file

Tags:

vim

I've made some changes in the current buffer, and I want vim to automatically save the current buffer when I'm going to edit a new file with the following command:

:e another_file_which_is_not_a_buffer_in_vim_yet

I added the following line in my .vimrc file, but it didn't work.

autocmd BufLeave * update

Vim still prompted me No write since last change, why? How can I make it work? By the way, I only want to save the current buffer instead of all buffers, because saving all buffers seems to mess up the order of the buffers, which would bring trouble when I run :bp or :bn.

like image 401
Searene Avatar asked Dec 25 '15 14:12

Searene


1 Answers

Vim has an option to save also for, among others, :edit in addition to what triggers autowrite:

set autowriteall

Relevant manual excerpts:

autowrite:

'autowrite' 'aw'  boolean (default off)
                  global
  Write the contents of the file, if it has been modified, on each
  :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!,
  :make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I,
  '{A-Z0-9}, or `{A-Z0-9} command takes one to another file.
  Note that for some commands the 'autowrite' option is not used, see
  'autowriteall' for that.

and autowriteall:

'autowriteall' 'awa'  boolean (default off)
                      global
                      {not in Vi}
  Like 'autowrite', but also used for commands ":edit", ":enew", ":quit",
  ":qall", ":exit", ":xit", ":recover" and closing the Vim window.
  Setting this option also implies that Vim behaves like 'autowrite' has
  been set.
like image 179
Benjamin W. Avatar answered Sep 23 '22 20:09

Benjamin W.