In Emacs Lisp, how do I check if a variable is defined?

mike picture mike · Apr 16, 2009 · Viewed 21.9k times · Source

In Emacs Lisp, how do I check if a variable is defined?

Answer

dfa picture dfa · Apr 16, 2009

you may want boundp: returns t if variable (a symbol) is not void; more precisely, if its current binding is not void. It returns nil otherwise.

  (boundp 'abracadabra)          ; Starts out void.
  => nil

  (let ((abracadabra 5))         ; Locally bind it.
    (boundp 'abracadabra))
  => t

  (boundp 'abracadabra)          ; Still globally void.
  => nil

  (setq abracadabra 5)           ; Make it globally nonvoid.
  => 5

  (boundp 'abracadabra)
  => t