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

[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 <= y2
  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(\"\'<\") - 1
  75. let len = col(\"\'>\") - x1 -1
  76. if len == 0
  77. let let = 1
  78. endif
  79. while y1 <= y2
  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 <= y2
  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(\"\'<\") - 1
  124. let len = col(\"\'>\") - x1
  125. while y1 <= y2
  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

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



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/267265/viewspace-777463/,如需转载,请注明出处,否则将追究法律责任。

user_pic_default.png
请登录后发表评论 登录
全部评论
<%=items[i].createtime%>

<%=items[i].content%>

<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%=items[i].items.items[j].createtime%> 回复

<%=items[i].items.items[j].username%>   回复   <%=items[i].items.items[j].tousername%><%=items[i].items.items[j].content%>

<%}%> <%if(items[i].items.total > 5) { %>
还有<%=items[i].items.total-5%>条评论 ) data-count=1 data-flag=true>点击查看
<%}%>
<%}%> <%}%>

转载于:http://blog.itpub.net/267265/viewspace-777463/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用以下命令来输入这些内容: 1. 打开终端并输入vim命令 2. 按下i键进入插入模式 3. 输入“Learn Vim. Vim Example Line1.”并按下Enter键 4. 输入“Vim Example Line 2”并按下Enter键 5. 输入“Vim Example Line 3”并按下Enter键 6. 输入“Vim Example Line 4”并按下Enter键 7. 输入“Vim Example Line 5”并按下Enter键 8. 按下Esc键退出插入模式 9. 输入:wq并按下Enter键保存并退出Vim编辑器 ### 回答2: 使用Vim编辑器输入以下内容的最少操作,可以按照以下步骤进行: 1. 打开Vim编辑器。 2. 进入插入模式,按下大写字母 i,光标跳到第一行的行首。 3. 输入 "Learn Vim." 字符串。 4. 按下回车键,新起一行。 5. 输入 "Vim Example Line1." 字符串。 6. 按下回车键,新起一行。 7. 输入 "Vim Example Line2" 字符串。 8. 按下回车键,新起一行。 9. 输入 "Vim Example Line3." 字符串。 10. 按下回车键,新起一行。 11. 输入 "Vim Example Line4." 字符串。 12. 按下回车键,新起一行。 13. 输入 "Vim Example Line5." 字符串。 14. 按下回车键。 15. 按下大写字母 ESC 键,退出插入模式。 经过以上操作,你可以使用Vim编辑器输入所需的内容。 ### 回答3: 使用Vim编辑器输入以下内容的最少操作,可以按照以下步骤进行: 1. 打开终端,输入`vim`来启动Vim编辑器。 2. 按下`i`键,进入插入模式,开始输入内容。 3. 输入"Learn Vim. Vim Example Line1."(包括双引号内的内容),然后按下`Enter`键换行。 4. 输入"Vim Example Line 2",按下`Enter`键换行。 5. 输入"Vim Example Line 3",按下`Enter`键换行。 6. 输入"Vim Example Line 4",按下`Enter`键换行。 7. 输入"Vim Example Line 5.",然后按下`Esc`键退出插入模式,回到命令模式。 8. 输入`:wq`,按下`Enter`键保存并退出Vim编辑器。 这样,你就使用了最少的操作将内容输入到Vim编辑器中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值