How to create temporary files `.#filename` in `/tmp`, not working directory

StackOverflow https://stackoverflow.com/questions/23317002

  •  10-07-2023
  •  | 
  •  

سؤال

When files are being modified in Emacs, a temporary file is created in the working directory that looks like this: .#filename. The file is deleted when the buffer is saved.

I found a few of these types of temporary files in my Git remote repositories, and I thought it might be better to nip the bud at the source instead of configuring Git to ignore them for every project.

How can we configure Emacs to create those files in the /tmp directory instead of the working directory?

هل كانت مفيدة؟

المحلول

The file at issue is called a lock file -- commencing with Emacs version 24.3, it can be controlled with the following setting:

(setq create-lockfiles nil)

https://stackoverflow.com/a/12974060/2112489

نصائح أخرى

As a more general solution, you could also make a global exclude file, which applies to all repositories locally. By default, this will be in $XDG_CONFIG_HOME/git/ignore (usually ~/.config/git/ignore). The path can be overridden using the core.excludesFile option. See the gitignore manpage for more details.

$ mkdir -p ~/.config/git
$ echo '.#*' >> ~/.config/git/ignore

These files are auto-save files. The variable auto-save-file-name-transforms controls what modifications to make to the buffer's file name to generate the auto save file name. Usually, the default in file.el will suffice to put all the auto save files in the /tmp directory. It's default value is:

(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "/tmp/\\2" t))

That /tmp comes by reading the variable temporary-file-directory. Check that value so that it points to /tmp. Then, the value constructed for auto-save-file-name-transforms (and hence for the auto save file name) will be correct.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top