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.4k
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
  • 49.4k
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,706
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.4k
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.7k
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.4k
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.4k
8 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
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
  • 20.4k
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.7k
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.7k
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,600
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
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
  • 49.4k
5 votes
Accepted

How can I clearly document errorformat patterns in a plugin compiler file?

Settings like errorformat can be modified using the += and -= operators. You can take advantage of this by setting an initial value and adding to it, with a comment accompanying each addition. You ...
muru's user avatar
  • 24.7k
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
  • 49.4k
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
  • 49.4k
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,834
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
  • 49.4k
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
  • 31.3k
4 votes

How to toggle comments with ctrl+/?

Unfortunately, as far as I know Ctrl+/ cannot be bound to in vim. :( But if you're not attached to that particular mapping the rest is pretty simple. Here's an example that uses Ctrl+M (simply ...
Tumbler41's user avatar
  • 7,706
4 votes

visual mode gq not reflowing comment block

You need to check your formatoptions setting. According to the manual you need the "q" flag to be present: *fo-table* You can use the '...
Christian Brabandt's user avatar
4 votes
Accepted

Folding my .vimrc doesn't work as expected

Make the else if an elseif. Also I find it usually simpler to give the variable v:lnum as parameter to the foldfunction. This makes it easier to debug the function. Some hints: You can use :set ...
Christian Brabandt's user avatar
4 votes

vim comment out every line of text containing a pattern using the 'global' command

Try using :g/\\draw/norm gcc From :help norm: Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line. {commands} are executed ...
gandalf3's user avatar
  • 267
4 votes
Accepted

How to add JavaScript and JSX comments in the same .js file

You need the following plugins: vim-commentary vim-jsx, to distinguish javascript and jsx files vim-context-commentstring, to change commentstring depending on the (syntax?) region. Now, depending ...
Ralf's user avatar
  • 9,097
4 votes
Accepted

Vim doesn't extend Roxygen R comments

Create the file ~/.vim/after/ftplugin/r.vim (Windows: $HOME/vimfiles/after/ftplugin/r.vim) with the following content: setlocal formatoptions+=r This file is sourced, after Vims filetype specific ...
Ralf's user avatar
  • 9,097

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