Quick save to a temporary file before more edits

From Vim Tips Wiki

Jump to: navigation, search
Tip 694 Printable Monobook Previous Next

created 2004 · complexity basic · author U.R. Molar · version 6.0


You are about the modify an important file. You want to save this original file under a different name (and keep the file view). Quick, think of a new name -- oh, time wasted, for you could have quickly typed in ";s" and got back to the modification at hand.

map ;s :up \| saveas! %:p:r-<C-R>=strftime("%y%m%d")<CR>-bak.txt \| 3sleep \| e #<CR>
" Dated-BAKUP date number format, re-edit original
" first update, else changes get lost on re-edit,
" saves view (attn: write alone does not save view)
" thus better than generic copy at OS level,
" but be careful when split editing

The above saves the original file with a date-stamp as part of the filename. Note that it overwrites throughout a single day. If you want a more narrow period of time for overwrites, then supplement above map with hour, min, or seconds. The 3sleep is there just to visually verify that the save has taken place.


[edit] Comments

I modified a little:

map ;s :up \| saveas! %:p:r-<C-R>=strftime("%y%m%d-%H:%M")<CR>-bak.<C-R>=expand("%:e")<CR> \| 3sleep \| e #<CR>

this save's the original file extension ( so you have the same syntax highlighting when openning the backup)


Here's my mapping to work on a temp copy:

:map zs :exe "sav $TMP/" . expand("%:t") . strftime("-%Y-%m-%d_%H%M%S")<CR>

Unfortunately this tip interferes with MRU.vim script#521.


If you don't want the backup files in the MRU, try using :w! {file}

instead of :sav! {file} \| :e

Using :w[rite] with a filename writes the new file, but doesn't change the name of the buffer.

check also :file for buffer name problems

Personal tools