sponsor Vim development Vim logo Vim Book Ad

tComment : An extensible & universal comment plugin that also handles embedded filetypes

 script karma  Rating 71/25, Downloaded by 2433

created by
Tom Link
 
script type
utility
 
description
TComment works like a toggle, i.e., it will comment out text that
contains uncommented lines, and it will remove comment markup for
already commented text (i.e. text that contains no uncommented lines).

If the file-type is properly defined, TComment will figure out which
comment string to use. Otherwise you use |TCommentDefineType()| to
override the default choice.

TComment can properly handle an embedded syntax, e.g., ruby/python/perl
regions in vim scripts, HTML or JavaScript in php code etc.

Most of the time the default toggle keys will do what you want (or to be
more precise: what I think you want it to do ;-).

As operator (the prefix can be customized via g:tcommentMapLeaderOp1
and g:tcommentMapLeaderOp2):

    gc{motion}   :: Toggle comments (for small comments within one line
                    the &filetype_inline style will be used, if
                    defined)
    gcc          :: Toggle comment for the current line
    gC{motion}   :: Comment region
    gCc          :: Comment the current line

By default the cursor stays put. If you want the cursor to the end of
the commented text, set g:tcommentOpModeExtra to '>' (but this may not
work properly with exclusive motions).

Primary key maps:

    <c-_><c-_>   :: :TComment
    <c-_><space> :: :TComment <QUERY COMMENT-BEGIN ?COMMENT-END>
    <c-_>b       :: :TCommentBlock
    <c-_>a       :: :TCommentAs <QUERY COMMENT TYPE>
    <c-_>n       :: :TCommentAs &filetype <QUERY COUNT>
    <c-_>s       :: :TCommentAs &filetype_<QUERY COMMENT SUBTYPE>
    <c-_>i       :: :TCommentInline
    <c-_>r       :: :TCommentRight
    <c-_>p       :: Comment the current inner paragraph

There is also a secondary set of key maps with <Leader>_ as leader (more
preferable on terminals).

