Prevent (g)vim from auto-indenting comments
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.
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" | set 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.
See :help 'formatoptions'
- I know how annoying this is!
Try this:
:set fo-=or