Counting Words in Some Buffers Only

It is often, but not always, nice to have a running count of the words in a buffer. Most usually, this is convenient for writers or students who need to keep track of word count in a paper or book. One way of doing that is the excellent wc-mode that I wrote about previously.

I like to keep a running count when I’m writing a blog post but almost never otherwise. I have a key sequence to turn it on and off so it’s easy to enable it for the buffers that I need it in. Still, it got to be a pain to turn it on every time I wrote a post so I added a bit of Elisp to enable it when I open a file in /Users/jcs/org/blog, which is where my blog posts live.

Here’s the code, which as you can see, is merely part of the use-package call.

(use-package wc-mode
  :ensure t
  (add-hook 'org-mode-hook
            (lambda () (when (and (buffer-file-name)
                                  (string= (file-name-directory (buffer-file-name))
                         (wc-mode 1))))
  :bind ("H-=" . wc-mode)
  (wc-mode nil))

It’s important to add the hook in the :init section, otherwise it won’t work until wc-mode is enabled manually at least once.

This is pretty trivial and I wasn’t going to write about it but I saw a question on reddit asking how to do something similar so I thought my solution might be useful to someone else.

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