I want to count the words in my buffers, but for TeX files, for instance, a lot of words (e.g., commands) shouldn't be counted. I'm aware that there are scripts that do this specifically for LaTeX, but I find them pretty inconvenient and they don't even work that well.

On the other hand, whoever wrote the syntax files for TeX already did a great job at tagging which words should be counted: the words for which Spell is enabled. Besides, using a method like this would be useful for every filetype, potentially ignoring comments and so on. And if you want to ignore more words you could just change it in your after/syntax files.

I couldn't figure out a way to do it, though. So, any ideas on how I could count only the words for which Spell is enabled?

I think this method (:%s/\<\w\{-}\>//gn) of counting the words looks like a good start, but we'd have to skip some words depending on their highlighting group.

  • 3
    I'd run some TeX to text converter in the shell, then count those words. tex.ac.uk/FAQ-toascii.html
    – VanLaser
    Commented Sep 24, 2016 at 17:53
  • @VanLaser Ah, yes, like I said, there are scripts that do that already, and Vimtex even uses one of them… I just think this method would be better if there were a way to do it. And I think it would be easier to keep the count up to date and show the results in the status line for instance.
    – dbmrq
    Commented Sep 24, 2016 at 18:11
  • I'm not sure I trust the syntax that much :) This thread may help (you or somebody that wants to test his/her skills): vi.stackexchange.com/questions/4050/…
    – VanLaser
    Commented Sep 24, 2016 at 18:31
  • 1
    @VanLaser Interesting, thanks! Maybe the syntax isn't that trustworthy out of the box, but I've been playing with it and tweaking my after/syntax file and I think it would work pretty well at this point. :)
    – dbmrq
    Commented Sep 24, 2016 at 18:39
  • Just for reference (and not what you want), I was reading a reddit post today - it mentions texcount (included with TeXLive etc.); just did a check and saw that it's the same one used by the vimtex plugin (which you mentioned above).
    – VanLaser
    Commented Sep 25, 2016 at 10:05


Your Answer

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