1

I try to make a new file, according to this question and answer: How to create an empty file by elisp?

I copied the code

(write-region "" nil custom-file)

into a new emacs file and wanted to execute it by C-x C-e. But all i get is

 Wrong type argument: stringp, nil

So what am i doing wrong here ? Im pretty new to emacs and have tried finding a solution by googling it but had no success.

Community
  • 1
  • 1
user2664856
  • 630
  • 14
  • 30

1 Answers1

2

Sounds like the value of custom-file is nil. It should be a string (that names a file).

If you set variable debug-on-error to t then you'll get a backtrace that will show you which arg that was expected as a string is actually nil.

Try wrapping your (relative) file name in expand-file-name, like this:

(write-region "" nil (expand-file-name "new_file"))

Now it should create the file in the current default-directory.

It also makes a difference where your cursor is, when executing C-x C-e. Put it at the end of the document.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • If I replace custom-file with "test_new_file", the output in the bottom of emacs is "test_new_file". But I can not find the new_file. Or what should I replace it with ? The new file should be in my home folder, right ? Or somewhere else ? – user2664856 Oct 21 '16 at 16:23
  • The text you are asking it to write to the file is empty: `""`. So you can expect that the file will be empty. You (apparently) asked Emacs to write an empty string to a new file. What do you expect? What are you really trying to do? – Drew Oct 21 '16 at 20:09
  • The problem is not, that the file is empty (also i did not say that anywhere). The problem is however, that no file at all is created. – user2664856 Oct 21 '16 at 20:57
  • 1
    Works for me. Did you use an *absolute* file name? If not, try wrapping your (relative) file name in `expand-file-name`. – Drew Oct 21 '16 at 22:55