I don't want to comment lines of text in a text file or code. I am looking for inserting block comments in a .vimrc file. To insert a single line comment " is used, for example:

" remove spaces and tabs as well  at the end of line automatically while saving
autocmd BufWritePre * :%s/\s\+$//e

Now I've got a relatively large amount of settings/configs/plugins collected over the years after browsing through the internet to add cool features. I'm looking to add some large comment blocks to my .vimrc file. Adding " to each line is a option but I am looking for a block comment syntax. Similar to /**/ in C.

// is identical to "
/* */ is identical to _____ ?

Searching Unix.SE, SO and googling didn't help much.

  • 1
    I suspect multi-line comments feature does not exist in vim-config files.
    – Dilawar
    Jun 3, 2013 at 7:56

5 Answers 5


My solution is to wrap the code inside a function.

This works well in vimrc, e.g., for in-place heredoc syntax highlighting tests, which also require real block comments without leading characters. In my vimrc, I use such a block directly after setting up my heredoc SyntaxRanges.

function! _blockcomment()

    " free text comment
    You can write free text here,
    but vim will try to highlight it as vimscript!

    " markdown heredoc
    test <<MD
    ### Nevertheless ###
    * for testing my fuzzy SyntaxRange heredocs
    * having no leading chars is essential
    * and the blockcomment function does the trick


This solution is similar to @Stéphane's if 0 trick, which did not work for me. Just make sure to never call this function!


I do not think this is possible. The best you'll be able to do is block select some text, and do a search/replace on the first character s/^/"/ in vim to insert a " to the beginning of each line.

The vim plugin NERD Commenter might help make this easier as well.

  • 1
    I use the workaround :.,+3s/^/"/ where dot represents current line and +3 represents amount of consecutive (after the dot line) lines.
    – daGo
    Oct 9, 2019 at 13:53

There is this life changing plugin by tpope called vim-commentary


This plugin provides:

  • Sanity
  • Properly indented comments
  • Does not comment out empty/unnecessary lines


  • Install via Vundle (or Pathogen I guess).
  • Highlight your text and press : which will show as :<,'>
  • Type Commentary here :<,'>Commentary and press enter.
  • Bom. Your done bud.

One trick:

  • place your cursor on the first character of the first line to comment
  • press Ctrl-V to enter in Visual Block mode
  • get your cursor down until the last line to comment
  • press I (shift+i) to enter in conditional insert mode
  • press '" ' to comment (a quote and a space)
  • press Esc to quit insert mode

All your selected lines are now prepended by the string you typed in insert mode.

  • Thanks but I was looking for what exactly is the block comment style in vimrc.
    – mtk
    Jun 3, 2013 at 12:27
  • I don't think there is such a feature in vim. By the way, it's not so common. In a lot of script languages, it's more usual to use single-line comments on a range of lines.
    – lgeorget
    Jun 3, 2013 at 12:40

Use the tComment plugin for Vim: http://www.vim.org/scripts/script.php?script_id=1173

Yes, it does support Python (added in 2011).

description :TComment works like a toggle, i.e., it will comment out text that contains uncommented lines, and it will uncomment 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 based on the values of &commentstring or &comments. For some filetypes, the comment definition is explicitly defined. You can |tcomment#DefineType()| to add your own definitions.

TComment knows how to deal with embedded code of a different filetype than the main filetype, e.g., ruby/python/perl regions in vim scripts, HTML or JavaScript in php code etc.

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 
gcc          :: Toggle comment for the current line 
gC{motion}   :: Comment region 
gCc          :: Comment the current line 

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 _ as leader (more preferable on terminals).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.