77

When I modify a buffer, Emacs automatically creates a temporary symlink in the same directory as the file being edited (e.g. foo.c):

.#foo.c -> user@host.12345:1296583136

where '12345' is Emacs' PID (I don't know what the last number means).

Why does Emacs create these links, and how do I prevent it from doing that?

Note that I have turned off auto save mode (M-x auto-save-mode) and disabled backup files (M-x set-variable -> make-backup-files -> nil). When I save a modified buffer, or undo the changes to it, the symlink disappears.

In particular, I'm trying to prevent Emacs from creating these links because they cause the directory timestamp to be modified, which causes our build system to rebuild an entire module instead of compiling and linking for one changed file :/

Thanks for any input!


Update: In order to prevent Emacs from creating interlocking files permanently, you can change src/filelock.c and build a custom binary:

void
lock_file (fn)
     Lisp_Object fn;
{
     return;
     // Unused code below...
}

Update 2: Arne's answer is correct. It's now possible to disable lock files in the latest Emacs (24.3.1), by adding this to your .emacs file:

(setq create-lockfiles nil)
3
  • 1
    thank you lots and lots for your update! I’ll see if I can change that, because it interferes badly with the buildsystem of a project I’m working on. Oct 19, 2012 at 11:21
  • The reason I had to disable lockfiles (now cleanly, with the create-lockfiles variable - thanks Emacs devs!) is that in the case of files on a Samba/CIFS share, they stick around :-( After a few editing sessions, the directories are completely cluttered with them, and because they are implemented as intentionally dangling symlinks, my filesystem lint tool reports them. Why? Mount options: rw,relatime,vers=1.0,cache=strict,domain=,uid=0,noforceuid,gid=0,noforcegid,addr=10.8.78.1,soft,unix,posixpaths,serverino,mapposix,acl,rsize=1048576,wsize=65536,echo_interval=60,actimeo=1 Jul 8, 2018 at 18:36
  • The second (last) number is apparently a timestamp.
    – x-yuri
    Dec 20, 2020 at 21:06

3 Answers 3

69

Update: Emacs 24.3 has been released with full support for this new setting!

In the current trunk of emacs, you can simply customize the variable create-lockfiles:

C-h v create-lockfiles

Documentation: Non-nil means use lockfiles to avoid editing collisions.

In your init file, you can set

(setq create-lockfiles nil)

Get it via

bzr branch bzr://bzr.savannah.gnu.org/emacs/trunk emacs-trunk
make
src/emacs

(I found out about this, because I decided to get active and just add an option like that myself… :) )

1
  • 7
    Is there a way to retain this functionality by creating the symlink elsewhere?
    – Michael
    Dec 1, 2017 at 13:48
40

The symbolic link is emacs' file interlocking system: the symbolic link indicates that an instance of emacs is editing this file. If another instance tries to edit the same file, emacs will issue a warning. See http://www.gnu.org/software/emacs/manual/html_node/emacs/Interlocking.html

This has nothing to do with auto-save.

I cannot find how to modify or disable file locking from within emacs.

3
  • Can you comment on whether this feature is at all useful on single-user systems? (I don't mean DOS and pre-WinNT; I mean systems with de facto single user) Jan 10, 2014 at 16:07
  • @ErikAllik: Most files can only be edited by the file's owner, so single-user system or not makes very little difference. This locking system is to help you avoid shooting your own foot, e.g. when you have two Emacs instances that want to modify this file at the same time.
    – Stefan
    Nov 16, 2015 at 14:18
  • I wonder why it uses a symbolic link, and not just a regular lock file? Is it just to have extra information in the target? Aug 28, 2018 at 2:57
0

It's possible to create the symlinks in a different directory. For example...

(setq lock-file-name-transforms `((".*" "~/tmp/emacs-lockfiles/" t)))

The advantage of this is that it keeps the functionality but prevents the symlinks from cluttering the directory you're working in.

You can find further information here: File Locks, GNU Emacs Lisp Reference Manual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.