title: Vim Scripting - call function once for all selected lines
body: <p>So I have a text file like this:</p>
  
  <pre><code>Item a: &lt;total&gt;
    Subitem: 10 min
    Subitem 2: 20 min
  </code></pre>
  
  <p>I'd like to replace <code>&lt;total&gt;</code> with the total of 10 and 20. Right now I'm doing it with the following functions:</p>
  
  <pre><code>let g:S = 0  "result in global variable S
  function! Sum(number)
    let g:S = g:S + a:number
    return a:number
  endfunction
  
  function! SumSelection()
    let g:S=0
    '&lt;,'&gt;s/\d\+/\=Sum(submatch(0))/g
    echo g:S
  endfunction
  
  vnoremap &lt;s-e&gt; call SumSelection()&lt;cr&gt;
  </code></pre>
  
  <p><code>Sum</code> gets the sum of numbers passed in, <code>SumSelection</code> calls sum over all the numbers in selected lines, and (supposedly) Shift+e calls SumSelection in visual mode (or whatever you choose to call it.)</p>
  
  <p>Problem is, when I hit Shift+e when I have some lines selected, instead of <code>:call SumSelection()</code> I really get <code>:'&lt;,'&gt;call SumSelection()</code> which means the function gets called once per selected line. No good, right? So as far as I can tell there's no way around this. What can I do to get the function to:</p>
  
  <ol>
  <li>be only called once</li>
  <li>maybe total these in a more efficient way</li>
  </ol>