Counting Words

For years, I’ve used this bit of Elisp that I stole from DJCB at Emacs-Fu. The latest versions of Emacs now have this functionality built in so I thought I should move on. The new functionality is bound to the convenient key sequence 【Meta+=】 giving me even more reason to change.

There’s a problem though. The semantics of the command are strange. By default, it counts the lines, words, and characters of the current region even if the region is not active. That’s never what I want and I can’t think of a reasonable use case for such behavior. If the region is active, you get the desired behavior. If you want the entire buffer you can get it by specifying a prefix argument. I adopted the practice of typing 【Meta+0 Meta+=】 to count the words in the current buffer.

That worked fine but it was infuriating. Why should I have to add a keystroke to work around a default that doesn’t make any sense? I decided I’d advise count-words-region to do the sensible thing. Once I started researching the code, I discovered that there’s already a function that does the right thing: count-words. I rebound 【Meta+=】 to count-words and everything worked the way I expected.

The story has a happy ending but it still bothers me a bit. The default behavior was so unexpected that I was suspicious that I’d somehow caused the old binding. There’s nothing in my init.el that does it and when I ran

M-x bug-hunter-init-file RET (string-suffix-p "count-words-region" (describe-key-briefly (kbd "M-=") nil) t)

it told me that 【Meta+=】 is bound to count-words-region even when Emacs is invoked as

emacs -Q

so it appears that the binding is on purpose.

If I’ve overlooked something or you have some guidance on why this behavior is the default, please leave a comment.

This entry was posted in General and tagged . Bookmark the permalink.