0

I recently made myself custom insert mode keybindings to type faster in LaTeX. However, When I use them, Vim adds tabs that weren't part of the original keybinding.

Example:

Here is my inoremap:

autocmd FileType tex inoremap ;t \begin{table}<Enter>\caption{}<Enter>\label{<++>}<Enter>\begin{center}<Enter>\begin{tabular}{<++>}<Enter>\toprule<Enter><++><Space>\\<Enter>\toprule<Enter><++><Space>\\<Enter><++><Space>\\<Enter>\bottomrule<Enter>\end{tabular}<Enter>\end{center}<Enter>\end{table}<Enter><Enter><++><Esc>?caption{<Enter>8li

This is the output (<X> is the final location of my cursor):

             \begin{table}
             \caption{<X>}
             \label{<++>}
             \begin{center}
             \begin{tabular}{<++>}
             \toprule
 <++> \\
             \toprule
 <++> \\
 <++> \\
             \bottomrule
             \end{tabular}
             \end{center}
             \end{table}

 <++>

Why isn't everything lining up at the beginning of the line? (the previous lines are not indented)

Thanks!

P.S.

I know that it would be more appropriate to use functions instead of mapping but I haven't learned them yet...

Hotschke
  • 4,740
  • 26
  • 37
  • Id bet $$$ it’s a weird interaction with auto/smart/cindent if any of those are set. – D. Ben Knoble Jan 21 '19 at 03:32
  • I added your snippet to my vimrc and I cannot confirm your observation. Debug with an empty vimrc except your snippet. It looks like you are using vim-latex: <++> are the jump markers used by vim-latex. Vim-latex provides a different mechanism for template expansion: https://github.com/vim-latex/vim-latex/blob/master/plugin/imaps.vim#L5-L83 and http://vim-latex.sourceforge.net/documentation/latex-suite/latex-macros.html – Hotschke Jan 22 '19 at 18:59

1 Answers1

0

Snippet plugin instead of a simple inoremap

If you are interested in features like jumping from one placeholder to another one, you might be interested in using a plugin. There are many options you can choose from. There are a few dedicated to LaTeX:

  1. vim-latex (details in section 3.12 and plugin/imaps.vim)

    You can also install only plugin/imaps.vim with
    $ wget https://raw.githubusercontent.com/vim-latex/vim-latex/master/plugin/imaps.vim -O ~/.vim/plugin/imaps.vim
    and then define in ~/.vim/after/ftplugin/tex.vim:

    call IMAP('ETE', "\\begin{table}\<CR>\\caption{<++>}\<CR>\\label{<++>}\<CR>\\begin{center}\<CR>\\begin{tabular}{<++>}\<CR>\\toprule\<CR><++><Space>\\\\\<CR>\\toprule\<CR><++><Space>\\\\\<CR><++><Space>\\\\\<CR>\\bottomrule\<CR>\\end{tabular}\<CR>\\end{center}\<CR>\\end{table}\<CR>\<CR><++>", 'tex')
    
  2. latex-support
  3. quicktex

There are also a few plugins which provide a fixed set of snippets for LaTeX (however, you can always modify the source code):

  1. auctex.vim
  2. drcstubs
  3. texmaps

I personally prefer a general plugin such as

  1. Ultisnips
  2. snipmate
  3. neosnippet

Your insert mode mapping as a ultisnips snippet (e.g. placed in ~/.vim/UltiSnips/tex.snippets):

snippet table "Table & Tabular"
\begin{table}
  \caption{${1:<+Caption text+>}}
  \label{tab:${2:<+label+>}}
  \centering
  \begin{tabular}{${3:<+dimensions+>}}
    \toprule
    $4 \\
    \toprule
    ${5:${VISUAL}} \\
    \bottomrule
  \end{tabular}
\end{table}$0
endsnippet

A more advanced snippet request is the question Easiest way to insert LaTeX matrix?.

You find also similar ones predefined in snippet collections such as

The question What is the difference between the vim snippets plugins? could also be interesting to compare the different options.

Hotschke
  • 4,740
  • 26
  • 37