" ********************************************************************************************* " comments.vim " ********************************************************************************************* " Description : Global Plugin to comment and un-comment different " source files in both normal and visual mode " Last Change : 26th April, 2006 " Created By : Jasmeet Singh Anand " Version : 2.2 " Usage : For VIM 6 - " Stick this file in your ~/.vim/plugin directory or " in some other 'plugin' directory that is in your runtime path " For VIM 5 - " Stick this file somewhere and 'source /comments.vim' it from " your ~/.vimrc file " Note : I have provided the following key mappings " To comment in both normal and visual range select mode " To un-comment in both normal and visual range select mode " These can be changed based on user's likings or usage " Contact : For any comments or bug fixes email me at " ********************************************************************************************* "Modification: " ********************************************************************************************* " Jasmeet Anand 26th April, 2006 v2.0 " Fixed C commenting where a single line already had previous comments. " int x=0; /*this is an x value*/ " Still working on situations like " Issue A: " 1 int x=0; /*this " 2 is " 3 an " 4 x " 5 value*/ " ********************************************************************************************* " Jasmeet Anand 26th April, 2006 v2.1 " Provided more granule checking for C Code but still working on Issue A " ********************************************************************************************* " Jasmeet Anand 27th April, 2006 v2.2 " Fixed another minor C code commenting bug " Provided for .csh, .php, .php2 and .php3 support " Resolved Issue A with the following logic " 1 /* int x=0; */ /*this*/ " 2 /*is*/ " 3 /*an*/ " 4 /*x*/ " 5 /*value*/ " However care should be taken when un-commenting it " in order to retain the previous comments " ********************************************************************************************* " Jasmeet Anand 1st May 2006 v2.3 " Provided [:blank:] to accomodate for space and tab characters " ********************************************************************************************* " Jasmeet Anand 1st May 2006 v2.4 " Provided support for .css as advised by Willem Peter " ********************************************************************************************* " Jasmeet Anand 2nd May 2006 v2.5 " Removed auto-indenting for .sql, .sh and normal files when un-commenting " ********************************************************************************************* " Jasmeet Anand 5th June 2006 v2.6 " Added support for .html, .xml, .xthml, .htm, .vim, .vimrc " files as provided by Jeff Buttars " ********************************************************************************************* " " Exit if already loaded if exists("loaded_comments_plugin") finish endif let loaded_comments_plugin="v2.6" " key-mappings for comment line in normal mode noremap cm :call CommentLine() " key-mappings for range comment lines in visual mode vnoremap cm :call RangeCommentLine() " key-mappings for un-comment line in normal mode noremap cu :call UnCommentLine() " key-mappings for range un-comment lines in visual mode vnoremap cu :call RangeUnCommentLine() " function to comment line in normal mode function! CommentLine() let file_name = buffer_name("%") " for .cpp or .hpp or .java files use // if file_name =~ '\.cpp$' || file_name =~ '\.hpp$' || file_name =~ '\.java$' || file_name =~ '\.php[23]\?$' execute ":silent! normal ^i//\==\^" " for .c or .h or .pc or .css files use /* */ elseif file_name =~ '\.c$' || file_name =~ '\.h$' || file_name =~ '\.pc$' || file_name =~ '\.css$' || file_name =~ '\.js$' " if there are previous comments on this line ie /* ... */ if stridx(getline("."), "\/\*") != -1 && stridx(getline("."), "\*\/") != -1 execute ":silent! normal :nohlsearch\:s/\\([^\\/\\*]*\\)\\(\\/\\*.*\\*\\/\\)/\\1\\*\\/\\2/\:s/\\([^[:blank:]]\\+\\)/\\/\\*\\1/\:nohlsearch\==" " if there is a /* but no */ like line 1 in Issue A above elseif stridx(getline("."), "\/\*") != -1 && stridx(getline("."), "\*\/") == -1 execute ":silent! normal :nohlsearch\:s/\\(.*\\)\\(\\/\\*.*$\\)/\\/\\*\\1\\*\\/\\2\\*\\//\:nohlsearch\==" " if there is a */ but no /* like line 5 in Issue A above elseif stridx(getline("."), "\/\*") == -1 && stridx(getline("."), "\*\/") != -1 execute ":silent! normal :nohlsearch\:s/\\(.*\\*\\/\\)/\\/\\*\\1/\:nohlsearch\==" " if there are no comments on this line elseif stridx(getline("."), "\/\*") == -1 && stridx(getline("."), "\*\/") == -1 execute ":silent! normal ^i/*\$a*/\==\^" endif " .html,.xml,.xthml,.htm elseif file_name =~ '\.html$' || file_name =~ '\.htm$' || file_name =~ '\.xml$' || file_name =~ '\.xhtml$' if stridx( getline("."), "\ elseif file_name =~ '\.html$' || file_name =~ '\.htm$' || file_name =~ '\.xml$' || file_name =~ '\.xhtml$' execute ":silent! normal :nohlsearch\:s///\==" " for all other files use # else execute ":silent! normal :nohlsearch\:s/\\#//\:nohlsearch\" endif endfunction " function to range comment lines in visual mode function! RangeCommentLine() let file_name = buffer_name("%") " for .cpp or .hpp or .java files use // if file_name =~ '\.cpp$' || file_name =~ '\.hpp$' || file_name =~ '\.java$' || file_name =~ '\.php[23]\?$' execute ":silent! normal :s/\\S/\\/\\/\\0/\:nohlsearch==" " for .c or .h or .pc or .css files use /* */ elseif file_name =~ '\.c$' || file_name =~ '\.h$' || file_name =~ '\.pc$' || file_name =~ '\.css$' || file_name =~ '\.js$' " if there are previous comments on this line ie /* ... */ if stridx(getline("."), "\/\*") != -1 && stridx(getline("."), "\*\/") != -1 execute ":silent! normal :nohlsearch\:s/\\([^\\/\\*]*\\)\\(\\/\\*.*\\*\\/\\)/\\1\\*\\/\\2/\:s/\\([^[:blank:]]\\+\\)/\\/\\*\\1/\:nohlsearch\==" " if there is a /* but no */ like line 1 in Issue A above elseif stridx(getline("."), "\/\*") != -1 && stridx(getline("."), "\*\/") == -1 execute ":silent! normal :nohlsearch\:s/\\(.*\\)\\(\\/\\*.*$\\)/\\/\\*\\1\\*\\/\\2\\*\\//\:nohlsearch\==" " if there is a */ but no /* like line 5 in Issue A above elseif stridx(getline("."), "\/\*") == -1 && stridx(getline("."), "\*\/") != -1 execute ":silent! normal :nohlsearch\:s/\\(.*\\*\\/\\)/\\/\\*\\1/\:nohlsearch\==" " if there are no comments on this line elseif stridx(getline("."), "\/\*") == -1 && stridx(getline("."), "\*\/") == -1 execute ":silent! normal :s/\\(\\S.*$\\)/\\/\\*\\1\\*\\//\:nohlsearch\==" endif " .html,.xml,.xthml,.htm elseif file_name =~ '\.html$' || file_name =~ '\.htm$' || file_name =~ '\.xml$' || file_name =~ '\.xhtml$' if stridx( getline("."), "\ elseif file_name =~ '\.html$' || file_name =~ '\.htm$' || file_name =~ '\.xml$' || file_name =~ '\.xhtml$' execute ":silent! normal :nohlsearch\:s///\==" elseif file_name =~ '\.[kc]\?sh$' || file_name =~ '\.pl$' || file_name =~ '\.pm$' execute ":silent! normal :s/\\#//\:nohlsearch\" " for all other files use # else execute ":silent! normal :s/\\#//\:nohlsearch\" endif endfunction