Alternatively, you can type (? meaning "optional argument"):

    :?<range> TComment ?commentBegin ?commentEnd
    :?<range> TComment! ?commentBegin ?commentEnd
    NOTE: If there is a visual selection that begins and ends in the same
    line, then TCommentInline is used instead.

    NOTE: The range is optional and defaults to the current line.

    :?<range> TCommentInline ?commentBegin ?commentEnd
    :?<range> TCommentInline! ?commentBegin ?commentEnd
    Use the {&ft}_inline comment style.

    :?<range> TCommentBlock ?commentBegin ?commentEnd
    :?<range> TCommentBlock! ?commentBegin ?commentEnd
    Comment as "block", e.g. use the {&ft}_block comment style.
    NOTE: This command is kind of crude. It doesn't indent or reformat
    the text.

    :?<range> TCommentAs FILETYPE [COUNT]
    :?<range> TCommentAs! FILETYPE [COUNT]
    NOTE: TCommentAs requires g:tcomment_{filetype} to be defined.
    NOTE: This command supports command line completion. See 'wildmode'
    and 'wildmenu' for how to get the most out of it. If a count is
    given, the comment leader is repeated, e.g. TCommentAs vim 3 will
    use """ as comment leader.

    :?<range> TCommentRight
    :?<range> TCommentRight!
    NOTE: This command comments out the text to the right of the cursor.
    If a visual selection was made (be it block-wise or not), all lines
    are commented out at from the current cursor positon downwards.

    The bang (!) variants always comment out the selected text and don't
    work as toggles.

    Using this command you can also use different comment styles with
    the TCommentDefineType(name, commentstring) function. This function
    takes two arguments:
        name :: The name is either &filetype or {&filetype}_{style}.
            I.e., For block comments the {&filetype}_block and for
            inline comments the {&filetype}_inline styles are used.
        comment string :: a string mostly as described in
            'commentstring'.
    
    If you want to define, e.g., a fancy block comment style for html
    you put something like this into ~/.vim/after/plugin/tComment.vim:

        call TCommentDefineType("html_fancy_block", "<!--%s  -->\n  -- ")

    The part after the newline character is used for marking "middle"
    lines.

    This comment style could then be accessed via (this command has
    command line completion): >

        '<,'>TCommentAs html_fancy_block

    If you're editing a html file, this could best be done by the <c-_>s    
    key map.
 
install details
Edit the vba file and type:

    :so %

See :help vimball for details. If you use vim 7.0, you may need to
update your vimball installation first.


Also available via git
http://github.com/tomtom/vimtlib/tree/master
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
tComment.vba.gz 1.10 2009-10-11 7.0 Tom Link - tcomment#Operator defines w:tcommentPos if invoked repeatedly
- s:GuessFileType: use len(getline()) instead of col()
tComment.vba.gz 1.9 2008-05-15 7.0 Tom Link - Fix left offset for inline comments (via operator binding)
tComment.vba.gz 1.8a 2008-05-02 7.0 Tom Link The file autoload/tcomment.vim was packed as autoload/tComment.vim. If you installed 1.8, please rename that file or remove autoload/tComment.vim and use this vimball.
tComment.vba.gz 1.8 2008-05-01 7.0 Tom Link - Definitly require vim7
- Split the plugin into autoload & plugin.
- g:TCommentFileTypes is a list (this could cause problems when updating in certain situations)
- Fixed some block comment strings
- Removed extraneous newline in some block comments.
- Maps for visal mode (thanks Krzysztof Goj)
tComment.vba.gz 1.7 2007-08-30 7.0 Tom Link - gc{motion} (see g:tcommentMapLeaderOp1) functions as a comment toggle
operator (i.e., something like gcl... works, mostly); gC{motion} (see
g:tcommentMapLeaderOp2) will unconditionally comment the text.
- TCommentAs takes an optional second argument (the comment level)
- New "n" map: TCommentAs &filetype [COUNT]
- Defined mail comments/citations
- g:tcommentSyntaxMap: Map syntax names to filetypes for buffers with
mixed syntax groups that don't match the filetypeEmbeddedsyntax scheme (e.g.  
'vimRubyRegion', which should be commented as ruby syntax, not as vim
syntax)
- FIX: Comments in vim*Region
- TComment: The use of the type argument has slightly changed (IG -> i,
new: >)
tComment.vba.gz 1.6 2007-05-01 7.0 Tom Link - Ignore sql when guessing the comment string in php files; tComment
sometimes chooses the wrong comment string because the use of sql syntax
is used too loosely in php files; if you want to comment embedded sql
code you have to use TCommentAs
- Use keepjumps in commands.
- Map <c-_>p & <L>_p to vip:TComment<cr>
- Made key maps configurable via g:tcommentMapLeader1 and
g:tcommentMapLeader2
tComment.zip 1.5 2006-03-22 6.0 Tom Link - "Inline" visual comments (uses the &filetype_inline style if
available; doesn't check if the filetype actually supports this kind of
comments); tComment can't currently deduce inline comment styles from
&comments or &commentstring (I personally hardly ever use them); default
map: <c-_>i or <c-_>I
- In visual mode: if the selection spans several lines, normal mode is
selected; if the selection covers only a part of one line, inline mode
is selected
- Fixed problem with lines containing ^M or ^@ characters.
- It's no longer necessary to call TCommentCollectFileTypes() after
defining a new filetype via TCommentDefineType()
- Disabled single <c-_> mappings
- Renamed TCommentVisualBlock to TCommentRight
- FIX: Forgot 'x' in ExtractCommentsPart() (thanks to Fredrik Acosta)
tComment.zip 1.4 2005-03-16 6.0 Tom Link - Fixed problem when &commentstring was invalid (e.g. lua)
- perl_block
- <c-_>s mapped to :TCommentAs <c-r>=&ft<cr>
tComment.zip 1.3 2005-03-14 6.0 Tom Link - slightly improved recognition of embedded syntax
- if no commentstring is defined in whatever way, reconstruct one from
&comments
- The TComment... commands now have bang variants that don't act as toggles
but always comment out the selected text
- fixed problem with commentstrings containing backslashes
- comment as visual block (allows commenting text to the right of the main
text, i.e., this command doesn't work on whole lines but on the text to the
right of the cursor)
- enable multimode for dsl, vim filetypes
- added explicit support for some other file types I ran into
tComment.zip 0.2 2005-01-11 6.0 Tom Link - Fixed uncommenting of non-aligned comments
- improved support for block comments (with middle lines and indentation)
- using TCommentBlock for file types that don't have block comments creates
single line comments
- the default key bindings have slightly changed
- removed the TCommentAsBlock command (TCommentAs provides its functionality)
- removed g:tcommentSetCMS
tComment.zip 0.1 2005-01-07 6.0 Tom Link Initial upload

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo