How to silently (in background) create a new line at the end of a specific file, say /path/to/file.txt (which is not necessarily a visiting file and is not necessarily already open in emacs) and insert some string (say xyz) in that new line and then to save the file after?

1 Answer 1


You can use the append-to-file and write-region functions, for example,

~ $ echo hello > file.txt
~ $ emacs --batch --eval '(append-to-file "xyz\n" nil "file.txt")'
~ $ cat file.txt
~ $

See also (info "(elisp) Writing to Files").

  • Thanks, hence the answer is (append-to-file "xyz\n" nil "/path/to/file.txt").
    – Name
    Commented Feb 2, 2018 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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