Created
January 26, 2020 18:18
-
-
Save romainl/2e28a2eb6c48d06d50089907e0a8c69e to your computer and use it in GitHub Desktop.
Sum operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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