9

I want to disable autoindent and smartindent for LaTeX so I did

:set noai noci nosi

When I open a brand new mwe.tex, filetype=plaintex and there is no auto indent magic.

When I then enter some code, such as \begin{frame}\end{frame}, save and reopen the file, filetype=tex, colours have changed, and... there is what to me looks like both auto indent and smart indent going on:

  • I start typing \begin{frame} at the beginning of the line, so far so good
  • I press enter, and the cursor is indented by four spaces, despite setting noai, noci, and nosi. At this point, :set shows:
--- Options ---
  background=dark     helplang=en         shiftwidth=4        ttyfast
  commentstring=%%s nomodeline            showmatch           ttymouse=xterm
  expandtab           modified            suffixesadd=.tex
  filetype=tex        ruler               syntax=tex
  foldmethod=indent   scroll=34           tabstop=4
  backspace=indent,eol,start
  comments=sO:% -,mO:%  ,eO:%%,:%
  define=\\\([egx]\|char\|mathchar\|count\|dimen\|muskip\|skip\|toks\)\=def\|\\font\|\\\(future\)\
=let\|\\new\(count\|dimen\|skip\|muskip\|box\|toks\|read\|write\|fam\|insert\)\|\\\(re\)\=new\(boo
lean\|command\|counter\|environment\|font\|if\|length\|savebox\|theorem\(style\)\=\)\s*\*\=\s*{\=\
|DeclareMathOperator\s*{\=\s*
  fileencoding=utf-8
  fileencodings=ucs-bom,utf-8,default,latin1
  include=\\input\|\\include{
  includeexpr=substitute(v:fname, '^.\{-}{\|}.*', '', 'g')
  indentexpr=GetTeXIndent()
  indentkeys=0{,0},:,0#,!^F,o,O,e,[,(,{,),},],&,=\bibitem,=\item
  • I type \end{frame at the indented base. After I add a }, it gets automatically dedented (with a fraction of a second delay).

From :scriptnames, I can tell there are scripts loaded at /usr/share/vim/vim80/indent.vim, /usr/share/vim/vim80/syntax/tex.vim, /usr/share/vim/vim80/ftplugin/tex.vim, and /usr/share/vim/vim80/indent/tex.vim. So it looks like there is a script overriding the standard vim settings. How do I configure the behaviour of this script?

2 Answers 2

11

'indentexpr' has the highest priority of how vim can automatically indent lines.

:h 'indentexpr':

When this option is not empty, it overrules the 'cindent' and 'smartindent' indenting.

Your value is indentexpr=GetTeXIndent().

Customize GetTeXIndent()

The behavior of GetTeXIndent() can be customized by a few variables (e.g. g:tex_indent_brace or g:tex_indent_items). They are documented in-source in $VIMRUNTIME/indent/tex.vim.

If you only want to disable the indentation after \begin{frame} but keep indenting other code, you can put in your vimrc

let g:tex_noindent_env = 'document\|verbatim\|lstlisting\|frame'

This variable is also respected by Tex_CalcIdent() of vim-latex and BuckyTeXIndent() of vim-bucky.

Vimtex user: Customize VimtexIndentExpr()

vimtex provides its own indentation function VimtexIndentExpr(). This function can be customized with a similar option g:vimtex_indent_ignored_envs which, however, is a list of names

let g:vimtex_indent_ignored_envs = ['document', 'verbatim', 'lstlisting', 'frame']

Disable indentation completely

:set indentexpr=

Permanent vim setting

If you want to set indentexpr= permanently in your vim config, see the question

How can I change the default indentation based on filetype?

and its answers. Some recommend to not use autocmds but to place

setlocal indentexpr=

into a file such as ~/.vim/after/indent/tex.vim which is always sourced by vim and has the last word.

1
  • 1
    Of note, these can (and should) be set locally. Also, these options won’t persist—adding them to an ftplugin can help this.
    – D. Ben Knoble
    Jul 10, 2019 at 3:45
0

Within your .vimrc file, add the line

autocmd FileType plaintex,tex,context setlocal indentexpr=

where

  1. autocmd automatically applies the setup when criterion is met
  2. FileType is the criterion to be met (for files of certain type)
  3. plaintex,tex,context are the common filetypes recognized by vim (see Vim documentation)
  4. setlocal indentexpr= sets the required indent to be nothing
1
  • @D.BenKnoble Done! Please feel free to improve if necessary!
    – zyy
    Oct 20, 2021 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.