ELisp: Print, Output

By Xah Lee. Date: . Last updated: .

Emacs lisp has several ways to print. You can print to the Messages Buffer, or insert text directly into a buffer.

Print to Messages Buffer

message
(message FORMAT_STRING &rest ARGS)

Print a Format String to the Messages Buffer .

(message "Name is: %s" "Joe")

Insert Text to Current Buffer

insert
(insert &rest ARGS)

Instert string to current buffer, at cursor position.

(insert "something")

[see ELisp: Text Editing Functions]

Print to a Buffer

print
(print OBJECT &optional TARGET)
  • Print lisp object in lisp syntax.
  • Print a newline at end.
  • Optional arg for a target buffer, or other functions.

Add format if you need to print multiple items in one shot. [see ELisp: Format String]

When writing a elisp script that does batch processing, it's best to print to your own buffer, because the Messages Buffer scrolls off.

(setq xbuff (generate-new-buffer "*my output*"))

(print "something" xbuff)

(switch-to-buffer xbuff )

[see Buffer Functions]

prin1
(prin1 OBJECT &optional TARGET)

Like print, but does not add newline at end.

princ
(princ OBJECT &optional TARGET)
  • Print in more human-readable format. (e.g. no string delimiters.)
  • No newline at end.
warn
(warn FORMAT_STRING &rest ARGS)

Print a warning of Format String .

(warn "Name is: %s" "Joe")

Temporarily Specify a Buffer for Output

with-output-to-temp-buffer
(with-output-to-temp-buffer BUFNAME &rest BODY)

Bind standard-output to buffer BUFNAME, eval BODY, then show that buffer.

  • buffer is cleared before printing to it.
  • print functions default to print to the buffer.
(let ((xbuff (generate-new-buffer "*my output*")))
  (with-output-to-temp-buffer xbuff
    ;; this is printed to xbuff
    (print "abc")
    ;;
    ))

Reference

Emacs Lisp, Print, Output