Question

A while ago, I had to put

filetype plugin on

in my .vimrc for a plugin I use.

But this caused a change in autoindent: Whenever I write a comment "//", and then press enter, vim autoindentation automatically enters another "//" in the next line.

// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there

What can I do to avoid this? I use the autoindent setting in my vim file. I already tried

filetype plugin indent off

but it does not work.

Was it helpful?

Solution

Take a look at :h formatoptions and :h fo-table. The options you need to turn off are r and o. Turning them off prevents vim from automatically inserting the comment leader (in this case "//") when you press enter in insert mode or when you press o or O in normal mode.

OTHER TIPS

See :help 'formatoptions' - I know how annoying this is!

Try this:

:set fo-=or

I am answering your title rather than the body of your question, since your title brings people to this page who are looking to stop Vim from indenting comments.

The variable that controls whether Vim auto-indents a new character is indentkeys. I've noticed incorrect indentation only in Python and Yaml, so I've turned off auto-indentation only for the "#" character at the beginning of the line: :set indentkeys-=0#

Since loading the filetype indentation plugin will override any .vimrc settings you've made, you can set up an autocmd to change the indentkeys after a file is created or loaded. Here are mine:

autocmd BufNewFile,BufReadPost * if &filetype == "python" | set indentkeys-=0# | endif
autocmd BufNewFile,BufReadPost * if &filetype == "yaml" expandtab shiftwidth=2 indentkeys-=0# | endif

See :h indentkeys

Note that because of (possibly) a bug, if you use Neovim you must also specify filetype plugin indent on, or the filetype won't be set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top