5

The following in my .emacs file does not produced the desired result of showing me the word count for the current buffer (it "does nothing"):

(global-set-key (kbd "<f9>") (kbd "C-u M-="))

Is this because M-= is an interactive function? Would I need a function to do this?

1
  • 1
    Normally you bind keys to functions by name (i.e., 'count-words), rather than to other keybindings. You can find the name of a function bound to a key with the command C-h k. i.e., C-h k M-= will tell you the function name is count-words-region
    – Tyler
    Oct 19, 2017 at 14:56

2 Answers 2

6

simply

(global-set-key (kbd "<f9>") 'count-words)

7

Your binding does successfully run the keyboard macro you've specified.

The problem is that the resulting message does not remain in the echo area long enough for you to see it, which is a side-effect of invoking the command via a keyboard macro (you will see the same problem if you record the macro with <f3>C-uM-=<f4>).

If you open the messages buffer with C-he and then use your binding again, you'll see the message being generated.

Is this because M-= is an interactive function?

Your terminology is mixed up. M-= is not a function. "M-=" is a keyboard macro representing the key sequence M-= which is bound to the interactive function count-words-region.

(Note also that you cannot bind keys to non-interactive functions.)

Would I need a function to do this?

To get the behaviour you're looking for, yes (and djangoliv has provided the appropriate answer for that).

Your Answer

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

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