Vim Tips Wiki
(Added to LanguageSpecific Category + links to 2 of the numerous related scripts)
(Added reference to the tcomment plugin)
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=355
 
|id=355
  +
|previous=352
|title=Comment Lines according to a given filetype
 
  +
|next=356
|created=October 30, 2002 8:12
+
|created=2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Luis Mondesi
 
|author=Luis Mondesi
 
|version=6.0
 
|version=6.0
 
|rating=8/6
 
|rating=8/6
  +
|category1=Filetype
|text=
 
 
|category2=LanguageSpecific
There is probably an easier way to do this, but, if I cannot find an easy solution for a given problem, I just devise one that works for the meantime -- which usually becomes permanent :-) .
 
 
}}
 
}}
 
There is probably an easier way to do this, but, if I cannot find an easy solution for a given problem, I just devise one that works for the meantime -- which usually becomes permanent.
   
This function comments out lines according to file type. So if a file is .sh, it uses # to comment lines. And if a file is type .c it will start the comments with /* and end them with */.
+
This function comments out lines according to file type. So if a file is .sh, it uses # to comment lines. And if a file is type .c it will start the comments with /* and end them with */.
   
Put this lines in your .vimrc file:
+
Put these lines in your [[vimrc]] file:
  +
<pre>
" -------- vimrc ---------
 
" comment out highlighted lines according to file type
+
" comment out highlighted lines according to file type
" put a line like the following in your ~/.vim/filetype.vim file
+
" put a line like the following in your ~/.vim/filetype.vim file
" and remember to turn on filetype detection: filetype on
+
" and remember to turn on filetype detection: filetype on
" au! BufRead,BufNewFile *.sh,*.tcl,*.php,*.pl let Comment="&#35;"
+
" au! BufRead,BufNewFile *.sh,*.tcl,*.php,*.pl let Comment="#"
" if the comment character for a given filetype happens to be @
+
" if the comment character for a given filetype happens to be @
" then use let Comment="\@" to avoid problems...
+
" then use let Comment="\@" to avoid problems...
 
function CommentLines()
 
  +
"let Comment="#" " shell, tcl, php, perl
fun CommentLines()
 
"let Comment="&#35;" " shell, tcl, php, perl
+
exe ":s@^@".g:Comment."@g"
exe ":s@^@".g:Comment."@g"
+
exe ":s@$@".g:EndComment."@g"
  +
endfunction
exe ":s@$@".g:EndComment."@g"
 
 
" map visual mode keycombo 'co' to this function
endfun
 
 
vmap co :call CommentLines()<CR>
 
  +
</pre>
" map visual mode keycombo 'co' to this function
 
vmap co :call CommentLines()&lt;CR&gt;
 
" ------- end vimrc -------
 
   
Now create a ~/.vim/filetype.vim file if you don't have one and add things like these to it (remember to put a line
+
In [[filetype.vim]] you can add things such as:
  +
<pre>
:filetype on
 
 
au BufRead,BufNewFile *.inc,*.ihtml,*.html,*.tpl,*.class set filetype=php
in your vimrc file ... again if you don't already have one. Vim needs to be compiled with filetype detection support for this to work. You have been warned.):
 
 
\ | let Comment="<!-- " | let EndComment=" -->"
 
au BufRead,BufNewFile *.sh,*.pl,*.tcl let Comment="#" | let EndComment=""
 
au BufRead,BufNewFile *.js set filetype=html | let Comment="//" | let EndComment=""
 
au BufRead,BufNewFile *.cc,*.php,*.cxx let Comment="//" | let EndComment=""
 
au BufRead,BufNewFile *.c,*.h let Comment="/*" | let EndComment="*/"
  +
</pre>
   
 
All set, now whenever you are editing a file of those you have defined in your filetype.vim script, you can just go into Visual mode, highlight what you want to comment out, and type "co". Simple.
-------- filetype.vim ---------
 
if exists("did_load_filetypes")
 
finish
 
endif
 
 
augroup filetypedetect
 
au! BufRead,BufNewFile *.inc,*.ihtml,*.html,*.tpl,*.class set filetype=php
 
\ | let Comment="&lt;!-- " | let EndComment=" --&gt;"
 
au! BufRead,BufNewFile *.sh,*.pl,*.tcl let Comment="&#35;" | let EndComment=""
 
au! BufRead,BufNewFile *.js set filetype=html | let Comment="//" | let EndComment=""
 
au! BufRead,BufNewFile *.cc,*.php,*.cxx let Comment="//" | let EndComment=""
 
au! BufRead,BufNewFile *.c,*.h let Comment="/*" | let EndComment="*/"
 
augroup END
 
------ end filetype.vim -------
 
 
All set, now whenever you are editing a file of those you have defined in your filetype.vim script, you can just go into Visual mode, highlight what you want to comment out, and type "co". Simple.
 
   
 
==See also==
 
==See also==
* EnhancedCommentify: {{script|id=23}}
+
*EnhancedCommentify: {{script|id=23}}
* ToggleCommentify: {{script|id=4}}
+
*tComment: {{script|id=1173}}
  +
