81 votes
Accepted

How can I get vim to stop putting comments in front of new lines?

This is controlled with the formatoptions setting; from :help fo-table: You can use the 'formatoptions' option to influence how Vim formats text. 'formatoptions' is a string that can contain ...
37 votes
Accepted

How to comment out a set of lines which are selected in visual mode?

If you want language specific commenting you will need a plugin like nerdcommenter. Alternatively, although it doesn't answer your actual question, you can use builtin vim actions and your knowledge ...
  • 1,037
26 votes

How to comment out a set of lines which are selected in visual mode?

Using Visual Block mode (CtrlV), select the beginning of the lines. Then hit I# (that's a capital letter I) to insert the hash character on each of those lines. Then press Esc to return from Insert ...
  • 8,859
25 votes
Accepted

Adding 80-column wide comment header block with centered text

Here's a slightly more efficient / easier method. Type it with the cursor at the beginning of the line of text (i.e. LOAD) that you want to center. :center 80<cr>: center the text with Vim's ...
  • 13.9k
19 votes

How to comment out a set of lines which are selected in visual mode?

If you just need a quick solution for whatever language you're currently in, and you already have the text selected in visual mode, then :norm 0i# does the job. (For each line, in normal mode, go to ...
  • 2,557
13 votes
Accepted

How to toggle comments with ctrl+/?

It all depends on how extensive you want it to be. What type of files are you editing? I have the following in my .vimrc that works with python files: let Comment='#' xnoremap # :norm 0i<C-r>=...
  • 16.8k
12 votes
Accepted

How to comment a line in vim?

One way would be to map the keystrokes you would use to a key combination: nnoremap gcc I/* <ESC>A */<ESC> In normal mode, when pressing gcc, this will go to the beginning of the line and ...
  • 435
11 votes

Uncomment multiple lines

I do approve @mMontu comment suggesting to use a comment plugin (vim-commentary is an option but NERDCommenter has my preference over vim-commentary). But you could do it in other ways: First if all ...
  • 46.8k
10 votes

How to comment out a set of lines which are selected in visual mode?

Doing it automatically would require you to add something like the following to your vimrc file (source): au FileType haskell,vhdl,ada let b:comment_leader = '-- ' au FileType vim let b:...
  • 885
10 votes

Hide certain lines completely (not folding)

I don't know how to achieve exactly what you want, only a part. You want to hide some text inside ~/.vimrc (for example). First we need to know which highlight group handles the text you want to ...
  • 6,506
10 votes

Uncomment multiple lines

I like the approaches statox suggested. Here's another one: 52:norm x This only works if the # is the first character on each line. Otherwise, I would do 52:s/# These two work very similarly. ...
  • 16.8k
9 votes
Accepted

Is it possible to add inline comments in a multiline statement?

Not directly. But: if the comment applies to the whole line, you better place it above the line. If the comment(s) are each to indicate something about a portion of the line, you could consider ...
  • 9,340
9 votes
Accepted

How can I create a /* comments section with /// like in XCode?

The method in my earlier answer doesn't transform well to dynamic content. This is where snippet plugins like UltiSnips and SnipMate come in. I'll provide a demo of UltiSnips here. Install it using ...
  • 24k
9 votes
Accepted

Toggling comments on and off

if you're willing to use a plugin, there are many commenting plugins out there. Some of the most popular examples are: tpope/vim-commentary tomtom/tcomment_vim scrooloose/nerdcommenter All of these ...
  • 16.8k
8 votes
Accepted

Why are new lines under commented lines automatically commented too?

This has nothing todo with the normal command or the visual block. This is due to a setting 'formatoptions', see :help 'formatoptions'. In particular, the mentioned behaviour comes from the flags r ...
8 votes

How to generate patterns for comments?

Please don't do that: This type of title comments is just too heavy and doesn't improve the readability of your code. If you really need to create a section use light multi lines comments like: " " ...
  • 46.8k
8 votes
Accepted

visual mode gq not reflowing comment block

In my vimrc file I had set paste turned on. When I removed this configuration I was able to get comments to flow correctly.
  • 201
8 votes
Accepted

How do I comment out lines without changing indentation?

Alternatively, you could use the :g command. I would do: :g/\s*#pdb.set_trace()/normal ^x to uncomment and :g/\s*pdb.set_trace()/normal I# to comment. This is nice because it's more readable (and ...
  • 16.8k
6 votes
Accepted

Vim "gq" command doesn't recognize Haskell-style comments

:set comments+=:-- This appends :-- to the value of comments, which adds -- as a comment line indicator. The format of this option is described by :help format-comments.
  • 990
6 votes

How to comment out a set of lines which are selected in visual mode?

After you have selected the lines, simply type :norm I# : will automatically put '<,'> on your command line, which is a range from the start of your selection to the end; norm executes a ...
  • 231
6 votes
Accepted

Is it possible to put vim comments in lines that include `iabbrev`

As explained in :help 40.1 (SPECIAL CHARACTERS): It is not possible to put a comment directly after a mapping, because the " character is considered to be part of the mapping. You can use |", this ...
  • 6,470
6 votes
Accepted

Automatic comment leader not inserted on some file types

You might have to set a custom comments. For me, comments in a Python file is: comments=b:#,fb:- With some experimenting, I found that, for Vim to insert # after lines with ##, I needed to use a ...
  • 24k
6 votes

How can I create a /* comments section with /// like in XCode?

One way would be to create a file containing this snippet, and read it when you type ///. For example, create ~/.vim/snippets/my_header.snip containing this header. Then define this mapping: ...
  • 24k
6 votes
Accepted

Disable absolutely all auto-comments, for real

The culprit is $VIMRUNTIME/ftplugin/c.vim, and likely all other standard ft plugins. If you want everything they define but the setting for 'formatoptions', I don't see any simple solution. (Just in ...
  • 16.2k
6 votes
Accepted

How can I return the current filetype's comment character in vimscript?

I usually go with the 'commentstring' option and depend on the filetype plugin to set this up correctly for me (if not, that should be added to the corresponding filetype to make at least folding work)...
5 votes
Accepted

write a key command for a markdown comment

Vim creates marks for the beginning and the end of the last visual selection in the current buffer. You can use '< to move to the beginning and '> to move to end. In this way you can ...
  • 3,209
5 votes

Adding 80-column wide comment header block with centered text

First, you need to :set expandtab. 80i#Esc to create the top line Yp to duplicate it OSpaceLOADSpaceEsc :center 80 (The 80 may be omitted, depending on your textwidth setting.) hhv0r# to fill the ...
  • 8,859
5 votes

How to comment out a set of lines which are selected in visual mode?

I use scrooloose/nerdcommenter for this. With this plugin you can visual select your lines and hit leader+c to toggle comments. Depending on the file type it will use different symbols for commenting....
  • 3,296
5 votes

How to comment out a set of lines which are selected in visual mode?

I find the vim-commentary plugin is by far and away the easiest way to do this. Select a range of lines, then just hit gc. It will use an appropriate commenting character for the filetype you have ...
5 votes

How to generate patterns for comments?

This is a perfect use case for ultisnips with Python interpolation. A sketch of a snippet for the first header might look like this: global !p from math import floor, ceil def left(lng, fill, s): ...
  • 3,889

Only top scored, non community-wiki answers of a minimum length are eligible