Ubuntu下VIM C++代码编辑环境搭建
插件安装列表
- Pathogen:vim插件管理工具
- Auto-pairs: 自动匹配()、{}、[]等,可选用
- NERDTree: 有点类似文件管理的东西,可以浏览文件目录里的内容
- MinibufExplorer: 将每个文件的buf在单独的窗口里显示,支持不同buf间的快速切换和开关
- ctags + taglist: 源码的tag生成工具,以及源码内的符号内容浏览工具
- omnicppcomplete: 对C++的源码编辑自动补全工具
自动安装脚本
针对以上工具做了一个简单的脚本来自动化运行:
#!/bin/bash
# This is a simple script for preparing your vim working environment
# Target at C/C++ developer using VIM as editor under Ubuntu
# You can add more plugins into this if needed
# Add support for centOS in the future
##################################################
# General environment
##################################################
VIM_PATH=$HOME/.vim
AUTOLOAD_PATH=$VIM_PATH/autoload
BUNDLE_PATH=$VIM_PATH/bundle
# usage: die <line> <msg>
function die {
local exitcode=$?
local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
echo $msg 1>&2
exit $exitcode
}
if [[ $EUID -eq 0 ]]; then
die $LINENO "you shouldnt run this script under root!"
fi
mkdir -p $AUTOLOAD_PATH
if [[ ! -d $AUTOLOAD_PATH ]]; then
die $LINENO "cant create dir: $AUTOLOAD_PATH"
fi
mkdir -p $BUNDLE_PATH
if [ ! -d $BUNDLE_PATH ]; then
die $LINENO "cant create dir: $BUNDLE_PATH"
fi
###################################################
# Pathogen.vim
###################################################
PATHOGEN_PATH=$AUTOLOAD_PATH/pathogen.vim
PATHOGEN_URL="https://tpo.pe/pathogen.vim"
if [[ ! -f $PATHOGEN_PATH ]]; then
echo "downloading pathogen.vim ..."
curl -LSso $PATHOGEN_PATH $PATHOGEN_URL
if [[ $? -ne 0 ]]; then
die $LINENO "download $PATHOGEN_URL failed"
fi
fi
###################################################
# auto-pairs.vim
###################################################
AUTO_PAIRS_PATH=$BUNDLE_PATH/auto-pairs
AUTO_PAIRS_URL="git://github.com/jiangmiao/auto-pairs.git"
if [[ ! -d $AUTO_PAIRS_PATH ]]; then
echo "downloading auto-pairs.vim..."
git clone $AUTO_PAIRS_URL $AUTO_PAIRS_PATH
if [ $? -ne 0 ]; then
die $LINENO "downloading $AUTO_PAIRS_URL failed"
fi
fi
###################################################
# NERDTree.vim
###################################################
NERDTREE_PATH=$BUNDLE_PATH/nerdtree
NERDTREE_URL="https://github.com/scrooloose/nerdtree.git"
if [[ ! -d $NERDTREE_PATH ]]; then
echo "downloading NERDTree.vim..."
git clone $NERDTREE_URL $NERDTREE_PATH
if [[ $? -ne 0 ]]; then
die $LINENO "downloading $NERDTREE_URL failed"
fi
fi
###################################################
# MiniBufExplorer.vim
###################################################
MINIBUFEXP_PATH=$BUNDLE_PATH/minibufexplorer/plugin
MINIBUFEXP_URL="http://www.vim.org/scripts/download_script.php?src_id=3640"
if [[ ! -d $MINIBUFEXP_PATH ]]; then
mkdir -p $MINIBUFEXP_PATH
if [ ! -d $MINIBUFEXP_PATH ]; then
die $LIENNO "cant mkdir $MINIBUFEXP_PATH"
fi
echo "downloading minibufexplorer.vim"
wget $MINIBUFEXP_URL -O $MINIBUFEXP_PATH/minibufexpl.vim
if [[ $? -ne 0 ]]; then
die $LINENO "downloading $MINIBUFEXP_URL failed"
fi
fi
###################################################
# ctags
###################################################
command -v ctags >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "install ctags..."
sudo apt-get install ctags
if [[ $? -ne 0 ]]; then
die $LINENO "install ctags failed!"
fi
fi
###################################################
# taglist
###################################################
TAGLIST_PATH=$BUNDLE_PATH/taglist
TAGLIST_URL="http://www.vim.org/scripts/download_script.php?src_id=19574"
TAGLIST_ZIP=$BUNDLE_PATH/taglist.zip
if [[ ! -d $TAGLIST_PATH ]]; then
echo "downloading taglist..."
wget $TAGLIST_URL -O $TAGLIST_ZIP
if [ ! $? ]; then
die $LINENO "download $TAGLIST_URL failed"
fi
unzip $TAGLIST_ZIP -d $TAGLIST_PATH
rm -f $TAGLIST_ZIP
fi
#####################################################
# omnicppcomplete.vim
#####################################################
OMNICPPCOMPL_PATH=$BUNDLE_PATH/omnicppcomplete
OMNICPPCOMPL_URL="http://www.vim.org/scripts/download_script.php?src_id=7722"
OMNICPPCOMPL_ZIP=$BUNDLE_PATH/omnicppcomplete.zip
if [[ ! -d $OMNICPPCOMPL_PATH ]]; then
echo "downloading omnicppcomplete.vim..."
wget $OMNICPPCOMPL_URL -O $OMNICPPCOMPL_ZIP
if [[ $? -ne 0 ]]; then
die $LINENO "download $OMNICPPCOMPL_URL failed"
fi
unzip $OMNICPPCOMPL_ZIP -d $OMNICPPCOMPL_PATH
rm -f $OMNICPPCOMPL_ZIP
fi
#####################################################
# for auto complete STL support
#####################################################
STL_SRC_PATH=$VIM_PATH/tags
STL_SRC_URL="http://www.vim.org/scripts/download_script.php?src_id=9178"
STL_SRC_TAR=$STL_SRC_PATH/cpp_src.tar
if [ ! -d $STL_SRC_PATH ]; then
mkdir -p $STL_SRC_PATH
if [[ ! -d $STL_SRC_PATH ]]; then
die $LINENO "cant create dir for $STL_SRC_PATH"
fi
echo "downloading STL src..."
wget $STL_SRC_URL -O $STL_SRC_TAR
if [[ $? -ne 0 ]]; then
die $LINENO "download $STL_SRC_URL failed"
fi
tar jxvf $STL_SRC_TAR -C $STL_SRC_PATH
rm -f $STL_SRC_TAR
cd $STL_SRC_PATH/cpp_src
ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++
if [ ! -f $STL_SRC_PATH/cpp_src/tags ]; then
die $LINENO "Generate tags for STL failed"
fi
fi
#######################################################
# .vimrc
#######################################################
cat << EOF > $HOME/.vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
nmap <F3> :NERDTreeToggle<cr>
nmap <F4> :TlistToggle<cr>
set tags+=~/.vim/tags/cpp_src/tags
set number
set tabstop=4
set shiftwidth=4
set softtabstop=4
" taglist
let Tlist_Show_One_File=1 " Only show on file's taglist
let Tlist_Exit_OnlyWindow=1 " When taglist is the last window, quit automatically
let Tlist_Use_Right_Window=1 " taglist int the right window
let Tlist_Sort_Type="name" " sort in name
" omnicppcomplete
set completeopt=longest,menu
let OmniCpp_NamespaceSearch = 2 " search namespaces in the current buffer and in included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function list
let OmniCpp_MayCompleteScope = 1 " input :: and auto complete
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
EOF