Hongxu Chen
Hongxu Chen

Reputation: 5350

In emacs lisp how can I append a string to a file that I don't like to open?

I need to append a string S(belonging to no buffer) to a special file F, but since I hope this operation takes as little time as possible I don't want F opened as a buffer.

AFAIK, there is a built-in function in emacs called write-region, but this requires the content to be written into F inside one of the buffers(while in my case there is no guarantee that S should be such a string). How can I make it?

Upvotes: 10

Views: 4641

Answers (3)

Lindydancer
Lindydancer

Reputation: 26124

Why don't you try something like:

(defun my-append-string-to-file (s filename)
  (with-temp-buffer
    (insert s)
    (write-region (point-min) (point-max) filename t)))

EDIT: Apparently, as @Stefan answered, write-region is capable of appending a string to a file, so I would recommend using his answer instead.

Upvotes: 8

Stefan
Stefan

Reputation: 28541

(write-region <STRING> nil <FILENAME> 'append)

Upvotes: 25

tod davies
tod davies

Reputation: 9

Not sure exactly what you want to achieve but something like this might do the trick ... you can just put the path to the file you want to append to in place of /path/to/F.

(defun interactive-append (something)
  (interactive "sAppend S to F:")
  (shell-command (format "echo '%s' >> /path/to/F" something)))

Upvotes: 0

Related Questions