Skip to content

Instantly share code, notes, and snippets.

@romainl
Created January 26, 2020 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainl/2e28a2eb6c48d06d50089907e0a8c69e to your computer and use it in GitHub Desktop.
Save romainl/2e28a2eb6c48d06d50089907e0a8c69e to your computer and use it in GitHub Desktop.
Sum operator
" Print the sum of the lines covered by visual selection or motion
function! Sum(type, ...)
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
*t'>
'[,']!awk '{ sum += $0 } END { print sum }'
let &selection = sel_save
let @@ = reg_save
endfunction
nmap <silent> <key> :set opfunc=Sum<CR>g@
xmap <silent> <key> :<C-u>silent call Sum(visualmode())<CR>
" Usage:
" <key>ip Print the sum of the numbers that form the current paragraph
" below said paragraph
" vip<key> Same, for the current selection
" Example:
" 749.78
" 893.35
" 5420.70
" 2967.32
" 671.40
"
" vip<key>
"
" 749.78
" 893.35
" 5420.70
" 2967.32
" 671.40
" 10700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment