让vim炫酷起来

2017-03-05

vim很强大,vim是门学问。
但是可能打开vim一看,满屏幕灰蒙蒙的一片,不像强大的样子,又挺失落,于是便有了此文。
首先,打开目录或者是/etc目录下的.vimrc文件,这个文件的作用就是vim的配置文件,其实在vim命令行下也可以敲指令设置vim,但是关闭vim后就“失效了”,要想长久设置,就再这俩配置文件中设置。目录下的配置文件是配置当前系统用户下的。而/etc目录下的是配置全局的。

改变vim的外观

首先改变灰蒙蒙的外观

高亮

先让颜色亮丽起来

syntax enable
syntax on

增加默认色素

设置颜色,默认8

set t_Co=256

显示行号

set nu

~号和line number 同一列

set go=

方便使用

目前而言,应该是不难看了,但是使用起来可能还有点蹩脚,不过没关系。

支持中文

set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8

自动缩进

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

搜索到的所有匹配关键字高亮

在使用”:/search_name” 的时候,某些数据不高亮,看起来很不清楚,输入一下指令,让搜索到的所有匹配的关键字高亮:

set hlsearch

可以用backspace删除

可能你会发现默认的退格键不能使用,很不爽,没事,键入以下指令

set backspace=indent,eol,start

用vim来编程

大神们好像都是用vim来写程序的,显得逼格高的一笔。其实也就是配置一下或者下个插件就好了,很多很好的插件github上一搜也是一堆,没事儿可以研究一下。

vim默认设置

让vim认识html、css、c、php等语言,编写的的时候给予提示。

autocmd FileType Python set omnifunc=pythoncomplete#Complete
autocmd FileType JavaScript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType PHP set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

添加插件管理工具Vundle

“Vundle”是个牛逼的东西呀,可以下载和管理你想要的vim插件。装上“Vundle”后,以后在github等开源网站上,看见牛逼的vim插件,就可以直接下下来安装到自己的vim上使用了。
下面简单介绍一下Vundle的安装:(详情请看:Vundle

Set up Vundle:

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

配置

把以下内容贴到.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      

安装插件

重启vim,输入指令:PluginInstall

安装插件“Valloric/YouCompleteMe”

这是一款非常牛逼的插件,保证让你爽爽的写代码。
下面简单介绍一下安装过程:(详情请看YouCompleteMe

Vundle 上加上插件包

在.vimrc Vundle“代码块”内,你找一个合适的地儿,加上

Plugin 'Valloric/YouCompleteMe'

然后启动vim安装。

编译

跑到下号的插件包的目录下。

cd ~/.vim/bundle/YouCompleteMe

目前YouCompleteMe 支持的语言有很多,下面简单介绍这几种 (假如你想支持全部语言:运行./install.py --all

  • C# :运行 ./install.py --omnisharp-completer
  • C-family语言系:运行./install.py --clang-completer
  • Go:运行./install.py --gocode-completer
  • JS:运行./install.py --tern-completer

最后,试试你的vim吧,是不是比以前炫酷多了