10

I'm constantly doing this in vim: [do something in insert or normal mode], Esc, :ws

I do this hundreds of times a day. Instead, I'd like to lose the habit and have vim save to file immediately after any change to buffer.

Everything is running off an SSD so I don't expect performance to be an issue.

Nick Zalutskiy
  • 14,952
  • 7
  • 53
  • 50

4 Answers4

16

Add this simple mapping to your ~/.vimrc:

inoremap <Esc> <Esc>:w<CR>

to write the current buffer automatically on each <Esc> in INSERT mode.

romainl
  • 186,200
  • 21
  • 280
  • 313
6

The CursorHold and CursorHoldI might help. According to docs:

|CursorHold|        the user doesn't press a key for a while
|CursorHoldI|       the user doesn't press a key for a while in Insert mode

Those events fire only once after inactivity and depend on updatetime variable (default: 4000ms). So you can:

:au CursorHold <buffer> :update

Which will update current buffer file (i.e. save only if modified) after default 4 seconds of inactivity in Normal mode.

Add autocommand for CursorHoldI if you want to get the same behavior in Insert mode.

XLII
  • 1,172
  • 9
  • 18
3

Not that I know of, other than coding it (check out the VIM Wiki for a starting point). VIMs swap file almost does what you are asking, for recovery purposes.

From the VIM man page;

The swap file is updated after typing 200 characters or when you have not typed anything for four seconds

Seems like that would catch most quirks. Is there some specific problem you are trying to avoid?

AlG
  • 14,697
  • 4
  • 41
  • 54
  • I'd like to run syntastic every time I change something (by default it runs when you save) and I'd like to lose the habit of constantly saving manually. I don't like swap files because they pollute my working dir. I guess I could save every 1 second or so, but that seems unnecessary. – Nick Zalutskiy Mar 14 '12 at 21:29
2

Here's a little function for autosave: http://vim.wikia.com/wiki/Auto-save_current_buffer_periodically

Gabriel Tudor
  • 311
  • 2
  • 8