Unix Power ToolsUnix Power ToolsSearch this book

18.14. File-Backup Macros

Emacs automatically keeps backup copies of the file you're editing. If you have editing problems (or just change your mind), you can get the previous file version by recovering from a backup file. I like this idea, but I don't like the way that backups are done automatically. Instead, I want to choose when vi makes a backup "snapshot." This macro, CTRL-w, lets me do that: it writes a copy of the current filename as filename~. (The trailing tilde (~) is an Emacs convention. Section 14.17 shows ways to remove these backup files.) Whenever I want to save a snapshot of the editing buffer, I just type CTRL-w.

^M Section 18.6

map ^W :w! %~^M

The w! writes without questions, overwriting any previous backup with that name. vi replaces % (percent sign) with the filename (or pathname) you're currently editing.

If you want an Emacs-style backup to be made every time you write the file (except the first time), you could try something like this:

map ^W :!cp -pf % %~^M:w^M

The first command uses cp -p (Section 10.12) to make a backup of the previously written file; the cp -f option forces the write. (vi may warn you File modified since last write, but the versions I've checked will run cp anyway.) The next command writes the current editing buffer into the file.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.