[20131125]使用vim做合计计算.txt
工作需要,需要使用vim做合计计算,把一列的数据累加,输出结果,到www.vim.org网站检索,发现:
http://www.vim.org/scripts/script.php?script_id=1932
visSum.vim插件比较合适.结果测试发现一些问题,结果自己花了一个下午晚上的时间测试修改,有点浪费时间.
主要原因:
1.不熟悉vim里面的函数,以及命令格式.
2.vim scripts 不知道如何调试,我最后只能单步跟踪执行.
3.作者使用折叠功能,自己还花一点时间学习这方面的知识.
最后脚本修改如下:
VimSum.vim脚本如下,点击(此处)折叠或打开
-
\" vim:filetype=vim foldmethod=marker textwidth=78
-
\" ==========================================================================
-
\" File: visSum.vim (global plugin)
-
\" Last Changed: 2012-07-17
-
\" Maintainer: Erik Falor
-
\" Version: 1.0
-
\" License: Vim License
-
\"
-
\" A great big thanks to Christian Mauderer for providing a patch for
-
\" floating-point support!
-
\"
-
\" ________ __ __
-
\" /_ __/ /_ ____ _____ / /_______/ /
-
\" / / / __ \\/ __ `/ __ \\/ //_/ ___/ /
-
\" / / / / / / /_/ / / / / ,
-
\" /_/ /_/ /_/\\__,_/_/ /_/_/|_/____(_)
-
\"
-
\" This plugin will work whether or not your Vim is compiled with support for
-
\" floating-point numbers. If your Vim doesn\'t has(\'float\'), we\'ll just
-
\" ignore whatever comes after the decimal point.
-
\"
-
\" Due to the way Vim parses floating-point numbers, the only valid separtator
-
\" between the whole and fractional parts of the number is a period. Vim
-
\" won\'t accept a comma, even if that\'s your locale\'s preference. This
-
\" plugin follows that convention.
-
\" ==========================================================================
-
-
\" Exit quickly if the script has already been loaded
-
let s:this_version = \'1.0\'
-
if exists(\'g:loaded_visSum\') && g:loaded_visSum == s:this_version
-
finish
-
endif
-
let g:loaded_visSum = s:this_version
-
-
\"Mappings {{{
-
\" clean up existing key mappings upon re-loading of script
-
if hasmapto(\'SumNum\')
-
nunmap \\su
-
vunmap \\su
-
nunmap SumNum
-
vunmap SumNum
-
endif
-
-
\" Key mappings
-
nmap su SumNum
-
vmap su SumNum
-
-
if has(\'float\')
-
\" Call the floating-point version of the function
-
nmap
-
vmap
-
command! -nargs=? -range -register VisSum call SumNumbers_Float(\"\")
-
else
-
\" Call the integer version of the function
-
nmap
-
vmap
-
command! -nargs=? -range -register VisSum call SumNumbers_Int(\"\")
-
endif
-
\"}}}
-
-
function! SumNumbers_Float(...) range \"{{{
-
let l:sum = 0.0
-
let l:cur = \"\"
-
-
if visualmode() =~ \'\\cv\'
-
let y1 = line(\"\'
-
let y2 = line(\"\'>\")
-
while y1
-
let l:cur = matchstr( getline(y1), \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
-
if l:cur == \"\"
-
let l:cur = \"0\"
-
endif
-
let l:sum += eval(l:cur)
-
let y1 += 1
-
endwhile
-
elseif visualmode() == \"\\\"
-
let y1 = line(\"\'
-
let y2 = line(\"\'>\")
-
\"modify by lfree
-
let x1 = col(\"\'
-
let len = col(\"\'>\") - x1 -1
-
if len == 0
-
let let = 1
-
endif
-
while y1
-
\"let line = getline(y1)
-
\"let chunk = strpart(line, x1, len)
-
\"let l:cur = matchstr( chunk, \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
-
let l:cur = matchstr( strpart(getline(y1), x1, len ), \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
-
if l:cur == \"\"
-
let l:cur = \"0\"
-
endif
-
let l:sum += eval(l:cur)
-
let y1 += 1
-
endwhile
-
else
-
echoerr \"You must select some text in visual mode first\"
-
return
-
endif
-
-
\"Drop the fractional amount if it\'s zero
-
\"TODO: When scientific notation is supported, this will need to be changed
-
if abs(l:sum) == trunc(abs(l:sum))
-
let l:sum = float2nr(l:sum)
-
endif
-
-
redraw
-
\"echo \"sum = \" l:sum
-
\"save the sum in the variable b:sum, and optionally
-
\"into the register specified by the user
-
\"let b:sum = l:sum
-
\"if a:0 == 1 && len(a:1) > 0
-
\" execute \"let @\" . a:1 . \" = printf(\'%g\', b:sum)\"
-
\"endif
-
let @p = \"sum = \" . string(l:sum)
-
endfunction \"}}}
-
-
function! SumNumbers_Int(...) range \"{{{
-
let l:sum = 0
-
let l:cur = 0
-
-
if visualmode() =~ \'\\cv\'
-
let y1 = line(\"\'
-
let y2 = line(\"\'>\")
-
while y1
-
let l:cur = matchstr( getline(y1), \'-\\{-}\\d\\+\' )
-
let l:sum += l:cur
-
let y1 += 1
-
endwhile
-
elseif visualmode() == \"\\\"
-
let y1 = line(\"\'
-
let y2 = line(\"\'>\")
-
let x1 = col(\"\'
-
let len = col(\"\'>\") - x1
-
while y1
-
let line = getline(y1)
-
let chunk = strpart(line, x1, len)
-
let l:cur = matchstr( strpart(getline(y1), x1, len ), \'-\\{-}\\d\\+\' )
-
let l:sum += l:cur
-
let y1 += 1
-
endwhile
-
else
-
echoerr \"You must select some text in visual mode first\"
-
return
-
endif
-
redraw
-
echo \"sum = \" l:sum
-
\"save the sum in the variable b:sum, and optionally
-
\"into the register specified by the user
-
let b:sum = l:sum
-
if a:0 == 1 && len(a:1) > 0
-
execute \"let @\" . a:1 . \" = b:sum\"
-
endif
-
endfunction \"}}}
-
-
\"Test Data \"{{{
-
\" The winter of \'49
-
\" The Summer of \'48
-
\" 123
-
\" 123
-
\"1.5 123
-
\"-2 123.0
-
\"3.1 123.1
-
\"-4.2 123.2
-
\"+5.9 123.3
-
\"-6.0
-
\"7
-
\"8
-
\"8.2
-
\"9.
-
\"10.
-
\"-11.
-
\"+12.
-
\"
-
\"The pedant in me wants to make these numbers work as well;
-
\"but if I\'ve learned anything, it\'s that the perfect is the
-
\"enemy of the good.
-
\"Avogadro 6.0221415e23
-
\"Planck 6.626068E-34 m^2 kg / s
-
\"Borh Radius 5.2917721092e鈭?1 m
-
\"}}}
--说明:
1.SumNumbers_Int 我没有修改,正常都支持浮点,不会调用这个模块.
2.使用V模式一般没有问题
3.使用ctrl+v(windows下使用ctrl+Q),截取矩形区域,最好从上向下选取.并且最后一行的截取长度>=其他行,没有找到一个函数确定选取
的右边界.
4.修改了输出,原来在提示行输出,我修改为保存在register p,然后直接在下面输出结果.
5.使用很简单,选择区域,按\su输出.
--另外编辑的文本要求不受边界的影响,我记得以前看过有这方面的功能,检索查询发现,设置:
set virtualedit=all
这样就可以任意移动,不受回车换行的限制,使用鼠标定位更快一些.