101

In Vim, if I'm typing a comment in a code file, and I hit Enter, it automatically makes the newline a comment, too.

For instance, in a Ruby file:

# I manually typed the pound at the start of this line and hit enter.
# This line formatted itself this way automatically.

Generally, this is what I want, but not always. How can I temporarily turn off this auto-commenting behavior?

3
  • By temporarily, do you want a a command that does it a single time, or set some option that will last for a few commands until you turn it back on? Possible cross site duplicate: stackoverflow.com/questions/4896003/… Commented Jan 22, 2014 at 9:34
  • 9
    I've seen it elsewhere but I'll add it here: "Whoever thought this was a good idea as a default should have their fingers broken." Commented Dec 16, 2019 at 18:57
  • This wiki talks about this.
    – zyy
    Commented Jul 9, 2020 at 15:53

9 Answers 9

127

I think you're looking for

:set formatoptions-=cro

From :help fo-table:

You can use the 'formatoptions' option  to influence how Vim formats text.
'formatoptions' is a string that can contain any of the letters below.  The
default setting is "tcq".  You can separate the option letters with commas for
readability.

letter  meaning when present in 'formatoptions'

t       Auto-wrap text using textwidth
c       Auto-wrap comments using textwidth, inserting the current comment
        leader automatically.
r       Automatically insert the current comment leader after hitting
        <Enter> in Insert mode.
o       Automatically insert the current comment leader after hitting 'o' or
        'O' in Normal mode.
...
11
  • 26
    Thank you so much! This behavior was bothering me-- I came from unix vi to Linux vim. For googlers: For permanent use, see also stackoverflow.com/questions/6076592/… and enter this line into /etc/vimrc:autocmd BufNewFile,BufRead * setlocal formatoptions-=cro
    – bgStack15
    Commented Jul 23, 2014 at 14:20
  • 3
    Didn't help; typing /**<ESC>o still adds a star on the next line. Commented Sep 6, 2016 at 3:06
  • 5
    Doesn't work. z
    – felwithe
    Commented Sep 19, 2019 at 12:06
  • 4
    Also didn't work for me. I believe the issue is that -=cro only works if formatoptions contains the flags exactly in this order. set formatoptions-=c formatoptions-=r formatoptions-=o works better for me. Commented May 27, 2020 at 12:09
  • 2
    Didn't work for me.
    – daedsidog
    Commented Apr 27, 2021 at 13:53
16

Temporarily setting the 'paste' option may do what you want, but it also disables a lot of other Vim features:

Use :set paste to turn it on and :set nopaste to turn it off. Alternatively, you can use :set paste! to toggle it.

See also:

:help 'paste'
:help 'pastetoggle'

(Those commands are typed with the single-quotes.)

2
  • 4
    :set paste to turn it on and :set nopaste to turn it off
    – User
    Commented May 23, 2014 at 1:55
  • 2
    alternatively you can use :set paste! to toggle it TRUE and FALSE Commented Jun 24, 2014 at 3:37
12

An alternative is to just hit ctrl-w after you pressed enter. This deletes the previous word (e.g. # or //) when in insert mode. In fact, ctrl-w is a pretty universal shortcut in most applications.

1
  • 3
    > In fact, ctrl-w is a pretty universal shortcut in most applications It means "Close Window" (or tab) in most applications I use. Sucks for vim bindings in jupyter, e.g.
    – Cogwheel
    Commented Sep 13, 2021 at 21:37
5

I enter non-formatted plain new lines with <CR>.

When I want to continue typing the next line in the commented block I just use the O key as usual.

Try this:

nnoremap <silent> <cr> :set paste<cr>o<esc>:set nopaste<cr>
2
  • 1
    What do you mean by <CR>? A particular key? Commented Jul 5, 2018 at 21:32
  • 1
    CR stands for carriage return. On the computers I have used it has been mapped to the Enter key. Commented Jul 6, 2018 at 2:15
5

To permanently disable this behavior, add autocmd FileType * set formatoptions-=cro in your .vimrc/init.vim.

1

I have ended up with this:

nnoremap <Leader>o o<Esc>^Da
nnoremap <Leader>O O<Esc>^Da

It appends a new line, deletes everything already inserted there, and leaves the cursor in insert mode in the indented column, without messing with the format options.

1

This answer applies to Debian and some of its derivatives.

On a Debian distribution Vim defaults are unreasonable. They are located in /usr/share/vim/vim80/defaults.vim and applied after(!) /etc/vim/vimrc is run. The only way to tell Vim not to use its defaults is to ensure ~/.vimrc exists even if it is blank. Vim on startup tries to read from .vimrc, but if the file is not found it applies the defaults which brings a lot of undesirable behavior e.g. mouse integration, copy-paste quirks, comment auto-wrap, etc.

On Debian you can fix ALL that by simply running touch ~/.vimrc

1

This just makes it one line command which when you run this, it will disable continuation of comment permanently

echo 'au FileType * set fo-=c fo-=r fo-=o' >> ~/.vimrc
1
  • 3
    This answer would be better simply as: Add autocmd FileType * set formatoptions-=cro to your .vimrc file. But +1 non-the-less - this was the only answer that worked for me here! :D
    – user2121
    Commented Jun 19, 2020 at 12:00
1

If you just want to temporary disable this feature, I think the easiest way is to make a key map. <C-u> can delete content of current line to the beginning, keep indent space, so I created a key map in my .vimrc:

inoremap <silent><expr> <C-CR> "\<CR>\<C-u>" 

This map is for insert mode, you can create a normal mode key map if you want, like below:

nnoremap <silent><expr> <C-CR> "o\<C-u>"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .