Advertisement
anjishnu

CommentLines.vim

Sep 15th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 5.72 KB | None | 0 0
  1. " Comments and Uncomment lines
  2. " ******************************
  3. " Usage:
  4. " To comment lines by pressing CTRL-K with/without visual selection (CTRL-V)
  5. " To uncomment lines by pressing CTRL-L with/without visual selection (CTRL-V)
  6. " Author: Anjishnu Sarkar
  7. " Version: 1.0
  8. " Date: 15/04/07
  9. " Vim Version: 6.0+
  10. " Installation: Copy this file in your $HOME/.vim/plugin directory
  11. " Acknowledgement: www.vim.org -> Vim_Tip #271, comments.vim
  12. " ##############################################################
  13. "
  14. " BUG: (i) Same filebuffer used when different kind of file opened using
  15. " :sp command. As such comments in 2nd file are not according to filetype
  16. " -> Soln: Recognize file type ?
  17. "
  18. " TODO: To fix the above bug
  19. " ##############################################################
  20.  
  21. if v:version < 600  " Exit if Vim version is less than 6.0
  22. "    echohl WarningMsg
  23. "    echo "You need vim version 6.0 or higher"
  24. "    echohl None
  25.     finish
  26. elseif exists ("g:loaded_lines_comment") || &cp
  27. " Exit quickly when:
  28. " - when 'compatible' is set
  29. " - this plugin was already loaded (or disabled)
  30.     finish
  31. else "   Load the script
  32.     let g:loaded_lines_comment = 1
  33. endif
  34.  
  35. " ### Comment and Uncomment vim script starts ###
  36. "let file_ext = buffer_name("%")
  37. let file_ext = bufname("%")  " bufname = buffer_name
  38.  
  39. " lhs comments
  40.  
  41. " For Fortran files: .for, .f90: !
  42. if file_ext =~ '\.f$' || file_ext =~ '\.for$' || file_ext =~ '\.f90$' || file_ext =~ '\.f95$' || file_ext =~ '\.nml$'
  43.     map <c-k> :s/^/! /<CR>:nohlsearch<CR><CR>
  44.     map <c-l> :s/^\s*! \=//<CR>:nohlsearch<CR><CR>
  45.  
  46. " For latex files: .tex, .bib, .sty, .bbl: %   
  47. elseif file_ext =~ '\.tex$' || file_ext =~ '\.bib$' || file_ext =~ '\.sty$' || file_ext =~ '\.bbl$' || file_ext =~ '\.cls$' || file_ext =~ '\.not$' || file_ext =~ '\.clo$' || file_ext =~ '\.ldf$'
  48.     map <c-k> :s/^/% /<CR>:nohlsearch<CR><CR>
  49.     map <c-l> :s/^\s*% \=//<CR>:nohlsearch<CR><CR>
  50.  
  51. " For latex files: .frm : *
  52. elseif file_ext =~ '\.frm$'
  53.     map <c-k> :s/^/* /<CR>:nohlsearch<CR><CR>
  54.     map <c-l> :s/^\s*\* \=//<CR>:nohlsearch<CR><CR>
  55.  
  56. " For batch files: .bat, .com: REM/rem
  57. elseif file_ext =~ '\.bat$' || file_ext =~ '\.cmd$'
  58.     map <c-k> :s/^/REM /<CR>:nohlsearch<CR><CR>
  59.     map <c-l> :set ignorecase<CR>:s/^\s*rem \=//<CR>:nohlsearch<CR><CR>
  60.  
  61. " For vim files: .vim, .vimrc, .gvimrc: "
  62. elseif file_ext =~ '\.vim$' || file_ext =~ '\.vimrc$' || file_ext =~ '\.gvimrc$'
  63.     map <c-k> :s/^/" /<CR>:nohlsearch<CR><CR>
  64.     map <c-l> :s/^\s*" \=//<CR>:nohlsearch<CR><CR>
  65.  
  66. " For .cc, .cpp, .hpp, .java, .php[23] files: //
  67. elseif file_ext =~ '\.cc$' || file_ext =~ '\.cpp$' || file_ext =~ '\.hpp$' || file_ext =~ '\.java$' || file_ext =~ '\.php[23]\?$'
  68.     map <c-k> :s/^/\/\/ /<CR>:nohlsearch<CR><CR>
  69.     map <c-l> :s/^\s*\/\/ \=//<CR>:nohlsearch<CR><CR>
  70.  
  71. " For .plt, .gnu, .gpi files: #
  72. elseif file_ext =~ '\.plt$' || file_ext =~ '\.gnu$' || file_ext =~ '\.gpi$'
  73.     map <c-k> :s/^/# /<CR>:nohlsearch<CR><CR>
  74.     map <c-l> :s/^\s*# \=//<CR>:nohlsearch<CR><CR>
  75.  
  76. " For .sql files: --
  77. elseif file_ext =~ '\.sql$'
  78.     map <c-k> :s/^/-- /<CR>:nohlsearch<CR><CR>
  79.     map <c-l> :s/^\s*-- \=//<CR>:nohlsearch<CR><CR>
  80.  
  81. " For .ksh .sh, .csh, .pl, .pm files: #
  82. elseif file_ext =~ '\.ksh$' || file_ext =~ '\.sh$' || file_ext =~ '\.csh$' || file_ext =~ '\.pl$' || file_ext =~ '\.pm$'
  83.     map <c-k> :s/^/# /<CR>:nohlsearch<CR><CR>
  84.     map <c-l> :s/^\s*# \=//<CR>:nohlsearch<CR><CR>
  85.  
  86. " For .eps, .ps, .map files: %
  87. elseif file_ext =~ '\.ps$' || file_ext =~ '\.eps$' || file_ext =~ '\.map$'
  88.     map <c-k> :s/^/% /<CR>:nohlsearch<CR><CR>
  89.     map <c-l> :s/^\s*% \=//<CR>:nohlsearch<CR><CR>
  90.  
  91. " For ~/.Xdefaults ~/.XResources files: >
  92. elseif file_ext =~ '\.Xdefaults$' || file_ext =~ '\.XResources$'
  93.     map <c-k> :s/^/! /<CR>:nohlsearch<CR><CR>
  94.     map <c-l> :s/^\s*! \=//<CR>:nohlsearch<CR><CR>
  95.  
  96. " For .msg files: >
  97. elseif file_ext =~ '\.msg$'
  98.     map <c-k> :s/^/> /<CR>:nohlsearch<CR><CR>
  99.     map <c-l> :s/^\s*> \=//<CR>:nohlsearch<CR><CR>
  100.  
  101. " For .m (Matlab) files: %
  102. elseif file_ext =~ '\.m$'
  103.     map <c-k> :s/^/% /<CR>:nohlsearch<CR><CR>
  104.     map <c-l> :s/^\s*% \=//<CR>:nohlsearch<CR><CR>
  105.  
  106. " For .sce, .sci (Scilab) files: %
  107. elseif file_ext =~ '\.sce$' || file_ext =~ '\.sci$'
  108.     map <c-k> :s/^/\/\/ /<CR>:nohlsearch<CR><CR>
  109.     map <c-l> :s/^\s*\/\/ \=//<CR>:nohlsearch<CR><CR>
  110.  
  111. " For .lua (Lua script) files: --
  112. elseif file_ext =~ '\.lua$'
  113.     map <c-k> :s/^/-- /<CR>:nohlsearch<CR><CR>
  114.     map <c-l> :s/^\s*-- \=//<CR>:nohlsearch<CR><CR>
  115.  
  116. " Wrapping comments
  117. " For .m (Mathematica files): (*   *)
  118. " elseif file_ext =~ '\.m$'
  119. "     map <c-k> :s/^\(.*\)$/\(\* \1 \*\)/<CR>:nohlsearch<CR><CR>
  120. "     map <c-l> :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR><CR>
  121.  
  122. " For .c, .h, .pc, .css, .js: /*   */
  123. elseif file_ext =~ '\.c$' || file_ext =~ '\.h$' || file_ext =~ '\.pc$' || file_ext =~ '\.css$' || file_ext =~ '\.js$'  
  124.     map <c-k> :s/^\(.*\)$/\/\* \1 \*\//<CR>:nohlsearch<CR><CR>
  125.     map <c-l> :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR><CR>
  126.  
  127. " For .mac, .wxm, .mxm (Maxima files): /*   */
  128. elseif file_ext =~ '\.mac$' || file_ext =~ '\.wxm$' || file_ext =~ '\.mxm$'  
  129.     map <c-k> :s/^\(.*\)$/\/\* \1 \*\//<CR>:nohlsearch<CR><CR>
  130.     map <c-l> :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR><CR>
  131.  
  132. " For .html, .htm, .xhtml, .xml files: <!--   -->
  133. elseif file_ext =~ '\.html$' || file_ext =~ '\.htm$' || file_ext =~ '\.xml$' || file_ext =~ '\.xhtml$'
  134.     map <c-k> :s/^\(.*\)$/<!-- \1 -->/<CR>:nohlsearch<CR><CR>
  135.     map <c-l> :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR><CR>
  136.  
  137. " For all other files (Eg: .bashrc, .bash_profile): #
  138. else    
  139.     map <c-k> :s/^/# /<CR>:nohlsearch<CR><CR>
  140.     map <c-l> :s/^\s*# \=//<CR>:nohlsearch<CR><CR>
  141. endif
  142.  
  143. " ### Comment and Uncomment vim script ends ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement