Simultaneous editing occurs when two users visit the same file, both make changes, and then both save them. If nobody is informed that this is happening, whichever user saves first would later find that their 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 file.
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 specially-named symbolic link7 with special contents in the same directory. See (elisp)File Locks, for more details.) Emacs removes the lock when you save the changes. The idea is that the file is locked whenever an Emacs buffer visiting it has unsaved changes.
You can prevent the creation of lock files by setting the variable
nil. Caution: by
doing so you will lose the benefits that this feature provides. You
can also control where lock files are written by using the
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.
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.
Note that locking works on the basis of a file name; if a file has multiple names, Emacs does not prevent two users from editing it simultaneously under different names.
A lock file cannot be written in some circumstances, e.g., if Emacs lacks the system permissions or cannot create lock files for some other reason. In these cases, Emacs can still detect the collision when you try to save a file, by checking the file’s last-modification date. If the file has changed since the last time Emacs visited or saved it, that implies that changes have been made in some other way, and will be lost if Emacs proceeds with saving. Emacs then displays a warning message and asks for confirmation before saving; answer yes to save, and no or C-g cancel the save.
If you are notified that simultaneous editing has already taken place, one way to compare the buffer to its file is the M-x diff-buffer-with-file command. See Comparing Files.
You can prevent the creation of remote lock files by setting the
The minor mode
lock-file-mode, called interactively, toggles
the local value of
create-lockfiles in the current buffer.
If your file system does not support symbolic links, a regular file is used.