*ToggleCommentify: {{script|id=4}}
* .... -> [http://www.vim.org/scripts/script_search_results.php?keywords=comment Search Vim Scripts]
 
   
== Comments ==
+
==Comments==
 
I was used to using 'boxes' as an external filter to comment/decomment, but pushing to the program and back is more 'expensive' then using a simple script, but boxes really is better then EnhancedCommentify.vim (at least I haven't found a way yet to make it behave just like I want it to.).
Why don't you use EnhancedCommentify or ToggleCommentify ?
 
  +
They do work fine, they are maintained, extensible, etc.
 
 
But I would prefer a maintained thing any day.
   
[[User:Luc Hermitte|Luc Hermitte]]
 
, October 30, 2002 11:29
 
 
----
 
----
  +
The first filetype autocmd uses <code>set filetype=php</code> instead of <code>setf php</code>, which could cause it to replace the filetype (depending on where in the runtime path and file it's placed). It's also the only one which changes a filetype (and it does so a bit overzealously, not all .inc or .class files are php).
As I said in the first line: "There is probably an easier way to do this, but ..."
 
That's why. I couldn't find a script to do what I wanted.
 
   
  +
I think the <code>setf</code> can be removed altogether, or moved into a different tip, at least.
Now that you mention EnhancedCommentify I downloaded it and started going thru it. Essentially the same idea, but bloated :-). At least I learned that you can do "if fileType =~" maybe avoiding using a filetype.vim file to set the comment start and comment end variables.
 
   
  +
In fact, these would probably be better in vimrc autocmds as
I don't want to take merit out of EnhancedCommentify or the other one mentioned. They serve a purpose. In my case, I only needed to comment out blocks quick and dirty, never thinking about un-commenting them (or if I had to I could probably just quickly search thru the lines I just commented out: 1,23s/&#35;//g ... you get the idea...)
 
  +
<pre>
 
  +
au FileType php let Comment="<!-- " | let EndComment=" -->"
Thanks anyway for the tip!
 
  +
</pre>
 
  +
et al. Let the pre-flow evaluate the filetype, and the post-flow define the comment fields.
lemsx1[at]hotmail.com
 
, October 30, 2002 22:59
 
----
 
I was used to using 'boxes' as an external filter to comment/decomment, but pushing to the program and back is more 'expensive' then using a simple script, but boxes really is better then EnhancedCommentify.vim (at least I haven't found a way yet to make it behave just like I want it to.).
 
   
  +
Or probably even <code>after/ftplugin/&lt;filetype&gt;.vim</code> syntax modification files.
But I would prefer a maintained thing anyday ;)
 
   
  +
--[[User:JeremyBarton|JeremyBarton]] 07:42, 30 September 2008 (UTC)
regards, koenraad.
 
   
Koenraad HEijlen &lt;vipie --AT-- ulyssis.org&gt;
 
, November 27, 2002 17:37
 
 
----
 
----
<!-- parsed by vimtips.py in 0.723442 seconds-->
 
[[Category:LanguageSpecific]]
 

Revision as of 08:16, 15 January 2012

Tip 355 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Luis Mondesi · version 6.0


There is probably an easier way to do this, but, if I cannot find an easy solution for a given problem, I just devise one that works for the meantime -- which usually becomes permanent.

This function comments out lines according to file type. So if a file is .sh, it uses # to comment lines. And if a file is type .c it will start the comments with /* and end them with */.

Put these lines in your vimrc file:

" comment out highlighted lines according to file type
" put a line like the following in your ~/.vim/filetype.vim file
" and remember to turn on filetype detection: filetype on
" au! BufRead,BufNewFile *.sh,*.tcl,*.php,*.pl let Comment="#"
" if the comment character for a given filetype happens to be @
" then use let Comment="\@" to avoid problems...
function CommentLines()
  "let Comment="#" " shell, tcl, php, perl
  exe ":s@^@".g:Comment."@g"
  exe ":s@$@".g:EndComment."@g"
endfunction
" map visual mode keycombo 'co' to this function
vmap co :call CommentLines()<CR>

In filetype.vim you can add things such as:

au BufRead,BufNewFile *.inc,*.ihtml,*.html,*.tpl,*.class set filetype=php
        \ | let Comment="<!-- " | let EndComment=" -->"
au BufRead,BufNewFile *.sh,*.pl,*.tcl let Comment="#" | let EndComment=""
au BufRead,BufNewFile *.js set filetype=html | let Comment="//" | let EndComment=""
au BufRead,BufNewFile *.cc,*.php,*.cxx let Comment="//" | let EndComment=""
au BufRead,BufNewFile *.c,*.h let Comment="/*" | let EndComment="*/"

All set, now whenever you are editing a file of those you have defined in your filetype.vim script, you can just go into Visual mode, highlight what you want to comment out, and type "co". Simple.

See also

Comments

I was used to using 'boxes' as an external filter to comment/decomment, but pushing to the program and back is more 'expensive' then using a simple script, but boxes really is better then EnhancedCommentify.vim (at least I haven't found a way yet to make it behave just like I want it to.).

But I would prefer a maintained thing any day.


The first filetype autocmd uses set filetype=php instead of setf php, which could cause it to replace the filetype (depending on where in the runtime path and file it's placed). It's also the only one which changes a filetype (and it does so a bit overzealously, not all .inc or .class files are php).

I think the setf can be removed altogether, or moved into a different tip, at least.

In fact, these would probably be better in vimrc autocmds as

au FileType php  let Comment="<!-- " | let EndComment=" -->"

et al. Let the pre-flow evaluate the filetype, and the post-flow define the comment fields.

Or probably even after/ftplugin/<filetype>.vim syntax modification files.

--JeremyBarton 07:42, 30 September 2008 (UTC)