I want to create a command and a keybind to scan with tidy a full buffer. The keybinding has to be active only when editing XML files.

In my ~/.emacs.d/init.el file I've added the following lines:

(defun tidy-xml ()
  "Tidies the XML content in the buffer using `tidy'"
    (point-min) (point-max)
    "tidy -i -w 72 -q -xml"

(define-key nxml-mode-map "\C-c t" 'tidy-xml)

But when I open Emacs it returns the following warning:

Warning (initialization): An error occurred while loading ‘/home/jim/.emacs.d/init.el’:

Symbol's value as variable is void: nxml-mode-map

I'm not able to see where the error is. Can you give me a hand?

  • The error has actually nothing to do with the shell command. – Tobias Oct 28 '19 at 19:54
  • The question appears to be a duplicate. Search the site for "variable is void" to see similar questions. – Drew Oct 28 '19 at 20:02

The variable nxml-mode-map is defined in the library nxml-mode. This library is not yet loaded when you try to execute the form (define-key nxml-mode-map "\C-c t" 'tidy-xml).
Replace that line with:

(eval-after-load "nxml-mode"
  (lambda ()
    (define-key nxml-mode-map "\C-c t" 'tidy-xml)))

That will delay the execution of the form contained in the lambda until the library nxml-mode is loaded.

| improve this answer | |
  • 1
    Or, since 24.4, use with-eval-after-load and then you can drop the (lambda () ...) wrapper. – phils Oct 28 '19 at 23:31

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