2

The ftplugin vimtex provides the file indent/tex.vim with following line

setlocal indentkeys+=[,(,{,),},],\&,=item,=else,=fi

'indentkeys' 'indk' string (default "0{,0},:,0#,!^F,o,O,e")
local to buffer
A list of keys that, when typed in Insert mode, cause reindenting of the current line. Only happens if 'indentexpr' isn't empty. The format is identical to 'cinkeys', see |indentkeys-format|.

Consider this list environment:

\begin{itemize}
  \item <CURSOR> 
\end{itemize} 

When you enter a newline and then a second \item, at first the cursor will be indented to the second level (sw=2)

\begin{itemize}
  \item 
    <CURSOR>
\end{itemize} 

then when you enter manually \item, the line will be reindented and you get the desired indentation

\begin{itemize}
  \item 
  \item <CURSOR>
\end{itemize} 

However, if you do the same by using the UltiSnips snippet it (from honza/vim-snippets)

snippet it "Individual item" b
\item $0
endsnippet

you end up with an unintended second level indentation of the second \item:

\begin{itemize}
  \item 
    \item <CURSOR>
\end{itemize} 

Does anyone know how to resolve this?

This has already been raised on the issue tracker of UltiSnips, but I hope to find here a wider audience for this issue. BTW https://github.com/LucHermitte/mu-template claims to support reindentation.

1 Answer 1

5

You can manually fix the indent by typing CTRL-F (this is documented at :help indentkeys-format). Thus, an autonomous solution would be to issue this key-press after snippet expansion.

global !p
def fix_indent(snip):
    vim.eval('feedkeys("\<c-f>")')
endglobal

post_expand "fix_indent(snip)"
snippet it "Individual item" b
\item $0
endsnippet
2
  • Thanks for that ! Though, we need to add this post_expand "fix_indent(snip)" to every snippet we want to fix the indent. Wouldn't it be nice to fix indent for every snippet when autoindent is on ? UltiSnips doesn't seems to be able to do that.
    – perelo
    Commented Sep 14, 2020 at 16:17
  • This used to work wonderfully, but in newer versions (of UltiSnips?) it produces an error, at least for me. Commented Jun 16 at 15:17

Your Answer

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

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