15

I set an explicit file to customization created via the UI. It's named custom.el. Currently, I use the followed snippets to create this file if not exist.

(defconst custom-file (expand-file-name "custom.el" user-emacs-directory))
(unless (file-exists-p custom-file)
  (shell-command (concat "touch " custom-file)))

There is an ugly shell-command touch in, any other elisp functions can do this?

hbin
  • 2,657
  • 1
  • 23
  • 23

4 Answers4

23

You can use (write-region "" nil custom-file) not sure that is the ideal solution.

mathk
  • 7,973
  • 6
  • 45
  • 74
  • 6
    If you change the first `nil` to `""`, then you'd have the ideal solution, I think. – Sean Dec 28 '12 at 19:18
2

Perhaps a simpler solution to the underlying problem would be:

(defconst custom-file (expand-file-name "custom.el" user-emacs-directory))
;; NOERROR to ignore nonexistent file - Emacs will create it
(load custom-file t)

In other words, instead of manually creating custom.el, simply don't error when you try to load it (optional arg NOERROR is only for file existence error). Emacs will create the file the first time it writes out custom variables.

This is what I'm currently using in my init.el

Charl Botha
  • 4,373
  • 34
  • 53
0

I write this function below based on Joao Tavara anwser in this post: How do I create an empty file in emacs?

(defun fzl-create-empty-file-if-no-exists(filePath)
   "Create a file with FILEPATH parameter."
   (if (file-exists-p filePath)
       (message (concat  "File " (concat filePath " already exists")))
     (with-temp-buffer (write-file filePath))))
wagnermarques
  • 140
  • 1
  • 4
0
(make-empty-file custom-file)

Check help to know more about it: C-h f make-empty-file

nitin
  • 21
  • 1
  • 2