(Remove html character entities) |
(Added reference to the tcomment plugin) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
{{TipImported |
{{TipImported |
||
|id=355 |
|id=355 |
||
− | |previous= |
+ | |previous=352 |
|next=356 |
|next=356 |
||
− | |created= |
+ | |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= |
+ | |category1=Filetype |
− | |category2= |
+ | |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. |
||
Line 16: | Line 16: | ||
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 these lines in your vimrc file: |
+ | Put these lines in your [[vimrc]] file: |
− | |||
<pre> |
<pre> |
||
" comment out highlighted lines according to file type |
" comment out highlighted lines according to file type |
||
Line 25: | Line 24: | ||
" 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 |
"let Comment="#" " shell, tcl, php, perl |
||
exe ":s@^@".g:Comment."@g" |
exe ":s@^@".g:Comment."@g" |
||
exe ":s@$@".g:EndComment."@g" |
exe ":s@$@".g:EndComment."@g" |
||
+ | endfunction |
||
− | endfun |
||
" map visual mode keycombo 'co' to this function |
" map visual mode keycombo 'co' to this function |
||
vmap co :call CommentLines()<CR> |
vmap co :call CommentLines()<CR> |
||
</pre> |
</pre> |
||
− | + | In [[filetype.vim]] you can add things such as: |
|
− | :filetype on |
||
− | in your vimrc file, if you don't already have one. Vim needs to be compiled with filetype detection support for this to work. |
||
− | |||
<pre> |
<pre> |
||
⚫ | |||
− | if exists("did_load_filetypes") |
||
− | finish |
||
− | endif |
||
− | augroup filetypedetect |
||
⚫ | |||
\ | let Comment="<!-- " | let EndComment=" -->" |
\ | 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="*/" |
|
− | augroup END |
||
</pre> |
</pre> |
||
Line 55: | Line 46: | ||
==See also== |
==See also== |
||
− | * |
+ | *EnhancedCommentify: {{script|id=23}} |
− | * |
+ | *tComment: {{script|id=1173}} |
+ | *ToggleCommentify: {{script|id=4}} |
||
− | * .... -> [http://www.vim.org/scripts/script_search_results.php?keywords=comment Search Vim Scripts] |
||
==Comments== |
==Comments== |
||
Line 63: | Line 54: | ||
But I would prefer a maintained thing any day. |
But I would prefer a maintained thing any day. |
||
+ | |||
+ | ---- |
||
+ | 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). |
||
+ | |||
+ | I think the <code>setf</code> can be removed altogether, or moved into a different tip, at least. |
||
+ | |||
+ | In fact, these would probably be better in vimrc autocmds as |
||
+ | <pre> |
||
+ | au FileType php let Comment="<!-- " | let EndComment=" -->" |
||
+ | </pre> |
||
+ | et al. Let the pre-flow evaluate the filetype, and the post-flow define the comment fields. |
||
+ | |||
+ | Or probably even <code>after/ftplugin/<filetype>.vim</code> syntax modification files. |
||
+ | |||
+ | --[[User:JeremyBarton|JeremyBarton]] 07:42, 30 September 2008 (UTC) |
||
---- |
---- |
Revision as of 08:16, 15 January 2012
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
- EnhancedCommentify: script#23
- tComment: script#1173
- ToggleCommentify: script#4
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)