:r!awk '{sum+=$6} END {print "Total: "sum}' %
说明:
:r ........... read (put result in this file)
! ............ external command
awk .......... external tool
{sum+=$6} .... sixth field (awk considers spaces as field separator)
END .......... at the end
{print "Total: "sum} --> string "Total: " plus your result
% ............ current file
我一直在尝试在这里工作的功能:
" This function requires you select the numbers
fun! SumVis()
try
let l:a_save = @a
norm! gv"ay
let @a = substitute(@a,'[^0-9. ]','+','g')
exec "norm! '>o"
exec "norm! iTotal \<c-r>=\<c-r>a\<cr>"
finally
let @a = l:a_save
endtry
endfun
vnoremap <leader>s :<C-u>call SumVis()<cr>
使用上面包含的地图,在加载该函数之后,您要做的就是选择要求和的数字,并用<leader>s
其对所选区域求和。
功能说明:
它使用try/finally/endtry
拉伸来捕获错误。
let l:a_save = @a .......... if whe have register 'a' we save it temporarelly
norm! gv"a ................................... gv --> reselects and captures selection to 'register a'
let @a = substitute(@a,'[^0-9. ]','+','g') .... removes all but numbers, dots and spaces from 'register a' and puts '+' among the numbers
exec "norm! '>o" ............................. opens new line bellow selection. see :h '>
exec "norm! iTotal: \<c-r>=\<c-r>a\<cr>" ...... insert "Total: " plus 'expression register result
let @a = l:a_save ............................. restores original 'a' register content
如果要尝试使用此功能,请执行以下操作:在浏览器中复制此功能,然后在vim上运行此命令,:@+
这将使您可以:call SumVis()
正常使用。
:@+ ......... loads `+` register making the function avaiable
它需要您使用ctrl+ 进行可视块选择v,取消选择并最终调用该函数。或者,您可以使用建议的地图,它本身会在计算之前删除选择。