23.3.4 Protection against Simultaneous Editing
Simultaneous editing occurs when two users visit the same file, both
make changes, and then both save them. If nobody were informed that
this was happening, whichever user saved first would later find that his
changes were lost.
On some systems, Emacs notices immediately when the second user starts
to change the file, and issues an immediate warning. On all systems,
Emacs checks when you save the file, and warns if you are about to
overwrite another user's changes. You can prevent loss of the other
user's work by taking the proper corrective action instead of saving the
When you make the first modification in an Emacs buffer that is
visiting a file, Emacs records that the file is locked by you.
(It does this by creating a symbolic link in the same directory with a
different name.) Emacs removes the lock when you save the changes. The
idea is that the file is locked whenever an Emacs buffer visiting it has
If you begin to modify the buffer while the visited file is locked by
someone else, this constitutes a collision. When Emacs detects a
collision, it asks you what to do, by calling the Lisp function
ask-user-about-lock. You can redefine this function for the sake
of customization. The standard definition of this function asks you a
question and accepts three possible answers:
- Steal the lock. Whoever was already changing the file loses the lock,
and you gain the lock.
- Proceed. Go ahead and edit the file despite its being locked by someone else.
- Quit. This causes an error (
file-locked), and the buffer
contents remain unchanged—the modification you were trying to make
does not actually take place.
Note that locking works on the basis of a file name; if a file has
multiple names, Emacs does not realize that the two names are the same file
and cannot prevent two users from editing it simultaneously under different
names. However, basing locking on names means that Emacs can interlock the
editing of new files that will not really exist until they are saved.
Some systems are not configured to allow Emacs to make locks, and
there are cases where lock files cannot be written. In these cases,
Emacs cannot detect trouble in advance, but it still can detect the
collision when you try to save a file and overwrite someone else's
If Emacs or the operating system crashes, this may leave behind lock
files which are stale, so you may occasionally get warnings about
spurious collisions. When you determine that the collision is spurious,
just use p to tell Emacs to go ahead anyway.
Every time Emacs saves a buffer, it first checks the last-modification
date of the existing file on disk to verify that it has not changed since the
file was last visited or saved. If the date does not match, it implies
that changes were made in the file in some other way, and these changes are
about to be lost if Emacs actually does save. To prevent this, Emacs
displays a warning message and asks for confirmation before saving.
Occasionally you will know why the file was changed and know that it does
not matter; then you can answer yes and proceed. Otherwise, you should
cancel the save with C-g and investigate the situation.
The first thing you should do when notified that simultaneous editing
has already taken place is to list the directory with C-u C-x C-d
(see Directories). This shows the file's current author. You
should attempt to contact him to warn him not to continue editing.
Often the next step is to save the contents of your Emacs buffer under a
different name, and use
diff to compare the two files.