先看看效果如何:
Vim latex 自動補全
一般情況下,小的文檔都是用 texworks 編輯的。但是 texworks 不如 vim 的操作方便,所以今天配置了一下。需要先安裝 vim-plug
:
然后安裝 ultisnips
和vim-snippets
:
" vim-plug
call plug#begin('~/.vim/plugged')
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'
call plug#end()"
接著配置 ultisnips
:
mkdir ~/.vim/UltiSnips
cd ~/.vim/UltiSnips
cd .>tex.snippets
現在打開用vim打開 tex.snippets
:e ~/.vim/tex.snippets
定義一個文檔snippet:
snippet doca "article" b
\documentclass[11pt]{article}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage[tmargin=1in,bmargin=1in,lmargin=1.25in,rmargin=1.25in]{geometry}
\setmainfont{STSongti-TC-Bold}[
BoldFont = ToppanBunkyuMidashiMinchoStdN-ExtraBold,
ItalicFont = HYShangWeiShouShuW-Regular,
%BoldItalicFont = texgyrepagella-bolditalic.otf
]
\usepackage{etoolbox}}
\begin{document}
${0}
\end{document}
endsnippet
$0 代表鼠標所在的位置
b 代表
UltiSnips
會自動抓取最新的修改,所以我們不用重啟 vim
,只要保存了 snippet
文件,就可以直接使用。簡單來說,定義的一段 snip
的格式是這樣的:
- snippet 關鍵詞 “說明” 設定
- 內容
- endsnippet
在剛才那段 snip
里,std
就是我們可以展開的 關鍵詞,"use namespace std"
是一段說明(如果你用一些自動補全的插件,這段說明就會顯示出來,我們之后自然會提到,放心),而 b
的是關于這個 snip
的設定,
- b 代表只有關鍵詞出現在行首的時候,才可以被展開
- A 代表自動展開
-
w 代表可以展開這個 “詞”,具體 “詞” 的定義可以查看
:help iskeyword
。直觀感覺就是,這個關鍵詞是單獨的,和其他文字分開的。比如前后都是空格。 -
i 代表可以忽略前后字節,直接展開關鍵詞。(這個設定比 w 要更松)
其他的還有r, s, t, m
等等,都可以通過這個命令在自帶文檔里找到:
:help ultisnip
來看看別人效果
image
代碼如下:
snippet class "class" b
class ${1:Class}{
public:
// constructors, asssignment, destructor
$1();
$1(const $1&);
$1& operator=(const $1&);
~$1();
private:
};
endsnippet
這是我的基本配置:
snippet doca "article" b
\documentclass[11pt]{article}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage[tmargin=1in,bmargin=1in,lmargin=1.25in,rmargin=1.25in]{geometry}
\setmainfont{STSongti-TC-Bold}[
BoldFont = ToppanBunkyuMidashiMinchoStdN-ExtraBold,
ItalicFont = HYShangWeiShouShuW-Regular,
%BoldItalicFont = texgyrepagella-bolditalic.otf
]
\usepackage{etoolbox}}
\begin{document}
${0}
\end{document}
endsnippet
snippet docb "book" b
\documentclass[11pt]{book}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage[tmargin=1in,bmargin=1in,lmargin=1.25in,rmargin=1.25in]{geometry}
\setmainfont{STSongti-TC-Bold}[
BoldFont = ToppanBunkyuMidashiMinchoStdN-ExtraBold,
ItalicFont = HYShangWeiShouShuW-Regular,
%BoldItalicFont = texgyrepagella-bolditalic.otf
]
\usepackage{fontenc}
\begin{document}
${0}
\end{document}
endsnippet
snippet bfd "bdseries"
{\bfseries ${0}}
endsnippet
snippet ttd "ttfamily"
{\ttfamily ${0}}
endsnippet
snippet sfd "sffamily"
{\sffamily ${0}}
endsnippet
snippet scd "scshape"
{\scshape ${0}}
endsnippet
snippet sld "slshape"
{\slshape ${0}}
endsnippet
snippet itd "itshape"
{\itshape ${0}}
endsnippet
snippet upd "upshape"
{\upshape ${0}}
endsnippet
snippet emd "em"
{\em ${0}}
endsnippet
snippet bf "textbf"
{\textbf ${0}}
endsnippet
snippet tt "texttt"
{\texttt ${0}}
endsnippet
snippet sf "textsf"
{\textsf ${0}}
endsnippet
snippet sc "textsc"
{\textsc ${0}}
endsnippet
snippet sl "textsl"
{\textsl ${0}}
endsnippet
snippet it "textit"
{\textit ${0}}
endsnippet
snippet tup "textup"
{\textup ${0}}
endsnippet
snippet abs "abstract"
\begin{abstract}
${0}
\end{abstract}
endsnippet
snippet ali "align"
\begin{align}
${0}
\end{align}
endsnippet
snippet tab "tabular"
\begin{tabular}
\item ${0}
\end{tabular}
endsnippet
snippet app "appendix"
\begin{appendix}
${0}
\end{appendix}
endsnippet
snippet bma "bmatrix"
\begin{bmatrix}
${0}
\end{bmatrix}
endsnippet
snippet pma "pmatrix"
\begin{pmatrix}
${0}
\end{pmatrix}
endsnippet
snippet cas "cases"
\begin{cases}
${0}
\end{cases}
endsnippet
snippet des "description"
\begin{description}
\item[${0}]
\end{description}
endsnippet
snippet cen "center"
\begin{center}
${0}
\end{center}
endsnippet
snippet equ "equation"
\begin{equation}
${0}
\end{equation}
endsnippet
snippet enu "enumerate"
\begin{enumerate}
${0}
\end{enumerate}
endsnippet
snippet eqn "eqnarray"
\begin{eqnarray}
${0}
\end{eqnarray}
endsnippet
snippet fig "figure"
\begin{figure}
${0}
\end{figure}
endsnippet
snippet fla "flalign"
\begin{flalign}
${0}
\end{flalign}
endsnippet
snippet gat "gather"
\begin{gather}
${0}
\end{gather}
endsnippet
snippet ite "item"
\begin{item}
\item ${0}
\end{item}
endsnippet
snippet let "letter"
\begin{letter}
${0}
\end{letter}
endsnippet
snippet lis "list"
\begin{list}
${0}
\end{list}
endsnippet
snippet min "minipage"
\begin{minipage}
${0}
\end{minipage}
endsnippet
snippet mul "multiline"
\begin{multiline}
${0}
\end{multiline}
endsnippet
snippet pic "picture"
\begin{picture}
${0}
\end{picture}
endsnippet
snippet spl "split"
\begin{split}
${0}
\end{split}
endsnippet
snippet sub "subequations"
\begin{subequations}
${0}
\end{subequations}
endsnippet
snippet the "theorem"
\begin{theorem}
${0}
\end{theorem}
endsnippet
snippet tit "titlepage"
\begin{titlepage}
${0}
\end{titlepage}
endsnippet
snippet tri "trivlist"
\begin{trivlist}
${0}
\end{trivlist}
endsnippet
snippet var "varwidth"
\begin{varwidth}
${0}
\end{varwidth}
endsnippet
snippet ver "verbatim"
\begin{verbatim}
${0}
\end{verbatim}
endsnippet
snippet tex "\TeX"
{\TeX}${0}
endsnippet
snippet lx "\LaTeX"
{\LaTeX}${0}
endsnippet
snippet ttl "\title{}"
\title{${0}}
endsnippet
snippet aut "\author"
\author{${0}}
endsnippet
snippet cha "\chapter"
\chapter{${0}}
endsnippet
snippet sec "\section"
\section{${0}}
endsnippet
snippet subs "\subsection"
\subsection{${0}}
endsnippet
snippet [ "\[...\]"
\[
${0}
\]
endsnippet
snippet $ "$...$ inline"
$${0}$
endsnippet
snippet $$ "$$...$$"
$$
${0}
$$
endsnippet
snippet fl "\flushleft"
{\flushleft ${0}}
endsnippet
snippet fr "\flushright"
{\flushright ${0}}
endsnippet
參考: