News for hackers. Stuff that matters.
Re:My Favorite Vim Oneliners for Text Manipulation by agileAlligator on Saturday August 05, 2023 @09:21PM (#37018019)
> simple vim one liner which allows you to count the words in the file

> :%s/\w\+/\=submatch(0)+1/g | echo line('$')

Yeah, no. Isn't there a plugin to just display word count in the corner somewhere automatically?

Re:My Favorite Vim Oneliners for Text Manipulation by hyperhopper on Saturday August 05, 2023 @09:31PM (#37018079)
I never want to know the count of words once. If relevant, I want to _always_ see the count.
Re:My Favorite Vim Oneliners for Text Manipulation by blueside on Saturday August 05, 2023 @09:43PM (#37018156)
`g ctrl-g` will do it, but i can barely remember that, so i end up doing `!wc %`
Re:My Favorite Vim Oneliners for Text Manipulation by zaphod420 on Saturday August 05, 2023 @09:47PM (#37018177)
LOL. "simple". I really want to learn vim and be really good at it. I keep trying different tutorials, and I'm starting to be more comfortable with it, but I just keep using sublime text as my main editing tool because it's just easier.
Re:My Favorite Vim Oneliners for Text Manipulation by linhns on Sunday August 06, 2023 @12:36AM (#37019016)
Yep. I love vim but for big projects I have to use VSCode as I need the extensions.
Re:My Favorite Vim Oneliners for Text Manipulation by TonyStr on Sunday August 06, 2023 @02:17AM (#37019448)
Curious about which extensions are keeping you in vscode?
Re:My Favorite Vim Oneliners for Text Manipulation by foo42 on Sunday August 06, 2023 @03:07AM (#37019626)
not OP but for me it's Live Share for remote pairing. I can barely function without vim-mode enabled but have never fully made the jump to (n)vim.
Re:My Favorite Vim Oneliners for Text Manipulation by predmijat on Sunday August 06, 2023 @03:38AM (#37019769)
There’s a ViM plugin for VS Code, of course :)
Re:My Favorite Vim Oneliners for Text Manipulation by cerved on Sunday August 06, 2023 @04:05AM (#37019901)
which sadly is a bit lacking
Re:My Favorite Vim Oneliners for Text Manipulation by satvikpendem on Sunday August 06, 2023 @05:53AM (#37020456)
VSCode-Neovim (not VSCodeVim) is a fully working Neovim instance inside VSCode. Even plugins work.
Re:My Favorite Vim Oneliners for Text Manipulation by arp242 on Saturday August 05, 2023 @10:06PM (#37018276)
> Isn't there a plugin to just display word count in the corner somewhere automatically?

You can add it to your statusline (or tabline, ruler) with the wordcount() function:

  :let &statusline ..= ' words: %{wordcount().words}'
---

Aside: that "one-liner" doesn't "count the words in the file" at all? It just replaces all words with "1" and echos the last line number. I'm confused because this seems so wrong that I may be missing something? Unless the "67 substitutions on 5 lines" message is intended as the "word count", in which case I submit that 1) this is the ugliest thing I've ever seen, and 2) the author of this article doesn't know Vim all that well (there are so many better/easier ways to do this).

Re:My Favorite Vim Oneliners for Text Manipulation by anorakoverflow on Sunday August 06, 2023 @02:10AM (#37019425)
Also, if I’m seeing this correctly, it only recognises ASCII “words” (a–z, A–Z, 0–9, _), so it’s not even accurate and will over-count words with accents, umlauts, etc.
Re:My Favorite Vim Oneliners for Text Manipulation by akovaski on Sunday August 06, 2023 @12:24PM (#37023598)
It actually increments each word it finds, so while most words will turn to "1", "356" will become "357". I have to imagine this ending up in the blog is the result of a copy-paste mistake, a LLM hallucination, or something similar.

Others have commented on built-in vim functionality for counting words, but assuming the author copy-pasted a similar looking command, they may have intended something like:

    :%s/\w\+//gn
Knowing that the n flag will return a count instead of doing the substitute may be useful to know, but I personally don't need a search count very often.

Knowing that you can execute a vimscript expression on each search match using \= is also cool and I was unaware of such functionality. https://vimhelp.org/change.txt.html#sub-replace-expression

Re:My Favorite Vim Oneliners for Text Manipulation by arp242 on Sunday August 06, 2023 @05:39PM (#37027103)
> It actually increments each word it finds

Did you try it? Because that's not what it does. submatch(0) expands to the first submatch, which is the entire thing that's matched, and +1 adds 1 to this, which will a;ways result in "1" here since any string that doesn't start with a digit will be type-coerced to 0.

Anyway, /n being intended sounds likely; I forgot that would report the number of matches. Still very odd how that got morphed to what's in the article.

Re:My Favorite Vim Oneliners for Text Manipulation by akovaski on Monday August 07, 2023 @01:24AM (#37030469)
> Did you try it?

Yes

> Because that's not what it does. [...]

I don't see how your description significantly differs from my description.

Re:My Favorite Vim Oneliners for Text Manipulation by _madmax_ on Saturday August 05, 2023 @11:45PM (#37018810)
That one made me cringe hard, the guy would just not use the built-in function or have the decency to sling back into the shell with :! wc -w filename.whatever.

Insanity described in that post.

Re:My Favorite Vim Oneliners for Text Manipulation by ashton314 on Sunday August 06, 2023 @12:43AM (#37019047)
M-x count-words RET ;-)
Re:My Favorite Vim Oneliners for Text Manipulation by ISL on Sunday August 06, 2023 @01:28AM (#37019222)
wc
Re:My Favorite Vim Oneliners for Text Manipulation by cerved on Sunday August 06, 2023 @04:04AM (#37019892)
I prefer using the standard Unix utilities

  :%!wc