Vim Latex 的使用和配置技巧 (一)

在这篇文章里, 我将简单地介绍一下我用 vim 编辑 Latex的技巧, 以及一些相关的配置.

众所周知, Vim 是一个插件极其强大的编辑软件, 在 Mac 上, 它的安装方法是:

brew cask install macvim

brew的安装方法, 可以到brew 主页查看.

在这里, 我们假设所有读者都已经熟悉VIM的基本用法. 如果有兴趣了解, 可以参考下面的基本教程.

酷壳网简明 VIM 练级攻略

插件的管理器: vundle

为了配置 Latex, 我们需要安装一下相关的插件: vundle. 其链接为:
Vundle Github 网址
这是一个插件管理的插件, 有了它, 就可以轻松安装 Vim 的插件了.

  1. 安装 Vundle:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  1. 配置
    新建一个用户的.vimrc文件, 并且将下面的代码复制进来:
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
  1. 安装插件
    比如你要安装fugitive插件, 那么就在call vundle#begin()call vundle#end() 中间, 加上Plugin 'tpope/vim-fugitive'的命令, 然后保存文件, 并用:so % source 一下, 然后再:PluginInstall, 就可以自动安装插件了.

Vim-latex 插件

1. 安装

Vim-latex 插件是一个强大的Latex插件, 它的安装方法是:

将下面代码放在~/.vimrc 中:

Plugin 'vim-latex/vim-latex'
" REQUIRED. This makes vim invoke Latex-Suite when you open a tex file.
filetype plugin on
" IMPORTANT: win32 users will need to have 'shellslash' set so that latex
" can be called correctly.
set shellslash

" OPTIONAL: This enables automatic indentation as you type.
filetype indent on

" OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to
" 'plaintex' instead of 'tex', which results in vim-latex not being loaded.
" The following changes the default filetype back to 'tex':
let g:tex_flavor='latex'

注意, Plugin 'vim-latex/vim-latex'一定要在call vundle#begin()call vundle#end() 中间.

在 Vim中运行:

:source %
:PluginInstall

2. 配置: ~/.vim/ftplugin/tex.vim

这个文件以后就是你的用户配置文件. 所有的 tex 文档都会自动加载这里的命令.
~/.vim/ftplugin/tex.vim文件中, 加入下面的代码:

" this is mostly a matter of taste. but LaTeX looks good with just a bit
" of indentation.
set sw=2
" TIP: if you write your \label's as \label{fig:something}, then if you
" type in \ref{fig: and press  you will automatically cycle through
" all the figure labels. Very useful!
set iskeyword+=:

3. 安装和插入模板

当新建一个空白的tex文本之后, 可以运行:TTemplate来插入一个模板. 之后, 就会有一个模板列表出现, 选中相应的数字就可以插入.

你也可以自行配置模板的库文件. 比如, 在~/.vim/ftplugin/latex-suite/templates中, 可以存储几个 tex 文件, 这些文件都会出现在你的模板列表里. 而且, 你还可以自行定义一下模板的存储目录, 比如, 在tex.vim中, 输入

let g:Tex_CustomTemplateDirectory=‘你的模板目录’

然后, 这个目录下的所有文件, 也都将出现在模板列表中.

4. Latex-Suit 的宏命令

在 Latex-Suit 中, 一直会有一个 Place Holders 的东西, 默认为<++>, 比如, 当你用宏命令插入一个图片的环境时, 就会出现

\begin{figure}[<+htpb+>]
    \centering
    \includegraphics{<+file+>}
    \caption{<+caption text+>}
    \label{fig:<+label+>}
\end{figure}<++>

这些 Place Hoders, 就是你的光标所在的位置, 当你输入完这一个命令, 然后按时, 光标就会跳到下一个 Place Holder 上, 这样你就可以大大减少摁向右键, 从而加快了速度, 提高了效率.

4.1 环境映射

4.1.1 插入环境映射

  1. 比如, 你想插入
\begin{environment}
<++>
\end{environment}<++>

就简单地在 insert 模式下, 输入environment, 然后按下, 就 OK 了.

再比如, 在输入 theorem之后, 按一下, 默认就会有

\begin{theorem}
      \label{<++>}<++>
\end{theorem}<++>
  1. 用’三字母’法.
    什么是三字母法? 主要有下面几个规则:
  • All environment mappings begin with E
  • If the environment can be broken up into 2 distinct words, such as flushright (flush + right), then the next 2 letters are the first letters of the 2 words. Example:
    flushleft (_f_lush + _l_eft) ---> EFL
    flushright (_f_lush + _r_ight) ---> EFR
    minipage (_m_ini + _p_age) ---> EMP
  • If on the other hand, the environment name cannot be broken up into 2 distinct words, then the next 2 letters are the first 2 letters of the name of the environment. Example:
    equation (_eq_uation) ---> EEQ
    总而言之, 就是 E + 两个单词的首字母或者一个单词的前两个字母.
4.1.2 环境映射修改

如果你想把 equation 变成align, 那么应该怎么做呢? 有两个办法:

  1. 按一下, 然后输入align, 回车, 大功告成.
  2. vimtex的插件, 在 normal 的模式下, 输入cse, 然后输入 align, 回车.

vimtex的插件, 还可以快速修改下面的情况: 我想在alignalign*之间互换, 那么就在 normal 的模式下, 输入tse.

4.1.3 环境映射的配置

然而, 你想做一下更改, 可以在tex.vim中, 加入下面一行命令 :

