sponsor Vim development Vim logo Vim Book Ad

advanced Tip #205: Computing a sum of numbers in vim

 tip karma   Rating 14/10, Viewed by 1982 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 30, 2002 3:05      complexity:   advanced
author:   Stanislav Sitar      as of Vim:   6.0

"Sometimes you need to sum a some numbers in vim.  There *are* some plugins
"that can do the job.  But what if the numbers are not in a columns or are on
"the same line or are sacttered all across the file? You might also need to
"sum all the numbers in file that look like '1234$', or '54565 Eu'  ignoring others.
"
"There is a very simple trick, using (my favourite) command
":s
"
"First you define following function

:let g:S=0 "In global variable S we later find the result

:function! Sum(number) "The function is defined with a '!',
"so it does not complain during debugging
"when you are redefining the function
:let g:S=g:S+a:number "we accumulate the result in global variable S
:return a:number "function returns the argument, so after a :s
"command the text remains the same
:endfunction

"you can do issue those few commands from a command line,
"or create a small file and put it into your plugin directory,
"or write those few commands into a file end issue a command :so %



"how to use this little function:
"let's suppose you have a simple column of numbers like
"
"10
"20
"30
"
"you issue command like:
:let S=0
:%s/[0-9]\+/\=Sum(submatch(0))/
"the command finds the first number on the line and adds it to the S
"
"the result is displayed
:echo $S

"!!!! don't forget to do
:let g:S=0
"before use.

"you can also use \zs and \ze atoms in a regular expression to
"delimit the number, so submatch(0) returns only a number and
"the text remains unchanged after 'substitute'


"for starter on the wonderfull world of regular expressions see:
:help usr_27.txt

"for the definition of the search pattern see
:help :s
:help pattern

"for replacement strings begining with \= and special function submatch(0)see
:help sub-replace-special

"for the *ultimate* guide through the world of regular expressions see book:
"Mastering Regular Expressions
"Powerful Techniques for Perl and Other Tools
"by Jeffrey E.F. Friedl
"from O'REILLY

"the book does not write about vim, yet here you can learn that
":s command is the most powerfull command you can find in a text editor.
"(with the possible exception of :global command)

 rate this tip  Life Changing Helpful Unfulfilling 

<< Some mappings for using cscope with vim. | Highlight doubled word errors in text >>

Additional Notes

Anonymous, April 19, 2004 1:29
this tip did not help. i'm looking for some trick whereby I can get cumulative sum of a column of numbers in vim 6.1. For example

3
4
5
6

should give 3,7,12,18 as output. can you help ?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo