TIL: Emacs Lisp - write a string to a file
A short, sweet, very basic TIL today. You have a string. You want to write it to a file. You are using Emacs Lisp.
Use f. It’s a good library.
Then call f-write-text
.
(setq my-string "Hello world.")
(f-write-text my-string 'utf-8 "file_path.txt")
You can substitute the 'utf8
symbol with other encodings if that’s your
thing. The encoding is passed to encode-coding-string
which is the subject of
the previous TIL.
M-x list-coding-systems
will tell you which coding systems you have
installed.
The stdlib way is to use
with-temp-file
combined with insert
. This is nicer.
For most I/O operations, you want to start with the excellent f, just as most of the time, for string manipulation, you want to use s, and for sequences (or function combinators, or a bunch of other stuff), you want dash.
Awesome Elisp is a helpful list of other modern libraries for common programming tasks, as is this blog post.
This is a TIL post. What’s that?