let g:Tex_Env_theorem = "\\begin{theorem}\<++>\\\end{theorem}"

那么, 就会变成:

\begin{theorem}
      <++>
\end{theorem}<++>

如果你想对诸如align*这样带星号或者其他特殊符号的做修改, 就需要用下面的命令:

let g:Tex_Env_{'align*'} =
         \ "\\begin{align*}\<++> &= <++>\\\end{align*}<++>"

这样, 就会出现

\begin{align*}
     <++> &= <++>
 \end{align*}<++>

有的环境需要加标签, 比如, 你希望对于所有的 equation 环境, 都有像\label{eq:<++>}的标签, 那么可以用以下的命令:

let g:Tex_EnvLabelprefix_equation = "eq:"

这样一来, 就会有

\begin{equation}
      <++>
      \label{eq:<++>}
\end{equation}<++>

同样地, 对于以下几个, 也同样适用, 倍有奇效.
figure, table, theorem, definition, lemma, proposition, corollary, assumption, remark, equation, align and multline.
如果你什么也没输入, 在空格前按一下, 那么也会有一个默认的列表出现, 只要按一下序号, 就会出现相应的环境. 默认的列表是: 'equation,equation*,align,align*,enumerate,itemize,figure,table’. 你可以用

let g:Tex_PromptedEnvironments=‘你的列表’

来修改, 注意用逗号隔开.

4.2 命令映射

4.2.1 插入命令映射

如果你想得到\frac{<++>}{<++>}<++>, 就直接输入 frac. 然后按, 就可以了, 它的默认的配置命令是

let g:Tex_Com_frac = "\\frac{<++>}{<++>}<++>"

一般地, 当你随便输入一个东西(如word)时, 将会出现 \word{<++>}.

4.2.2 更换命令映射

如果你想把\word{something} 变成 \abcd{something}, 那么也有下面两种办法:

  1. 在 insert 或者 normal模式, 按.
  2. vimtex插件, 在 normal 模式, 输入csc.

4.3 字体映射

如果你想插入\textsf{<++>}<++>(命令: FSF), 那么就可以用下面的三字母法:

  1. first letter is always F (F for font)
  2. next 2 letters are the 2 letters describing the font.

所以在 insert模式, 输入 FEM, 就会有\emph{<++>}<++>.

4.4 章节映射

在 insert 模式, 输入SSE, 就会有\section{<++>}<++>. 一般地,

SPA for part
SCH for chapter
SSE for section
SSS for subsection
SS2 for subsubsection
SPG for paragraph
SSP for subparagraph

4.5 希腊字母映射

在 insert模式, 如果你想输入一个希腊字母, 那么就简单地用a-z来输入\alpha-\zeta. 键默认是

let g:Tex_Leader=‘`’

就是~上的那个键. 但是对于我自己来说, 这个键比较难摁到, 我将它修改为;, 这个命令是:

let g:Tex_Leader=';'

但是接下来, 因为网页上的说明依然是默认键, 我们还是以默认的键来说明.

另外, 还有大写的希腊字母:

`D = \Delta
`F = \Phi
`G = \Gamma
`Q = \Theta
`L = \Lambda
`X = \Xi
`Y = \Psi
`S = \Sigma
`U = \Upsilon
`W = \Omega

4.6 常用的其他映射

Latex-suit 还提供了一些其他的映射, 现列表于下:
在insert 模式下,

`^   Expands To   \Hat{<++>}<++>
`_   expands to   \bar{<++>}<++>
`6   expands to   \partial
`8   expands to   \infty
`/   expands to   \frac{<++>}{<++>}<++>
`%   expands to   \frac{<++>}{<++>}<++>
`@   expands to   \circ
`0   expands to   ^\circ
`=   expands to   \equiv
`\   expands to   \setminus
`.   expands to   \cdot
`*   expands to   \times
`&   expands to   \wedge
`-   expands to   \bigcap
`+   expands to   \bigcup
`(   expands to   \subset
`)   expands to   \supset
`<   expands to   \le
`>   expands to   \ge
`,   expands to   \nonumber
`~   expands to   \tilde{<++>}<++>
`;   expands to   \dot{<++>}<++>
`:   expands to   \ddot{<++>}<++>
`2   expands to   \sqrt{<++>}<++>
`|   expands to   \Big|
`I   expands to   \int_{<++>}^{<++>}<++>

在 visual 模式下,

`(  encloses selection in \left( and \right)
`[  encloses selection in \left[ and \right]
`{  encloses selection in \left\{ and \right\}
`$  encloses selection in $$ or \[ \] depending on characterwise or
                                      linewise selection

4.7 自定义映射

我们可以用IMAP()来自行定义一些映射 . 比如: 在tex.vim中, 加入

call IMAP('NOM', '\nomenclature{<++>}<++>', 'tex')

那么在 insert 模式下, 当你输入NOM时, 就会有\nomenclature{<++>}<++>. 注意如果用双引号, 就需要是

call IMAP('NOM', “\\nomenclature{<++>}<++>”, 'tex')

或者

call IMAP('EFE', "\\begin{figure}\<++>\\end{figure}<++>", 'tex')

这里, IMAP中的I指的是 insert 模式. 最后tex指的是这个映射所应用的文件类型.

如果想移除某个命令, 可以用

call IUNMAP('FEM','tex')

来实现.


相关文章:

  • vim latex配置(一)
  • vim latex配置(二)
  • vim latex配置(三)

你可能感兴趣的:(Vim Latex 的使用和配置技巧 (一))