I have a list of integers, where each is on a separate line. I'm trying to find a quick way to sum them up directly in VIM without having to write a script that does it for me.
Here's a fun (albeit slightly hacky) answer, although I believe it's the shortest answer you'll get:
:%norm Dj@"<C-v><C-a><cr> p
<C-v> means ctrl-v, and
<C-a> means ctrl-a)
It works by doing the following on every line:
- Deleting it (into the unnamed register)
- Running the unnamed register as a count, and
- Incrementing the next number by that much.
Then, on the last line, there isn't a next number to increment, so it breaks. However, we have the current sum in the unnamed register, so we can just paste it with
If you don't want a bunch of blank lines, you could modify the last line to
Unfortunately, this only works with positive integers.