Auto-indent after carriage return after line containing open curly brace

I recently upgraded my Cygwin from a 2015 installation (5 years ago) to the current "2020" version. Gvim now automatically indents by shiftwidth columns when I press return after a curly brace { on a line by itself. From an initial web search, I think this might be related to the cindent option, so I issued the command set nocindent. The behaviour still persists. Can someone suggest what help page to look at next?

Background: I've been using autoindent for years, but that only matches the indentation of a new line to the indentation of the preceding line, which is what I want. What I'm trying to disable is indentation that is shiftwidth more than in the preceding line. I am using the same vimrc file as before the 2020 upgrade of Cygwin.

Here are some key options in my vim session:

indentexpr=GetTeXIndent()
tabstop=3
shiftwidth=3
nocindent
nosmartindent
filetype=tex


Here is a minimum working example (MWE) file test.tex that exhibits my problem:

% test.tex
%---------
%        1         2
%234567890123456789012345   <-- Physical character column number
navigation and direct manipulation of the data.\footnote
{%


I position my cursor on the { in column 7 and, in normal mode, press S or C to enter insert mode (and overwrite the text). I then enter {%<CR>, which creates a new line and positions the insertion point on column 10.

Even as I was typing the preceding paragraph and this paragraph, whenever I press <CR> to start a new line following a line containing the { character, the insertion point gets indented to column 4 instead of being positioned on column 1. (Yes, all of this text was composed in the file test.tex). The :map command shows that the key { is not mapped to anything.

For those who have tried the MWE, even if you don't know the answer, it would be helpful in identifying the cause of the problem to know whether you get the same behaviour.

New info

The unwanted extra indentation only afflicts a text file if the file name extension is *.tex, not if it is *.txt. It seems that I need to delve into how syntax-dependent behaviour controlled by the file type. I peeked into /usr/share/vim/vim81/syntax/tex.vim, but I think that deciphering it will be a long-term endeavour.

• What does :set indentexpr? say? How about :set smartindent?? And finally :set ft?? Please edit your question with the answers to these. – filbranden Feb 28 '20 at 22:38
• If you're editing a file with source code for any C-like language (C, C++, Java, etc.) then what you describe is, I believe, the default behavior. I don't recall this being something introduced in the last 5 years but I'm not 100% sure. You are using the same vimrc as you did with the previous version? (If you're not editing source code, just regular text, then that behavior would be strange/incorrect for default config.) – B Layer Feb 29 '20 at 1:05
• Thanks, filbranden & B Layer. I added the requested details into the question. I am using the same vimrc file with the exception of mapping <C-2> through <C-12> to different font setting commands, since different fonts are installed in the 2020 Cygwin installation of X-windows. – user2153235 Feb 29 '20 at 5:17
• This seems rather default for latex-indenting to, no? – D. Ben Knoble Feb 29 '20 at 16:06
• @D. Ben Knoble: It wasn't be behaviour before the 2020 upgrade. In any case, I would like to disable it. It's causing problems in that a new line is indented even if the { in the preceding line is in the middle of the line and in quotes. – user2153235 Feb 29 '20 at 18:53

This behavior is being enabled by the custom indentation function for the tex filetype.

You can configure the behavior of that function, by setting some variables in your vimrc.

For your specific case, to disable indentation after braces, you can use:

let g:tex_indent_brace = 0


The tex indentation also indents \item elements in a special way, which you can disable with:

let g:tex_indent_items = 0


If you want to completely block Vim indentation for filetype tex, one good way to do it is to create an indent/tex.vim file that supercedes the one you want to block.

You can create a file ~/.vim/indent/tex.vim, which comes earlier than the one in Vim runtimes in the search path, and add the following contents to it:

let b:did_indent = 1


This way, when the one you want to block gets to run, it will see that indentation is already configured and so it will skip it.

(That's better than messing with permissions or removing the indent/tex.vim in runtimes, since an operating system update will probably revert those changes.)

Note that you can't really avoid the indentation function by unsetting indentexpr from your vimrc. The indentation plugin will always load when the filetype is determined, which happens much later than your vimrc.

Disabling indentation plugins altogether from your vimrc will work, so filetype plugin off works as long as nothing else is re-enabling it later on. (Check with a plain :filetype, which will tell you which items are enabled.)

It's unclear why you started getting these starting with an upgrade, since that code in indent/tex.vim seems to be around for quite a long time... Perhaps you didn't have the Vim runtime files installed and now you do?

Another possibility is that, starting with Vim 8, if you don't have a vimrc file, a default one will be loaded, which will most likely enable all filetype support and have these plug-ins start to load when previously they wouldn't. See :help defaults.vim for details on that. In any case, the solution for that one is simple, just create a vimrc file, even if it's an empty one, that will prevent you from getting the settings from the defaults file.

• Thanks for that explanation and solution, filbranden. Unfortunately, I am unable to replicate my problem. I started a virgin Gvim session on test.tex -- no extraneous indenting. I tried started Gvim on test.txt and then issuing :e test.tex -- nada. In both cases, my vimrc did not contain set indentexpr=. Without the ability to replicate the problem, I can't mark your answer as the answer, but I will certainly upvote it, if I have enough points. Thanks again. – user2153235 Mar 1 '20 at 23:49
• @user2153235 Well, if the problem is not happening anymore I guess that's a good sign... Anyways, if it comes back, use the tips in here to troubleshoot it. Or feel free to leave a message here if that happens again! – filbranden Mar 1 '20 at 23:54
• This is driving me totally bananas. Now it's starting to extraneously indent after a line containing \item by itself. All I want to do is write a report that requires some really deep thinking, and I'm fighting the vagaries of an dictatorial package. As soon as I get uncommitted time, I will seek out how to turn off all file-type dependent indentation (but keeping the syntax highlighting heuristics). – user2153235 Mar 2 '20 at 2:26
• OK, thanks. BTW, the extraneous indenting after \item did not occur in a session in which the mksession file was sourced. I think the hack only works if you start a new session anyway, and time is still needed to tell whether it truly prevents all manner of extraneous indenting.... – user2153235 Mar 2 '20 at 2:58
• Thanks, filbranden. There are some places and machines from which external connectivity is very limited (not necessarily in terms of bandwidth). So I'm OK carting around a vimrc, as long as it's the same one. Probably better than a true single source, so that if it breaks on one location, I can roll back and take my time figuring out the cause. As for the original indent problem, I used ~/.vim/indent/tex.vim. Thanks again! – user2153235 Mar 11 '20 at 1:28

Based on this page, I found that :set indentexpr= removed tex file-dependent extraneous indentation. I intend to put it in my vimrc file.

Just a cautionary note: As filbranden said, this solution might not be persistent. If you are able to get any of his solutions working, it will less of a hack than :set indentexpr=. If for some reason you are running into problems, however, and don't have the time to troubleshoot and experiment, then :set indentexpr= might get you going for the immediate present.

To possibly save others time, based on this page, I also tried the following without success:

filetype indent off
filetype indent plugin off

• Setting indentexpr= from your vimrc won't work, since that will be overridden by the filetype plugin when it loads later on. Using filetype indent off should work. But there's a more specific solution to address your issue with braces only... See my answer. – filbranden Mar 1 '20 at 16:12