Disable all auto indentation in vim
Asked Answered
S

7

29

In TeX vim usually screws up my indentation. Mainly when I'm in a displayed equation which I think should look like this:

\[
    x=\frac{y}{z}  
\]

where the whitespace infront of the x is one tab.

When I start type the equation I type the \[ and \] marks first and then go back between them, typing the tab and then the rest of the equation.

Vim doesn't do anything wrong until I have to use something that incorporates curly braces (\frac{} for example). When I type the closing } vim automatically shifts the indentation for the whole line to the left, which undoes my typed tab.

This is very anoying, how do I disable it?

my .vimrc contains:

"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
Sipe answered 13/8, 2011 at 21:59 Comment(8)
Which version is this? I can't reproduce it in 7.3. I tried setting filetype to tex, enabling your options, and typing in your sample. The closing bracket did not change indentation level.Sporades
Accolade is a somewhat obscure term for curly braces ({, }); it's probably more common in Europe than in the US.Suavity
@Keith, o yeah, sloppy english there... I'll fix itSipe
Has there been a satisfactory solution to this problem? No matter what variant of indent I unset, the problem remains. Perhaps it's a limitation of $VIMRUNTINE/indent/tex.vim?!Tarrel
@Dominique: I now use noautoindent, nocindent and smartindent. I'm not quite sure why this works the best, but trial and error shows that this is the most satisfactory.Sipe
@romeovs: That combination still unindents in my case when I open and close a pair of braces.Tarrel
@romeovs: My fault. The combination you suggest works. I voted up your comment, which should really be considered as the answer to your question.Tarrel
People often forget indentexpr, which if set, overrides smartindent and cindent.Wheat
T
20

There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it set noautoindent by hand, or write it in your _vimrc.

There are two other automatic kinds of indentation, cindent and smartindent. Similarly, if you wish to disable them go with set nocindent and set nosmartindent

If you look in help (help autoindent, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.

Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.

Tridactyl answered 13/8, 2011 at 22:14 Comment(5)
For me, cindent is the one that usually causes the problemSquires
For me, removing filetype plugin indent on from my .vimrc helped.Strom
set nocindent and set nosmartindent in vimrc on cygwin (windows 7) fixed indention upon O and o new line inserts. that was so frustrating that it wouldn't just put an empty line in. thank you!Fontaine
There are four automatic kinds of indentation: autoindent, cindent, smartindent, and indentexprCoulee
All of these failed when editing a file with ".css" filename - current (2020) vim is stuck indenting (incorrectly) CSS files out of the box. But I discovered a different command - ":set paste" which does what this answer failed to do, and actually disables the broken autoindenting (temporarily).Quadragesimal
O
39

I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove filetype indent on from your vimrc!

This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.

File Specific Indent Settings

So if you're like me, you probably had filetype indent on in your vimrc and had no idea what it was doing.

All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.

  1. $VIMRUNTIME/indent/
  2. ~/.vimrc/after/indent/

The first place holds the default indent rules that come with vim. If you were to set filetype indent on on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file called index.html in would get the rules from $VIMRUNTIME/indent/html.vim.

In my experience, these default rules are pretty darn good, but they can get messed up by other settings.

The second place (the after directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.

Flavors of Indentation

There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:

autoindent

'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.

I use filetype indent on and set autoindent in my vimrc, since they work well together. I don't have the others set.

smartindent & cindent

'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax. 'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.

When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.

Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.

indentexpr

Runs filetype indent scripts found in (vimfolder)\indent\\(indentscripts). It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):

Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

Troubleshooting

There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily verbose will tell you which file was the last to change the option in question.

:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?

You may get a result such as

indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim

If that happens, you can move that file, close and open vim, and see if it fixes your problem.

Turning Off Indent Settings for TeX

Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the after directory.

I don't know much about Tex or LaTex, but when I created a file with the .tex extension and ran :filetype it had the filetype as plaintex. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim. In that file:

set autoindent&
set cindent&
set smartindent&
set indentexpr&

This will set all these values to their defaults whenever you open a .tex file.

Overspill answered 7/9, 2013 at 9:12 Comment(1)
Thanks for the answer! Is there a way to turn off indent settings for TeX within the .vimrc file?Lipocaic
T
20

There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it set noautoindent by hand, or write it in your _vimrc.

There are two other automatic kinds of indentation, cindent and smartindent. Similarly, if you wish to disable them go with set nocindent and set nosmartindent

If you look in help (help autoindent, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.

Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.

Tridactyl answered 13/8, 2011 at 22:14 Comment(5)
For me, cindent is the one that usually causes the problemSquires
For me, removing filetype plugin indent on from my .vimrc helped.Strom
set nocindent and set nosmartindent in vimrc on cygwin (windows 7) fixed indention upon O and o new line inserts. that was so frustrating that it wouldn't just put an empty line in. thank you!Fontaine
There are four automatic kinds of indentation: autoindent, cindent, smartindent, and indentexprCoulee
All of these failed when editing a file with ".css" filename - current (2020) vim is stuck indenting (incorrectly) CSS files out of the box. But I discovered a different command - ":set paste" which does what this answer failed to do, and actually disables the broken autoindenting (temporarily).Quadragesimal
T
4

The following command finally stopped VIM from pretending it knows how to indent files for me and only do what I explicitly tell it:

:setl noai nocin nosi inde=

Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting

Textualism answered 23/2, 2022 at 13:24 Comment(2)
The command :se paste works best for me, courtesy of the same page. Just remember to :se nopaste to restore normal indenting behavior when done pasting.Goatsbeard
I don't write code in vim so I don't really need any indenting behavior.Textualism
F
3

For anyone else having a similar problem, a solution that worked for me was:

  1. Use :verbose set indentexpr? to find what file was causing the de-indentation
  2. Find where indentexpr is changed (for me it was setlocal indentexpr=GetTeXIndent())
  3. Change that line to setlocal indentexpr& to turn indentexpr off

This removed all de-indenting from brackets, parentheses, and braces.

Fash answered 11/8, 2016 at 0:2 Comment(0)
D
2

Remove the lines set autoindent and set smartindent to remove all vim autoindentation.

Dodona answered 13/8, 2011 at 22:2 Comment(4)
This is not what autoindent does. It is just an indentation style that copies the indentation level of the preceeding line.Sporades
I was going based on the title of the question, "Disable all auto indentation in vim". Copying the indentation level of the preceding line, while not technically "smart" indentation, is still automatic indentation.Dodona
It is not what smartindent does either. This one reduces the level of indentation for lines that begin with a closing brace.Sporades
... which is still "automatic" indentation. I tried to be specific by stating in my answer that removing those lines would remove vim autoindentation.Dodona
M
0

If you are using the vim-latex plugin, set this option:

let g:tex_indent_brace=0

For other plugins, if you don't want to turn off indentexpr as in the above answers, you can find where indentkeys is set and comment out those lines. This should stop triggering re-indent when you type a closing brace.

:verbose set indentkeys?
Michell answered 20/11, 2020 at 19:2 Comment(0)
S
0

Here's the solution I settled on:

pico <myfilename>

;)

Streamlined answered 25/6 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.