0

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?

5

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
hello
xyz
~ $

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

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

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.