As of May 31, 2023, we have updated our Code of Conduct.
21 votes
Accepted

How to fold comments?

Folding only the comments is fairly easy by using foldmethod=expr: set foldmethod=expr foldexpr=getline(v:lnum)=~'^\\s*'.&commentstring[0] This will simply check if the line starts with any ...
Martin Tournoij's user avatar
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>=...
DJMcMayhem's user avatar
  • 17.2k
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 ...
Jakube's user avatar
  • 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 ...
statox's user avatar
  • 48.5k
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 ...
saginaw's user avatar
  • 6,626
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. ...
DJMcMayhem's user avatar
  • 17.2k
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 ...
VanLaser's user avatar
  • 9,530
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 ...
muru's user avatar
  • 24.5k
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 ...
DJMcMayhem's user avatar
  • 17.2k
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 ...
Karl Yngve Lervåg's user avatar
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: " " ...
statox's user avatar
  • 48.5k
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.
jmreicha's user avatar
  • 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 ...
DJMcMayhem's user avatar
  • 17.2k
7 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)...
Christian Brabandt's user avatar
7 votes
Accepted

Create indented comment box vim

Well, "put text into a box" is a well-known Vim excercise. I'm not sure where I saw it for the first time, but the most elegant solution, IMO, is to use :h v_r So it becomes: nnoremap <leader>#...
Matt's user avatar
  • 19.6k
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.
Lithis's user avatar
  • 1,020
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 ...
mMontu's user avatar
  • 6,550
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 ...
muru's user avatar
  • 24.5k
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: ...
muru's user avatar
  • 24.5k
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 ...
Luc Hermitte's user avatar
  • 16.7k
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): ...
Sato Katsura's user avatar
  • 3,949
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 ...
jjaderberg's user avatar
  • 3,339
5 votes
Accepted

Edit comments as if separate lines?

You might be interested by the tabular plugin it allows to align text based on patterns. In your case if you have some text %comment some more text %another comment &c. %=etc. with your ...
statox's user avatar
  • 48.5k
5 votes
Accepted

How can I change/delete comments easily?

What you are looking for is a text-object representing a comment. See :h text-objects for more information about text-objects. By default Vim doesn't contains text-objects representing comments but ...
statox's user avatar
  • 48.5k
5 votes

How do I comment out lines without changing indentation?

There is a solution which can be pretty simple if you are willing to use a plugin: NERDCommenter. This plugin is made for "intensely orgasmic commenting" according to their github page. More ...
statox's user avatar
  • 48.5k
5 votes
Accepted

gqip for VHDL comments does not work

You should tell Vim what a comment is using: :set comments+=:--,b:-- Explanation: The comments option contains a comma separated list of strings that can start a comment line. :-- specifies the ...
Jair López's user avatar
  • 1,794
5 votes
Accepted

How to comment a line directly from bash using vim?

You can use: vim +'normal! 2GI;' +'x' path/to/your/file The + parameter allows to execute a command after opening the buffer. The first command normal! 2GI; goes to line 2 and add a ; at the ...
statox's user avatar
  • 48.5k
5 votes
Accepted

How to add inline comment for the echo command?

The problem with this is, echo is expecting an expression. You can read what an expression is at :h expression in great detail. In short this means, :echo expects either a variable or a string (which ...
Christian Brabandt's user avatar
5 votes
Accepted

Vim Comment Box Macro

You don’t need to know the length of the line. This ought to do it: :let @a = 'yyPVr#jpVr#' I’d also automate the adding of the hashes at the start and end of the middle line: :let @a = "I## \<...
Rich's user avatar
  • 30.4k
5 votes
Accepted

Comment-out in vim with Shift-i not working

It depends on the way you entered visual mode. If you used V for line based visual selections, I will only enter insert mode for the very first line. However when you used Ctrlv, I will also take you ...
Christian Brabandt's user avatar

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