Elisp: Print, Output

By Xah Lee. Date: . Last updated: .

Here's a summary of most basic elisp printing functions:

Simple Printing with “message”

The most basic printing function is message. Here's a example:

; printing
(message "hi")

; printing variable values
(message "Her age is: %d " 16)        ; %d is for number
(message "Her name is: %s " "Vicky")  ; %s is for string
(message "Her mid init is: %c " 86)   ; %c is for character in ASCII code

You can see past output of the message function in the buffer named “*Messages*”. You can view the buffer by Alt+x view-echo-area-messagesCtrl+h e】.

The “*Messages*” buffer is a special buffer, because it is the general output destination for any messages from emacs.

For example, it automatically truncates the top entries when the buffer reaches some size (see variable messages-buffer-max-lines). Also, when a message is repeated many times, it automatically condenses the repeated lines. And if a message is a huge line, the line is truncated automatically. Here's a example.

;; print a line many times
(setq xx 1)
(while (< xx 20)
  (message "yay")
  (setq xx (1+ xx))
  )
(switch-to-buffer "*Messages*")

In the above example, it prints “yay [19 times]” instead of the “yay” line for 19 times.

(info "(elisp)Displaying Messages")

Print to Your Own Buffer

When writing a elisp script that does batch processing, it's best to print to your own buffer.

For example, suppose you have a elisp batch script that do find and replace on all files in a dir. For each file visited, it prints out the file path. If you use (message …), it prints to the “*Messages*” buffer, which automatically roll off the top if you have more than a hundred lines. Also, it may intermix your script's output with output from other emacs activities.

;; example of printing to a temp buffer
;; prints all file path ending in html

(require 'find-lisp)
(with-output-to-temp-buffer "*my output*"
  (mapc
   (lambda (x)
     (print x))
   (find-lisp-find-files
    "/home/xah/web/ergoemacs_org/emacs/"
    "\\.html$"))
  (switch-to-buffer "*my output*"))

In the above example, any call to print in “my-process-file” is output to your temp buffer.

“print” and “prin1” function

Elisp provides the print function. The basic syntax is this:

(print OBJECT)

The “OBJECT” is any elisp object you want to print. It can be any lisp datatype, such as string, number, list, buffer, frame, …, etc.

There's also a function named prin1. This is same as print except it doesn't add a newline.

“princ” for Human-Friendly Output

The princ is similar to print, except that the output is meant to be human-friendly. For example, it does not print the delimiters of strings.

(princ '("x" "y")) ; result display is (x y)
(prin1 '("x" "y")) ; result display is ("x" "y")

(info "(elisp) Output Functions")

Elisp, Writing Scritpts

  1. Run Elisp Script in Shell
  2. Get Command Line Args
  3. Read File
  4. Read File Content as List
  5. Write File
  6. Print, Output
  7. Format String
  8. Walk Dir
  9. Call Shell Command
  10. Get Script Name at Run Time
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.