MAC + LINUX + VIM + GIT + Latex + MarkdownLaTeX科研写作

VIM+LaTeX 自动补全

2018-09-19  本文已影响1人  千羽之城88

先看看效果如何:

Vim latex 自动补全

一般情况下,小的文档都是用 texworks 编辑的。但是 texworks 不如 vim 的操作方便,所以今天配置了一下。需要先安装 vim-plug

然后安装 ultisnipsvim-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 的设定,

: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

参考:

  1. https://vimzijun.net/2016/10/30/ultisnip/
上一篇下一篇

猜你喜欢

热点阅读