I am writing python, javascript, html and other config files and I realize that when I enter newline to an unfinished line (i.e. unterminated string, still inside dictionary brackets, etc) I get double indentation.
How do I fix this?
How to Turn On Auto Indent in Vim. To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing.
How to Turn Off Vim Auto Indent. You can also temporarily turn off automatic indenting by typing :set paste in command mode. (:unset paste to turn it back on again.) This is useful — as you might guess from the command name — if you're pasting in chunks of text or code to avoid getting annoying extraneous indents.
vim Inserting text Disable auto-indent to paste code And if you want to paste as is from the clipboard. Just press F3 in insert mode, and paste. Press F3 again to exit from the paste mode.
There are a few variables you can set in your .vimrc
file to affect how Python is indented:
Indent after an open parenthesis: let g:pyindent_open_paren = '&sw * 2'
Indent after a nested parenthesis: let g:pyindent_nested_paren = '&sw'
Indent for a continuation line: let g:pyindent_continue = '&sw * 2'
For more info: :help ft-python-indent
See $VIMRUNTIME/indent/javascript.vim
: it uses cindent
to perform indentation. cindent
is affected by a number of options through the cinoptions
variable. Some of them are set by default to &shiftwidth * 2
, you might want to reset those.
The relevant option for your case seems to be +N
. In your .vimrc
file you should put something like:
set cinoptions+=+1
even though this seems to be the default already.
Again, see $VIMRUNTIME/indent/html.vim
: this performs the indentation via a custom expression. I had a quick look and it doesn't seem to be performing any double indentation anywhere, but I might be wrong. The global variables available for that doesn't seem to be relevant.
In the worst case, you might want to amend that file yourself and put it in your ~/.vim/indent/
.
In general, each file is indented according to its own criteria, have a look in $VIMRUNTIME/indent/
to understand if and how each of them can be configured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With