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
To turn off autoindent when you paste code, there's a special "paste" mode. Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) -- . After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.
In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.
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.
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.
$VIMRUNTIME/indent/
~/.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.
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
'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' 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.
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.
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.
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.
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.
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