vim 脚本之快捷注释
今天初步学习了下vim的脚本知识,并尝试写了一个简单的脚本。当然,这个脚本很简单,使用的方法也很笨拙。不过,这仅仅是一个开始,等以后随着对vim语法的深入了解,会不断优化这个脚本的。先记录下来
" annotation.vim
" Version: 1.0
if exists("g:zsl_loaded_annotation") || &cp || v:version < 700
finish
endif
let g:zsl_loaded_annotation = 1
"更改java默认注释符号
autocmd FileType java set commentstring=/*%s*/ "获取当前文件的注释标签
function! s:surroundings() abort
return split(substitute(substitute(
\ get(b:, 'commentary_format', &commentstring)
\ ,'\S\zs%s',' %s','') ,'%s\ze\S', '%s ', ''), '%s', 1)
endfunction "添加注释行首跟行尾
function! s:AddAnnotion(type, linenum)
let [l, r] = s:surroundings()
let time = "2016-01-01"
let anno = "modify by zhangshui time begin"
let anno = l.anno.r
if exists("*strftime")
let time = strftime("%Y-%m-%d")
endif
let anno = substitute(anno, "time", time, '') if a:type ==0
call append(a:linenum, anno)
elseif a:type == 1
call append(a:linenum, substitute(anno, "begin", "end", ''))
endif
"光标会自动跳转到操作行
execute (a:linenum+1)." normal =="
endfunction "普通模式下基础函数
function AnnotationN()
call s:AddAnnotion(0, line("."))
call s:AddAnnotion(1, line("."))
endfunction "可视模式下基础函数
function! s:AnnotationV()
call s:go(line("'<"), line("'>"))
call s:AddAnnotion(0, line("'<")-1)
call s:AddAnnotion(1, line("'>"))
endfunction "注释掉选中文本
function! s:go(type,...) abort
if a:0
let [lnum1, lnum2] = [a:type, a:1]
else
let [lnum1, lnum2] = [line("'["), line("']")]
endif let [l, r] = s:surroundings()
let uncomment = 2
for lnum in range(lnum1,lnum2)
let line = matchstr(getline(lnum),'\S.*\s\@<!')
if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let uncomment = 0
endif
endfor for lnum in range(lnum1,lnum2)
let line = getline(lnum)
if strlen(r) > 2 && l.r !~# '\\'
let line = substitute(line,
\'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1],
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
endif
if uncomment
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
else
let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
endif
call setline(lnum,line)
endfor
silent doautocmd User CommentaryPost
endfunction
xnoremap <silent> <Plug>AnnotationV :<C-U>call <SID>AnnotationV()<CR>
函数调用的方式,是在.vimrc中配置如下内容
map <silent>fj :call AnnotationN()<cr>
vmap <silent>fj <Plug>AnnotationV
vim 脚本之快捷注释的更多相关文章
- vim脚本语言
转自:http://man.chinaunix.net/newsoft/vi/doc/usr_41.html#usr_41.txt Vim 脚本语言在很多地方用到,包括 vimrc 文件, 语法文件, ...
- grep 查找bash脚本中的注释代码
出于安全性的考虑,不建议在bash脚本中注释掉不使用的代码.也就是说如果某段代码不使用了,那么应该删除掉,而不是简单地注释掉.假如你突然意识到这一点,而以前并没有遵从这个原则,现在需要找出脚本中的注释 ...
- Vim 自动文件头注释与模板定义
Vim 自动文件头注释与模板定义 在vim的配置文件.vimrc添加一些配置可以实现创建新文件时自动添加文件头注释,输入特定命令可以生成模板. 使用方法 插入模式输入模式输入seqlogic[Ente ...
- Unity脚本自动添加注释脚本及排版格式
Unity脚本自动添加注释脚本及头部注释排版格式 公司开发项目,需要声明版权所有,,,,标注公司名,作者,时间,项目名称及描述等等. 自己总结实现的现成脚本及头部注释排版文本,添加到模版即可. 文件不 ...
- vim删除空行和注释
vim删除空行和注释 来源: http://jpuyy.com/2015/06/vim-delete-lines-using-regexp.html 删除空行 :g/^$/d 删除空行以及只有空格的 ...
- (转)Vim 脚本语言
2012 年 10 月 20 日 by name5566 Categories: Computer Science, Tools 参考文献列表: http://vimdoc.sourceforge.n ...
- vim 脚本——插件
:help usr_41.txt 查看vim默认可添加插件的路径 :set runtimepath? 查看vim系统插件与脚本位置 :echo $VIMRUNTIME :echo $VIM 查看所有插 ...
- AutoHotkey批量L版代码转H2的vim脚本
原脚本尽量用表达式的语法写,错误会比较少,比如"If a=", "fun(a=1)"这种语法在V2会出错文件放vim的autoload目录下,可使用以下map使 ...
- <实训|第五天>通过搭建NFS,FTP实现共享文件附Vim脚本游戏
先说个事情:我周末是不更新这个系列教程的,不过其他内容的会更新,我周末就整理这一周的各种内容到我的微信公众号中,提供给大家! 期待已久的linux运维.oracle"培训班"终于开 ...
随机推荐
- BZOJ 2560(子集DP+容斥原理)
2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 757 Solved: 497[Submit][Status][Discuss] ...
- JavaScript函数写法整理
1.普通函数定义的两种写法 function hello(){ console.log("hello!"); } var hello = function(){ console.l ...
- 使用 vue + thinkjs 开发博客程序记录
一入冬懒癌发作,给自己找点事干.之前博客程序写过几次,php 的写过两次,nodejs 用 ThinkJS 写过,随着 ThinkJS 版本从1.x 升级到 2.x 之前的博客程序也做过升级.但是因为 ...
- 紫书 习题8-3 UVa 12545 (构造法)
参考了https://blog.csdn.net/catglory/article/details/47188949 最后推出来操作的个数为问号的个数 加上 同一位置上S串为0而T串为1的位置数量 与 ...
- NYIST 1108 最低的惩罚
最低的惩罚 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 那么现在问题就来了... 给你N(1=<N<=15)个任务,每个任务有一个截止完成时间t(1= ...
- 【转】Geometry cannot have Z values
http://blog.csdn.net/tweeenty/article/details/44246407 在对矢量要素类添加要素,进行赋几何信息时(FeatureBuffer.Shape = IG ...
- ubuntu鼠标和触摸板的禁用
ubuntu鼠标和触摸板的禁用 学习了:http://www.2cto.com/os/201308/239403.html 在终端下直接执行以下命令来打开或关闭触摸板. sudo modprobe - ...
- Yocto tips (15): Yocto中的包管理器
使用包管理器 在local.conf中使能就可以: 然后编译后就会有rpm包了: 配置文件server 能够使用ngix和apache.可是我们也能够仅仅用使用python: python -m Si ...
- STM32中assert_param的使用
在STM32的固件库和提供的例程中.到处都能够见到assert_param()的使用. 假设打开不论什么一个例程中的stm32f10x_conf.h文件,就能够看到实际上assert_param是一个 ...
- RIP协议的配置
拓扑图: 要求: 理解RIP协议的工作原理,并通过 RIP 路由协议实现不同网络互通. 配置步骤: 1.配置好 PC 机的 IP 地址等參数. PC 0>ipconfig IP Address. ...