Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save file after each edit in vim

Tags:

vim

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.

like image 994
Nick Zalutskiy Avatar asked Mar 14 '12 19:03

Nick Zalutskiy


People also ask

How do I save something in vim?

Press Esc key and type :w to save a file in vim. One can press Esc and type :wq to save changes to a file and exit from vim.

What command is used to save and resume editing a file in the vim editor?

Save file in vim by pressing ESC and ZZ. This will save and exit in vim or vi text editor running on Unix or Linux.


4 Answers

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.

like image 150
XLII Avatar answered Oct 30 '22 17:10

XLII


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

like image 43
Gabriel Tudor Avatar answered Oct 30 '22 17:10

Gabriel Tudor


Add this simple mapping to your ~/.vimrc:

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

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

like image 44
romainl Avatar answered Oct 30 '22 18:10

romainl


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?

like image 25
AlG Avatar answered Oct 30 '22 16:10

AlG