Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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