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...

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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