Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-save in VIM as you type

Tags:

vim

As the question title mentions. I'm looking to automatically get the file saved as I type in VIM (insert mode).

Is this possible? How to achieve it?

like image 973
Omar Abid Avatar asked Jun 28 '13 13:06

Omar Abid


People also ask

What is auto saved?

Definition of autosave : a function of a computer program (such as a word processor or spreadsheet) that automatically saves an open file at regular intervals.

How do I save a vim file in Terminal?

The simplest way to both save and quit out of VI or VIM is with a keyboard shortcut ZZ. Note the capitalization, which means the save and quit command is executed by pressing Escape, then holding the Shift key then pressing Z twice, thus: Press the ESC key, then hold the Shift key then press Z twice.

How to save and edit a file in Vim?

The command to save a file in Vim is :w. To save the file without exiting the editor, switch back to normal mode by pressing Esc, type :w and hit Enter. There is also an update command :up, which writes the buffer to the file only if there are unsaved changes. To save the file under a different name, type :w new_filename and hit Enter.

How to enable auto-save in Vim?

There is no native support for auto-saving in Vim. But you can use vim-auto-save plugin to perform that. This plugin auto-saves in normal mode only by default, but there is a section in it's README which describes how to configure the plugin to save in insert mode too.

How to type text in Vim?

When you launch the Vim editor, you’re in normal mode. In this mode, you can use vim commands and navigate through the file. To type a text, you need to enter the insert mode by pressing the i key.

How does swap work in Vim?

A "copy" of your file. Yes, swap is held in memory. When Vim reads a file it adds it to RAM. That is both for existing and new (still unsaved files). As swap is saved it is flushed to disk. The HDD. From RAM. With default settings most of your work are saved. That is for every 200 characters entered, or when yo have a 4 second pause.


2 Answers

I recommend to save the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

I found it here. It works for me.

Note (thanks to @Kevin): Unfortunately, it will result in errors if you open vim without opening a file because vim will try to save the text you type but won't have where.

like image 101
simhumileco Avatar answered Oct 03 '22 18:10

simhumileco


You can use AutoSave plugin to perform that:

https://github.com/907th/vim-auto-save

Please notice that AutoSave is disabled by default, run :AutoSaveToggle to enable/disable it.

like image 41
Jayram Avatar answered Oct 03 '22 19:10

Jayram