Skip to main content
Share Your Experience: Take the 2024 Developer Survey
16 votes
Accepted

How to comment in Vim?

Because "commenting out" is context-dependent, the behavior is left to plugins rather than the core Vim editor. Many plugins reuse the sequence gc to comment. vim-commentary (authored by ...
Vivian De Smedt'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.7k
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
  • 50.1k
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.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.7k
9 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
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.7k
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.8k
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
  • 17.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

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
  • 50.1k
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,854
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
  • 50.1k
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
  • 50.1k
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
  • 32.1k
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,756
4 votes

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

Add this in your vimrc: au BufEnter * set fo-=c fo-=r fo-=o Using FileType won't work since other plugins set formatoption.
Aura's user avatar
  • 161
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,257
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,257
4 votes
Accepted

How can I make Vimscript check if the current line has a comment?

To check if the current line is commented, you can use :echo synIDattr(synIDtrans(synID(line("."), col("$")-1, 1)), "name") This checks, if the current line ends with a comment syntax item. You ...
Christian Brabandt's user avatar
4 votes
Accepted

Alert sound when using <C-L> etc. mappings

It is a bad habit to do spaces and remarks in the same line of the mapping. I believe it includes them in the mapping and it causes the beep(about the remark I am not sure). So try to remove them and ...
eyal karni's user avatar
  • 1,136
4 votes
Accepted

Are there comments in command-line mode?

Vimscript does have comments and they're started by the " character. They always work at the beginning of the line: " echo usage echo "hello" They sometimes work inline, but not always. It depends ...
filbranden's user avatar
  • 29k
4 votes
Accepted

Commenting out an element of a multiline list

Or, in general, can you combine line continuations with comments? Yes, but it must consist of three symbols (see :h line-continuation-comment): quote, backslash and space. As it stands in your ...
Matt's user avatar
  • 20.8k
4 votes

Adding new filetypes with vim commentary

Make sure you vim-commentary works, e.g. you can open python file and gcc to comment current line. To add support for a new filetype -- filetype has to provide commentstring, so if you are the author ...
Maxim Kim's user avatar
  • 13.5k
4 votes
Accepted

Don't auto-extend c/c++ trailing comments

Add / to formatoptions set formatoptions+=/ as described in :h fo-table: *fo-o* o Automatically insert the current comment leader after hitting 'o' or 'O' in Normal ...
Maxim Kim's user avatar
  • 13.5k
3 votes
Accepted

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

You can use g/\\draw/s/^/%/ to insert a percent sign at the start of every line containing the text \draw. s/^/%/ means replace the empty string at the start of the line with a percent sign. If you ...
Jürgen Krämer's user avatar

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