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>=...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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.
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 ...
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 ...
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)...
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>#...
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:
...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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## \<...
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 ...
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 '...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
comments × 112formatting × 15
vimscript × 12
vimrc × 10
neovim × 10
syntax-highlighting × 7
indentation × 7
folding × 7
plugin-vim-commentary × 7
key-bindings × 6
visual-mode × 5
plugin-nerdcommenter × 5
autocmd × 4
filetype × 4
filetype-markdown × 4
substitute × 3
colorscheme × 3
filetype-c++ × 3
alignment × 3
filetype-javascript × 3
regular-expression × 2
command-line × 2
cut-copy-paste × 2
highlight × 2
visual-block × 2