It’s long hassled me that Emacs doesn’t support word count by default. This morning I got tired of using wc
to count words and did a little search via Google. Right at the top of the list is DJ Delorie’s “Programming in Emacs Lisp”. This page teaches how to create this function, with a bug-free complete code chunk for use in your ~/.emacs
file:
;;; First version; has bugs! (defun count-words-region (beginning end) "Print number of words in the region. Words are defined as at least one word-constituent character followed by at least one character that is not a word-constituent. The buffer's syntax table determines which characters these are." (interactive "r") (message "Counting words in region ... ") ;;; 1. Set up appropriate conditions. (save-excursion (goto-char beginning) (let ((count 0)) ;;; 2. Run the while loop. (while (< (point) end) (re-search-forward "\\w+\\W*") (setq count (1+ count))) ;;; 3. Send a message to the user. (cond ((zerop count) (message "The region does NOT have any words.")) ((= 1 count) (message "The region has 1 word.")) (t (message "The region has %d words." count))))))
Do a quick byte-compile-file
to make your ~/.emacs.elc
file, restart Emacs, you are ready to count.
Or you could do it the easy way:
(defun word-count nil "Count words in buffer" (interactive)
(shell-command-on-region (point-min) (point-max) "wc -w"))
I don’t think DJ wrote this. He just hosts free documentation on his web site. The author appears to be Robert J. Chassell.
Thanks lazyweb (in the form of Peter), I’ll try that.
Good point, Anthony, I didn’t think to check actual authorship. It’s not very obvious, as it is not on the front page; in fact, the Thank You page seems to be the first instance of the author’s name. Perhaps he didn’t include an… in the source.
This raises an interesting point about writing technical content. I assumed the work was by the person with the nearest name, that is, the name of the website. Why? Because it is written in an informal, first person plural (we) style. I feel my reaction is natural — it’s speaking to “me” about “we” from DJ’s site.
Anyway, thanks for the correction.
I’m sick and tired of using “wc” and I can’t even use it when I’m using Emacs in Windows, so I wrote up a one-liner that uses the how-many function: http://www.neverfriday.com/sweetfriday/2008/06/emacs-tip-word-counting-with-a.html
i know its wrong but
(length ‘( your document in here ))
then C-x C-e
Or, if you wanna make it really easy, just go to the start of the region you want to count, then C-space (then move to end of region) M-| Enter (then type) wc -w Enter
and it tells you.
This has also been covered on the Emacs Wiki: http://www.emacswiki.org/emacs/WordCount
[…] to Karsten Wade and the discussion on this page. There are many word count alternatives in the Emacs Wiki, and there's also a word-count-mode that […]