Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write to a file in emacs

Tags:

file

emacs

elisp

I want the emacs lisp code to append some data to a log file from emacs. The log file is large so I don't want to read it into memory.

I just need to open the log file, append some data to it, close it. I never need to see or manually edit the content.

like image 618
ja. Avatar asked Mar 26 '11 14:03

ja.


People also ask

How do I edit a file in Emacs?

The emacs editing mode is entered when you enable either the emacs or gmacs option. The only difference between these two modes is the way each handles the Ctrl-T edit command. To edit, move the cursor to the point needing correction and insert or delete characters or words, as needed.

How do I type in Emacs?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.

Can you write code in Emacs?

Emacs helps you be productive by providing an integrated environment for many different kinds of tasks: All of the basic editing commands (and there are lots of them) are available no matter what you're trying to do: write code, read a manual, use a shell, or compose an email.

How do you write commands in Emacs?

To execute Control commands, written as CTRL-letter , hold down the Control key while typing the letter indicated. To execute Escape commands, written as ESC letter , press the Escape key (once) and then type the letter indicated. and hit the Return key.


2 Answers

You can use the append-to-file lisp function.

Append the contents of the region to the end of file filename. When called from a function, expects three arguments, start, end and filename. start and end are normally buffer positions specifying the part of the buffer to write.

If start is nil, that means to use the entire buffer contents.

If start is a string, then output that string to the file instead of any buffer contents; end is ignored.

More information is available here

like image 117
Sandro Munda Avatar answered Sep 20 '22 19:09

Sandro Munda


(defun add-log-entry (log-message log-file)
  "Add a given message string to the end of a file."
  (append-to-file log-message nil log-file))
like image 36
Thomas Avatar answered Sep 23 '22 19:09

Thomas