17

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.

slm
  • 363,520
  • 117
  • 767
  • 871
mtk
  • 26,802
  • 35
  • 91
  • 130

5 Answers5

9

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
    MD  

endfunction 

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!

Juve
  • 203
  • 2
  • 6
6

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.

slm
  • 363,520
  • 117
  • 767
  • 871
  • 1
    I use the workaround :.,+3s/^/"/ where dot represents current line and +3 represents amount of consecutive (after the dot line) lines. – daGo Oct 09 '19 at 13:53
3

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

https://github.com/tpope/vim-commentary

This plugin provides:

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

Usage:

  • 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.
Weston Ganger
  • 1,856
  • 1
  • 10
  • 10
2

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.

lgeorget
  • 13,656
  • 2
  • 41
  • 63
  • Thanks but I was looking for what exactly is the block comment style in _vimrc_. – mtk Jun 03 '13 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 03 '13 at 12:40
1

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 
                defined) 
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).

laebshade
  • 2,136
  • 13
  • 17