`
oldrev
  • 浏览: 229901 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

简单的D语言 VIM 缩写插件

阅读更多
昨晚我写了一个非常简单的 VIM 的D语言缩写插件,希望能让用VIM编辑D程序的指头轻松一点。

请看下图,当在VIM的插入模式键入“'main”这个字符序列时,该插件能自动将文本替换为一个完整的 main() 函数定义,并且能自动选中 <+ body +> 部分等待输入替换。Ctrl+j 键可以跳转到下一个由 <+ ????? +> 标记的地方,同样会自动选定。



插件中的代码片段由 d_imaps.vim 文件定义,此文件应当放到 ~/.vim/ftplugin(Windows 用户推荐放到 $VIMRUNTIME/ftplugin/) 目录下,下面是 d_imaps.vim 文件的内容:
代码
  1. let maplocalleader="'"
  2. " D snippets "
  3. inoremap <buffer><localleader>main <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>int main(char[][] args)\n{\n <+ body +>;\nreturn <+ return value+>;\n}")<cr></cr>
  4. inoremap <buffer><localleader>istd <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>import std.stdio;\n")<cr></cr>
  5. inoremap <buffer><localleader>cl <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>class <+ name +>\n{\ninvariant\n{\n\n}\n\nthis()\n{\n}\n}")<cr></cr>
  6. inoremap <buffer><localleader>if <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>interface <+ name +>\n{\npublic <+ first method +>;\n}")<cr></cr>
  7. inoremap <buffer><localleader>switch <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>switch(<+ expr +>)\n{\ncase <+ cond1 +>:\n\nbreak;\n\n\n\ndefault:\n\nbreak;\n}")<cr></cr>
  8. inoremap <buffer><localleader>case <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>case <+ cond +>:\n<+ action +>;\nbreak;")<cr></cr>
  9. inoremap <buffer><localleader>bl <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>{\n<+ block +>;\n}")<cr></cr>
  10. inoremap <buffer><localleader>c1 <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>/*\n<+ block +>\n*/")<cr></cr>
  11. inoremap <buffer><localleader>cs <c-r>=IMAP_PutTextWithMovement("</c-r></localleader></buffer>const char[] <+ name +> = \"<+ string +>\";")<cr> </cr>
  12. inoremap <buffer><localleader>sa <c-r>=IMAP_PutTextWithMovement("static assert(<+ const expr +>);\n")<cr> </cr></c-r></localleader></buffer>

文件格式非常简单,第一行定义了缩写的前导字符,在这里是单引号。<localleader>后的字符就是缩写,在 IMAP_PutTextWithMovement("") 函数参数的文本是要插入的代码片段,需要需要跳转的部分用 <+ +> 包围起来就可以了。

需要注意的是此D语言缩写插件需要 imaps.vim 插件的支持,该插件可以在这里下载,将其放入 VIM 的 plugin 目录里就 OK了。

Happy Vimming!</localleader>
  • 大小: 7.9 KB
分享到:
评论
4 楼 cookoo 2007-04-17  
说“缩写定义”比“插件”用词更准确些。
3 楼 cookoo 2007-04-17  
说“缩写定义”比“插件”用词更准确些。
2 楼 oldrev 2007-04-14  
楼上的,代码就在文章里面阿
1 楼 wzjin 2007-04-14  
能否公开你的源代码,并加上注释,以后队员们可以在你代码基础上进行完善,同时可以供初学者学习。

相关推荐

Global site tag (gtag.js) - Google Analytics