Vim で TeX 文書書いてますか。今時 Vim で TeX 文書を書いている人は、vimtex を使っている人は多いでしょう。コンパイルなどだけではなく、補完も使いこなしてますか。vimtex の強力な TeX 補完機能を便利に使う方法を紹介。
Complete citations
vimtex-complete-cites
Citation completion is triggered by '\cite{' commands. The completion parses
included bibliography files (*.bib) and thebibliography environments to
gather the completion candidates.
As an example, assume that a bibliography file is included with the following
entry:
@book { knuth1981,
author = "Donald E. Knuth",
title = "Seminumerical Algorithms",
publisher = "Addison-Wesley",
year = "1981" }
Then the bibliography key knuth1981 will be completed with e.g.:
\cite{Knuth 1981
\cite{algo
\cite{Don.*Knuth
In particular, note that regular expressions (or vim patterns) can be used
after '\cite{' to search for candidates.
vimtex の補完は、i_CTRL-X_CTRL-O です。つまり、挿入モードで C-x, C-o ってことですね。これは、Vim7 からの omni-completion って機能を使って実装されていて、cite や、ref を入力するときに便利なやつ。
んで、補完を自動化したい(つまり勝手に補完候補が表示されるようにする)場合は、neocomplete, deoplete, nvim-completion-manager, youcompleteme や VimCompletesMe との連携方法がヘルプに書いてある。
Autocomplete
vimtex-complete-auto
Vim does not provide automatic completion by itself, but there exist at least
four good plugins that provide this: neocomplete, deoplete,
nvim-completion-manager and youcompleteme. Moreover, there is
VimCompletesMe that overrides to trigger different built-in
completions, such as the omni-completion by vimtex, depending on the context.
See below for descriptions on how to setup these with vimtex.
自分の場合は、neocomplete を使っているので、
neocomplete
vimtex-complete-neocomplete
neocomplete is my preferred choice for an automatic completion engine in vim.
The plugin is available here: https://github.com/Shougo/neocomplete.vim.
The following options may be used to enable automatic completion for LaTeX
documents with neocomplete and vimtexs omni completion function:
if !exists('g:neocomplete#sources#omni#input_patterns')
let g:neocomplete#sources#omni#input_patterns = {}
endif
let g:neocomplete#sources#omni#input_patterns.tex =
\ g:vimtex#re#neocomplete
後半の if からの部分を、.vimrc に書いておけば、補完が自動化されます。補完の仕方を知ってるのと知らないのとでは生産性結構変わると思います。
ちなみに、vimtex の場合は、補完が発動する文字列はプラグイン内にハードコードされているので、\Cite だったり、\reffig みたいな、カスタマイズしたコマンドを補完のキーワードにするためには、オプションで指定とかはできなくてソースコードを書き換える必要がある。なので、現状は「\ref で補完して入力→コマンドを \reffig に書き直す」みたいにしてます。
