1

Create a new file foo.txt and type this code in it:

(+ 1 2)

Move the cursor to the end of the line (right after the close-paren) and press C-x C-e. The expression is evaluated.

Move the cursor to somewhere within the expression and press C-M-x. There is an error message: C-M-x is undefined.

Now replicate the same steps in a buffer foo.el. In foo.el both C-x C-e and C-M-x work successfully.

Why does C-x C-e work in foo.txt but C-M-x not work in foo.txt?

1
  • 1
    C-h k C-x C-e: “... found in global-map ...”
    – shynur
    Commented Mar 31, 2023 at 2:04

1 Answer 1

3

The combination of input such as C-x C-e is called key-binding, is defined in mode-specific keymaps. Text files are opened in text-mode, ELisp files are in emacs-lisp-mode (mode is usually selected by file extensions), so the key-binding that is not defined in each mode's keymap (defined as -map after the mode name) cannot be used.

Enter the C-M-x in a buffer foo.el after M-x describe-key, it shows

C-M-x runs the command eval-defun (found in emacs-lisp-mode-map)
...

saying the key binding is found(defined) in emacs-lisp-mode-map, not in text-mode-map, so if you want to use the key even for text-mode, this additional setting is required:

(define-key text-mode-map (kbd "C-M-x") 'eval-defun) ;; the command name shown by `describe-key'

See Major modes and Keymaps in the Emacs manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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