8

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.

Rich
  • 31,891
  • 3
  • 72
  • 139
dbmrq
  • 857
  • 6
  • 17
  • 3
    I'd run some TeX to text converter in the shell, then count those words. http://www.tex.ac.uk/FAQ-toascii.html – VanLaser Sep 24 '16 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 Sep 24 '16 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): http://vi.stackexchange.com/questions/4050/how-to-search-for-pattern-in-certain-syntax-regions – VanLaser Sep 24 '16 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 Sep 24 '16 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 Sep 25 '16 at 10:05
  • @VanLaser I wrote that post, actually, haha! After reading your comment I decided to take another look at texcount and came up with that. Then I thought "cool, I'll share this on reddit". I'm still curious about this approach, though. – dbmrq Sep 25 '16 at 15:58
  • LOL, I should've looked at the poster's nickname at least once ... :) Well, since you seem to like dabbling with Vim syntax, perhaps we'll see an answer from you, later on ;) – VanLaser Sep 25 '16 at 16:08

0 Answers0