1

Is there a way to see the values of a list of variables? E.g., if I have var1, var2 and var3, is there a standard elisp function that shows me the values of these variables at the same time?

I mean something that returns an output like:

var1   nil
var2   t
var3   nil

I don't need the output to be a string. I just need to read it in a window.

Please note that I asked if is there an emacs standard function that already does it. Otherwise every help is welcome but I can also try to do it by myself.

2
  • By "returns an output like..." do mean you want the function to return a string with the values formatted as you specified? Or do you want the function to insert this in a buffer perhaps? Could you be more specific? Feb 16 at 19:03
  • @Aquaactress I updated my question. Thank you for pointing this detail. Feb 16 at 19:56
1

This is a slight elaboration of Aquaactress' answer that uses an Org mode code block to produce a table. It uses -zip-with from the dash package.

#+begin_src emacs-lisp
  (setq x 'foo y 'bar z 'baz)

  (setq l '(x y z))

  (-zip-with #'list (mapcar #'symbol-name l) (mapcar #'symbol-value l))

#+end_src

#+RESULTS:
| x | foo |
| y | bar |
| z | baz |

BTW, I tried producing the pairs with a function and failed miserably:

#+begin_src emacs-lisp
  (setq x 'foo y 'bar z 'baz)

  (setq l '(x y z))

  (defun symbol-name-value (x)
    (list (symbol-name x) (symbol-value x)))

  (mapcar #'symbol-name-value l)

#+end_src

#+RESULTS:
| x | x   |
| y | bar |
| z | baz |

The failure is obvious in retrospect, but I don't know how to fix it.

1

The relevant function is symbol-value.

According to its documentation:

symbol-value is a function defined in C source code.

Signature
(symbol-value SYMBOL)

Documentation
Return SYMBOL's value.  Error if that is void.

Note that if lexical-binding is in effect, this returns the
global value outside of any lexical scope.

This is an example of using it on a list of variables.

(mapcar #'symbol-value '(emacs-version emacs-build-time evil-undo-system))
;;=> ("27.1 nil nil)

EDIT:

I copied your desired ouput and wrote a function which returns a string. Not that we need to use unique symbols so we avoid the possibility of running into scope problems (when a variable you want to print has the same name as the parameter of the lambda).


(defun my/tabular-values (variable-list)
  (let ((var (gensym "var")))
    (mapc `(lambda (,var)
         (thread-last (symbol-value ,var)
           (format "%S %S\n" ,var)
           (insert)))
      variable-list)))

(let ((var1 nil)
      (var2 t)
      (var3 nil))
  (my/tabular-values '(var1 var2 var3)))

;; The following is inserted into buffer after using `eval-print-last-sexp`.

var1   nil
var2   t
var3   nil
8
  • I updated my question. Thanks Feb 16 at 16:41
  • I can easily use your code to write a function that shows the values in a temp buffer. I'll wait to see if other answers come. Otherwise I'll accept your answer. Thanks. Feb 16 at 20:03
  • Your function suffers from the same problem that the function in my answer suffers: try changing var3 to var in the the let form (in both places).
    – NickD
    Feb 16 at 22:53
  • Could use a UUID in place of var in function :-) uuid | sed -e 's/-//g' -e 's/^/uuid/
    – alls0rts
    Feb 17 at 18:48
  • @NickD I don't understand. Could you elaborate? What problem was that? Feb 17 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.