Ubuntu下Vi/Vim的配置

2017年04月10日

Vim是我在Linux下用的文本编辑器,它可以让人在编辑文本过程中摆脱鼠标的束缚,书写 的速度可以和思维速度匹配。原生态的Vim使用起来还是有很多不方便的地方。Vim几乎可以完成 我们日常所有的文本编辑任务,但是需要一些配置。我使用Vundle来管理Vim插件, 它可以让我们方便快捷地配置好Vim,省去学多配置过程中的许多繁琐细节。Vim的插件 可以让Vim变得无比强大。

一、Vim配置和插件安装

1) 备份当前Vim配置文件
$ mv ~/.vim ~/.vim.orig
$ mv ~/.vimrc ~/.vimrc.orig  #如果没有.vimrc,这步可省
2) 克隆和安装远程仓库
$ git clone git://github.com/humiaozuzu/dot-vimrc.git ~/.vim
$ ln -s ~/.vim/vimrc ~/.vimrc
3) 安装Vundle
$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

4) 安装bundles

启动Vim(忽略当前错误,在安装完所需插件后,它们会消失),并且运行

:BundleInstall

二、如何管理vimrc

所有的插件都列在文件bundles.vim中,里面还包含非常详细的说明。也可以 往里面添加自己喜欢的其他插件。

  1. :BundleClean: 清除不用的插件;
  2. :BundleInstall: 安装新增插件;
  3. :BundleInstall!: 更新所有插件.

其他配置也十分详细地写在文件.vimrc之中,更详细的内容可以参看里面的内容。

三、安装Vim-Latex Suite插件

因为需要编写Tex文件,所以我还安装了Vim-Latex套件。这个插件无法用Vundle来管理, 需要独立安装。

1) 下载和解压文件

SourceForge Files for vim-latex 下载插件的压缩包文件。将文件解压到目录~/.vim中。

2) 配置.vimrc文件

.vimrc文件中进行如下配置:

" 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

" IMPORTANT: grep will sometimes skip displaying the file name if you
" search in a singe file. This will confuse Latex-Suite. Set your grep
" program to always generate a file-name.
set grepprg=grep\ -nH\ $*

" 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'

另外,还要在~/.vim/ftplugin/tex_latexSuite.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 <C-n> you will automatically cycle through
" all the figure labels. Very useful!
set iskeyword+=:"}
3) 安装帮助文件

安装latex-suite.txtlatexhelp.txt文件作为帮助文件,启动vim,并且键入 如下命令:

helptags ~/.vim/doc
4) 修复Alt+i功能键

因为在编写文档的时候需要频繁地用到列表环境, 所以Alt+i自动补全item真的是太方便了. 但是在我的ThinkPad T440s上(Xubuntu 16.04平台)这个功能却无法实现, 琢磨了很久, 终于 在网上找到了一个解决办法. 有了这个功能之后, 感觉身心舒畅极了.

原因是不同的终端对于Alt组合键有不同的解释. 一些终端将Alt组合键解释成8-bit码, 而另一些终端将其解释为7位码. 实现Latex-Suite中Alt+i宏补全组合键, 只需要在.vimrc 中作如下配置即可:

let c='a'
while c <= 'z'
    exec "set <A-".c.">=\e".c
    exec "imap \e".c." <A-".c.">"
    let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
5) 完成

至此,我们就算完成了插件的安装。现在我们可以在vim中编辑Tex文件,并且Latex-Suite 应该会自动启动。我们还可以通过如下命令:

:help latex-suite.txt

来获得在线的Latex-Suite帮助文档。

相关链接: