vim创建程序文件自动添加头部注释

前端之家收集整理的这篇文章主要介绍了vim创建程序文件自动添加头部注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

修改 ~/.vimrc,在文件最后添加以下内容


  1. " add header comments for .h .c .hpp .cpp .mk .sh new file
  2. " auto call SetTitle func
  3. autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh,*.py exec ":call SetTitle()"
  4.  
  5. " add comment for cpp
  6. func SetComment_ch()
  7. call setline(1,"/*================================================================")
  8. call append(line("."),"* Copyright (C) ".strftime("%Y")." * Ltd. All rights reserved.")
  9. call append(line(".")+1,"* ")
  10. call append(line(".")+2,"* File name : ".expand("%:t"))
  11. call append(line(".")+3,"* Author : longbin")
  12. call append(line(".")+4,"* Created date: ".strftime("%F %T"))
  13. call append(line(".")+5,"* Description : ")
  14. call append(line(".")+6,"*")
  15. call append(line(".")+7,"*===============================================================*/")
  16. call append(line(".")+8,"")
  17. call append(line(".")+9,"")
  18. endfunc
  19.  
  20. " add comment for shell,Makefile
  21. func SetComment_sh()
  22. call setline(3,"#================================================================")
  23. call setline(4,"# Copyright (C) ".strftime("%Y")." * Ltd. All rights reserved.")
  24. call setline(5,"# ")
  25. call setline(6,"# File name : ".expand("%:t"))
  26. call setline(7,"# Author : longbin")
  27. call setline(8,"# Created date: ".strftime("%F %T"))
  28. call setline(9,"# Description : ")
  29. call setline(10,"#")
  30. call setline(11,"#================================================================")
  31. call setline(12,"")
  32. call setline(13,"")
  33. endfunc
  34.  
  35. " SetTitle func,add comment
  36. func SetTitle()
  37. if &filetype == 'make'
  38. call setline(1,"")
  39. call setline(2,"")
  40. call SetComment_sh()
  41.  
  42. elseif &filetype == 'sh'
  43. call setline(1,"#! /bin/bash")
  44. call setline(2,"")
  45. call SetComment_sh()
  46.  
  47. elseif &filetype == 'python'
  48. call setline(1,"#! /usr/bin/env python")
  49. call setline(2,"# coding=utf-8")
  50. call setline(3,"")
  51. call SetComment_sh()
  52.  
  53. else
  54. call SetComment_ch()
  55. if expand("%:e") == 'hpp'
  56. call append(line(".")+10,"#ifndef _".toupper(expand("%:t:r"))."_H")
  57. call append(line(".")+11,"#define _".toupper(expand("%:t:r"))."_H")
  58. call append(line(".")+12,"#ifdef __cplusplus")
  59. call append(line(".")+13,"extern \"C\"")
  60. call append(line(".")+14,"{")
  61. call append(line(".")+15,"#endif")
  62. call append(line(".")+16,"")
  63. call append(line(".")+17,"#ifdef __cplusplus")
  64. call append(line(".")+18,"}")
  65. call append(line(".")+19,"#endif")
  66. call append(line(".")+20,"#endif //".toupper(expand("%:t:r"))."_H")
  67.  
  68. elseif expand("%:e") == 'h'
  69. call append(line(".")+10,"#pragma once")
  70.  
  71. elseif &filetype == 'c'
  72. call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
  73.  
  74. elseif &filetype == 'cpp'
  75. call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
  76.  
  77. endif
  78.  
  79. endif
  80. endfunc

猜你在找的Bash相关文章