先放一个按本文配置好的效果图~
在大部分的linux系统中,vim的配置文件包括系统配置文件/etc/vimrc,/user/share/vim/,以及用户配置文件~/.vimrc, ~/.vim/。由于用户配置文件加载顺序在后,因此仅需添加相关配置在用户配置文件中。
下面是.vimrc的一个配置。
" .vimrc
" See: http://vimdoc.sourceforge.net/htmldoc/options.html for details
" For multi-byte character support (CJK support, for example):
" set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,gb18030,latin1
set tabstop=4 " Number of spaces that a <Tab> in the file counts for.
set shiftwidth=4 " Number of spaces to use for each step of (auto)indent.
set expandtab " Use the appropriate number of spaces to insert a <Tab>.
" Spaces are used in indents with the '>' and '<' commands
" and when 'autoindent' is on. To insert a real tab when
" 'expandtab' is on, use CTRL-V <Tab>.
set smarttab " When on, a <Tab> in front of a line inserts blanks
" according to 'shiftwidth'. 'tabstop' is used in other
" places. A <BS> will delete a 'shiftwidth' worth of space
" at the start of the line.
set showcmd " Show (partial) command in status line.
set number " Show line numbers.
set showmatch " When a bracket is inserted, briefly jump to the matching
" one. The jump is only done if the match can be seen on the
" screen. The time to show the match can be set with
" 'matchtime'.
set hlsearch " When there is a previous search pattern, highlight all
" its matches.
set incsearch " While typing a search command, show immediately where the
" so far typed pattern matches.
set ignorecase " Ignore case in search patterns.
set smartcase " Override the 'ignorecase' option if the search pattern
" contains upper case characters.
set backspace=2 " Influences the working of <BS>, <Del>, CTRL-W
" and CTRL-U in Insert mode. This is a list of items,
" separated by commas. Each item allows a way to backspace
" over something.
set autoindent " Copy indent from current line when starting a new line
" (typing <CR> in Insert mode or when using the "o" or "O"
" command).
set textwidth=79 " Maximum width of text that is being inserted. A longer
" line will be broken after white space to get this width.
set formatoptions=c,q,r,t " This is a sequence of letters which describes how
" automatic formatting is to be done.
"
" letter meaning when present in 'formatoptions'
" ------ ---------------------------------------
" c Auto-wrap comments using textwidth, inserting
" the current comment leader automatically.
" q Allow formatting of comments with "gq".
" r Automatically insert the current comment leader
" after hitting <Enter> in Insert mode.
" t Auto-wrap text using textwidth (does not apply
" to comments)
set ruler " Show the line and column number of the cursor position,
" separated by a comma.
set background=dark " When set to "dark", Vim will try to use colors that look
" good on a dark background. When set to "light", Vim will
" try to use colors that look good on a light background.
" Any other value is illegal.
set mouse=a " Enable the use of the mouse.
filetype plugin indent on
syntax on
以上是对vim中文本的一些基础配置,包括语法高亮,缩进格式等。如果需要对代码格式进行调整,实现自动排版,那么需要用到vim的其他插件,这里分享一个代码格式化框架vim-autoformat。在安装插件之前,需要用到一个工具Vundle,Vundle是一个vim插件管理.器,安装方法自行阅读链接啦,很简单这里不再赘述了。
安装好Vundle后再来安vim-autoformat就很简单了,只需在.vimrc中加入一行:Plugin 'Chiel92/vim-autoformat',再重启vim,执行:PluginInstall即可完成安装。可以看到,利用Vundle来装插件非常方便。
vim-autoformat是一个格式化框架,各种语言风格检查工具都能支持。以C++为例,clang-format是一种用于优美代码的工具,这里可能有同学会问到,既然有了clang-format,为什么还需要vim-autoformat呢?其实这里vim-autoformat是相当于将原本独立的clang-format集成到了vim中,没有它,也就只能单独运行clang-format来格式化代码,这显然是不直观的。在ubuntu下安装clang-format也非常的方便,只需运行
sudo apt-get install clang-format
安装完毕后只需在.vimrc末尾,添加
"F3自动格式化代码
noremap <F3> :Autoformat<CR>
let g:autoformat_verbosemode=1
这样就可以实现在vim中按F3自动格式化代码了。如果想自定义格式化方式或者是在保存文件时自动格式化,也有对应配置,这里就不再赘述了。到这里,已经实现了一些入门级的配置vim来写C++。然而C++各种模板类库繁多,或是在一个大的项目中函数名变量名众多,不太可能记得所有的API,因此自动进行代码补全,提示语法错误是十分有必要的。这里分享一个插件叫YouCompleteMe,利用Vundle执行
Plugin 'Valloric/YouCompleteMe'
之后重启vim执行:PluginInstall,在 ~ /.vim/bundle/YouCompleteMe文件中运行./install.sh --clang-completer,这里如果你报错了,问题不大,点开上面链接,安装依赖吧~
最后需要把路徑加入.vimrc中~
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
完毕