[20131125]使用vim做合计计算.txt

简介: [20131125]使用vim做合计计算.txt 工作需要,需要使用vim做合计计算,把一列的数据累加,输出结果,到www.vim.org网站检索,发现: http://www.
[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脚本如下,点击(此处)折叠或打开

  1. \" vim:filetype=vim foldmethod=marker textwidth=78
  2. \" ==========================================================================
  3. \" File: visSum.vim (global plugin)
  4. \" Last Changed: 2012-07-17
  5. \" Maintainer: Erik Falor
  6. \" Version: 1.0
  7. \" License: Vim License
  8. \"
  9. \" A great big thanks to Christian Mauderer for providing a patch for
  10. \" floating-point support!
  11. \"
  12. \" ________ __ __
  13. \" /_ __/ /_ ____ _____ / /_______/ /
  14. \" / / / __ \\/ __ `/ __ \\/ //_/ ___/ /
  15. \" / / / / / / /_/ / / / / ,
  16. \" /_/ /_/ /_/\\__,_/_/ /_/_/|_/____(_)
  17. \"
  18. \" This plugin will work whether or not your Vim is compiled with support for
  19. \" floating-point numbers. If your Vim doesn\'t has(\'float\'), we\'ll just
  20. \" ignore whatever comes after the decimal point.
  21. \"
  22. \" Due to the way Vim parses floating-point numbers, the only valid separtator
  23. \" between the whole and fractional parts of the number is a period. Vim
  24. \" won\'t accept a comma, even if that\'s your locale\'s preference. This
  25. \" plugin follows that convention.
  26. \" ==========================================================================
  27. \" Exit quickly if the script has already been loaded
  28. let s:this_version = \'1.0\'
  29. if exists(\'g:loaded_visSum\') && g:loaded_visSum == s:this_version
  30. finish
  31. endif
  32. let g:loaded_visSum = s:this_version
  33. \"Mappings {{{
  34. \" clean up existing key mappings upon re-loading of script
  35. if hasmapto(\'SumNum\')
  36. nunmap \\su
  37. vunmap \\su
  38. nunmap SumNum
  39. vunmap SumNum
  40. endif
  41. \" Key mappings
  42. nmap su SumNum
  43. vmap su SumNum
  44. if has(\'float\')
  45. \" Call the floating-point version of the function
  46. nmap
  47. vmap
  48. command! -nargs=? -range -register VisSum call SumNumbers_Float(\"\")
  49. else
  50. \" Call the integer version of the function
  51. nmap
  52. vmap
  53. command! -nargs=? -range -register VisSum call SumNumbers_Int(\"\")
  54. endif
  55. \"}}}
  56. function! SumNumbers_Float(...) range \"{{{
  57. let l:sum = 0.0
  58. let l:cur = \"\"
  59. if visualmode() =~ \'\\cv\'
  60. let y1 = line(\"\'
  61. let y2 = line(\"\'>\")
  62. while y1
  63. let l:cur = matchstr( getline(y1), \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
  64. if l:cur == \"\"
  65. let l:cur = \"0\"
  66. endif
  67. let l:sum += eval(l:cur)
  68. let y1 += 1
  69. endwhile
  70. elseif visualmode() == \"\\\"
  71. let y1 = line(\"\'
  72. let y2 = line(\"\'>\")
  73. \"modify by lfree
  74. let x1 = col(\"\'
  75. let len = col(\"\'>\") - x1 -1
  76. if len == 0
  77. let let = 1
  78. endif
  79. while y1
  80. \"let line = getline(y1)
  81. \"let chunk = strpart(line, x1, len)
  82. \"let l:cur = matchstr( chunk, \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
  83. let l:cur = matchstr( strpart(getline(y1), x1, len ), \'-\\?\\d\\+\\(\\.\\d\\+\\)\\?\' )
  84. if l:cur == \"\"
  85. let l:cur = \"0\"
  86. endif
  87. let l:sum += eval(l:cur)
  88. let y1 += 1
  89. endwhile
  90. else
  91. echoerr \"You must select some text in visual mode first\"
  92. return
  93. endif
  94. \"Drop the fractional amount if it\'s zero
  95. \"TODO: When scientific notation is supported, this will need to be changed
  96. if abs(l:sum) == trunc(abs(l:sum))
  97. let l:sum = float2nr(l:sum)
  98. endif
  99. redraw
  100. \"echo \"sum = \" l:sum
  101. \"save the sum in the variable b:sum, and optionally
  102. \"into the register specified by the user
  103. \"let b:sum = l:sum
  104. \"if a:0 == 1 && len(a:1) > 0
  105. \" execute \"let @\" . a:1 . \" = printf(\'%g\', b:sum)\"
  106. \"endif
  107. let @p = \"sum = \" . string(l:sum)
  108. endfunction \"}}}
  109. function! SumNumbers_Int(...) range \"{{{
  110. let l:sum = 0
  111. let l:cur = 0
  112. if visualmode() =~ \'\\cv\'
  113. let y1 = line(\"\'
  114. let y2 = line(\"\'>\")
  115. while y1
  116. let l:cur = matchstr( getline(y1), \'-\\{-}\\d\\+\' )
  117. let l:sum += l:cur
  118. let y1 += 1
  119. endwhile
  120. elseif visualmode() == \"\\\"
  121. let y1 = line(\"\'
  122. let y2 = line(\"\'>\")
  123. let x1 = col(\"\'
  124. let len = col(\"\'>\") - x1
  125. while y1
  126. let line = getline(y1)
  127. let chunk = strpart(line, x1, len)
  128. let l:cur = matchstr( strpart(getline(y1), x1, len ), \'-\\{-}\\d\\+\' )
  129. let l:sum += l:cur
  130. let y1 += 1
  131. endwhile
  132. else
  133. echoerr \"You must select some text in visual mode first\"
  134. return
  135. endif
  136. redraw
  137. echo \"sum = \" l:sum
  138. \"save the sum in the variable b:sum, and optionally
  139. \"into the register specified by the user
  140. let b:sum = l:sum
  141. if a:0 == 1 && len(a:1) > 0
  142. execute \"let @\" . a:1 . \" = b:sum\"
  143. endif
  144. endfunction \"}}}
  145. \"Test Data \"{{{
  146. \" The winter of \'49
  147. \" The Summer of \'48
  148. \" 123
  149. \" 123
  150. \"1.5 123
  151. \"-2 123.0
  152. \"3.1 123.1
  153. \"-4.2 123.2
  154. \"+5.9 123.3
  155. \"-6.0
  156. \"7
  157. \"8
  158. \"8.2
  159. \"9.
  160. \"10.
  161. \"-11.
  162. \"+12.
  163. \"
  164. \"The pedant in me wants to make these numbers work as well;
  165. \"but if I\'ve learned anything, it\'s that the perfect is the
  166. \"enemy of the good.
  167. \"Avogadro 6.0221415e23
  168. \"Planck 6.626068E-34 m^2 kg / s
  169. \"Borh Radius 5.2917721092e鈭?1 m
  170. \"}}}



--说明:
1.SumNumbers_Int 我没有修改,正常都支持浮点,不会调用这个模块.
2.使用V模式一般没有问题
3.使用ctrl+v(windows下使用ctrl+Q),截取矩形区域,最好从上向下选取.并且最后一行的截取长度>=其他行,没有找到一个函数确定选取
  的右边界.
4.修改了输出,原来在提示行输出,我修改为保存在register p,然后直接在下面输出结果.
5.使用很简单,选择区域,按\su输出.

--另外编辑的文本要求不受边界的影响,我记得以前看过有这方面的功能,检索查询发现,设置:
set virtualedit=all

这样就可以任意移动,不受回车换行的限制,使用鼠标定位更快一些.



目录
相关文章
|
SQL 开发工具 Perl
[20180417]vim小技巧.txt
[20180417]vim小技巧.txt --//今天调试plsql,发现跟踪到的sql语句在跟踪文件是在一行的,开始以为是开发写成这样,实际上PL/SQL写的语句 --//到跟踪就变成一行,这样就太长,这样为了更好观察我必须设置wrap,执行如下:set wr...
1054 0
|
开发工具 数据库管理 关系型数据库
[20180211]在vim中使用bc进行各种运算.txt
[20180211]在vim中使用bc进行各种运算.txt --//别人的建议,完善一下在vim调用bc进行各种运算. --//我以前定义如下,完成计算,10,16进制转换.
1055 0
|
开发工具 Windows
[20131125]vim的bccalc.vim插件有关问题以及10,16进制转换的简单方法.txt
[20131125]vim的bccalc.vim插件有关问题以及10,16进制转换的简单方法.txt链接: http://www.vim.org/scripts/script.
841 0
|
开发工具 存储
[20130425]使用vim编辑没有保存的恢复.txt
今天使用vim编辑一个存储过程的时候,正好接听一个电话,不知道什么回事,点击X,选择“否”,导致将近半个小时的工作没有保存下 来。 我冷静下来,想起使用vim会在它的dir目录保存为"原来文件名.swp".我的vim设置如下: set dir=d:\\temp缺省如果不设置,保存在当前目录。
995 0
|
开发工具 Windows
[20121207]vim中使用bc做10与16进制计算.txt
[20121207]vim中使用bc做10与16进制计算.txt工作中需要经常做10与16进制的转换,我记得以前有一个插件与vim结合可以实现简单的计算功能,比在平时输入是时输入ctrl+r=计算式(在插入模式)要方便一些,我个人计算时经常使用。
755 0
|
开发工具
txt.vim : Universal syntax script for all txt docs, logs and other types
http://vim.sourceforge.net/scripts/script.php?script_id=1532 增加txt文件的显示效果,如图: http://www.
703 0
|
开发工具
在vim中使用bc进行算术计算
今天同事想导入一个文本文件进入execl进行算术运算,我正好看了一下,好像使用bc更快一些。使用vim编辑好文件,然后执行cat foo | bc ,就可以得到结果。
835 0
|
Unix Shell 开发工具
[20170616]vim 8.0的安装.txt
[20170616]vim 8.0的安装.txt --//晚上开始在自己的家里的机器上安装vim 8.0 for windows,事先做了备份,但是还是遇到许多问题,都是定制安装相关的问题,自己做 --//一个记录: --//里面的操作都是定制化的,可能不适合大家,我自己仅仅作为一个记录.
1038 0
|
20天前
|
Linux 开发工具
Linux的学习之路:5、粘滞位与vim
Linux的学习之路:5、粘滞位与vim
40 0