Emacs Lisp: Print, Output

By Xah Lee. Date: . Last updated: .

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

(message FORMAT_STRING &rest ARGS)
Print a Format String to the Messages Buffer .
(message "Name is: %s" "Joe")
(insert &rest ARGS)
Instert string to current buffer, at cursor position.
(insert "something")
(print OBJECT &optional PRINTCHARFUN)
Print lisp object in lisp syntax. Output can be read back by function read. Optional arg for a target buffer, or other functions.

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]

(info "(elisp) Output Functions")

(prin1 OBJECT &optional PRINTCHARFUN)
Like print, but does not add newline at end.
(princ OBJECT &optional PRINTCHARFUN)
Print without newline nor string delimiters. For easy human reading.


(with-output-to-temp-buffer BUFNAME &rest BODY)
Bind ‘standard-output’ to buffer BUFNAME, eval BODY, then show that buffer. This construct makes buffer BUFNAME empty before running BODY. It does not make the buffer current for BODY. Instead it binds ‘standard-output’ to that buffer, so that output generated with ‘prin1’ and similar functions in BODY goes into the buffer.
(setq xbuff (generate-new-buffer "*my output*"))

(with-output-to-temp-buffer xbuff

  ;; this is inserted in current buffer
  (insert "xyz")

  ;; this is printed in buffer xbuff
  (print "abc"))

(switch-to-buffer xbuff )
ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Lisp Basics


Lisp Data Structure


Lisp Symbol

Lisp Misc

Working with Elisp