打造自己的vim c/c++开发工具
0. Ubuntu
$ sudo lsb_release -a
[sudo] password for xxx:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
1. 需要的必备神器
cscope, ctags: 代码搜索必备;
vbundle, taglist , nerdtree, AutoComplPop:vim插件管理工具,源代码层级,目录位置查看,自动补全;
zsh, oh-my-zsh, zsh-autosuggestions: 好用的shell环境,带有自动补全功能
2. 安装
// zsh, oh-my-zsh, zsh-autosuggestions
sudo apt install zsh -y
git clone https://gitee.com/momolwx/ohmyzsh.git ~/.oh-my-zsh
git clone https://gitee.com/momolwx/zsh-autosuggestions.git ~/.oh-my-zsh/plugins/zsh-autosuggestions
// vbundle, taglist , nerdtree
mkdir -p ~/.vim/bundle
#git clone https://github.com.cnpmjs.org/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone https://gitee.com/zdevt/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone https://gitee.com/mirrors/taglist.vim.git ~/.vim/bundle/taglist.vim
git clone https://gitee.com/awish/nerdtree.git ~/.vim/bundle/nerdtree
// 安装 cscope, ctags
sudo apt install cscope -y
sudo apt install universal-ctags -y
3. .vimrc 内容如下
set nu
set cul
set shell=/bin/bash
set laststatus=2
"Generate tags and cscope.out from FileList.txt (c, cpp, h, hpp)
nmap <C-@> :!find -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" > FileList.txt<CR>
\ :!ctags -L -< FileList.txt<CR>
\ :!cscope -bkq -i FileList.txt<CR>
if has("cscope")
set csto=0
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add cscope.out
endif
set csverb
"set cst 这两句会将cscope当作tag,当找不到时会卡住,因此注释掉
"set cscopetag
endif
nmap zs :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap zi :cs find i <C-R>=expand("<cfile>")<CR>$<CR>
nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR>
"每当拿到新的工程代码,进入相关代码目录,利用 vim ./ 打开当前目录, 然后 crtl + @ 生成插件所需文件,最后退出 vim。此步只需进行一次。
"在此目录中打开任意代码文件或任意子目录代码文件,
"利用 crtl + ] 跳转到定义。
"利用 z + c 跳转到调用。
"利用 z + t 查找光标所在的字符串出现的所有位置。
"利用 crtl + t 跳转回到上次的位置。
" Vbundle
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'
Plugin 'scrooloose/nerdtree'
Bundle 'taglist.vim'
Plugin 'vim-scripts/AutoComplPop' " 自动补全功能
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
"
"" taglist
map <F3> :Tlist<CR>
let Tlist_Show_One_File=1 "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow=1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Ctags_Cmd="/usr/bin/ctags" "将taglist与ctags关联
"set mouse=a " always use mouse
let Tlist_Use_SingleClick=1
"[taglist 基本功能使用方法』
"
"在Vim命令行下运行":Tlist"就可以打开Taglist窗口,再次运行":Tlist"则关闭。
"
"左右窗口切换Ctrl+ww
"在taglist窗口中,可以使用下面的快捷键:
"<CR> 跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样
"o 在一个新打开的窗口中显示光标下tag
"<Space> 显示光标下tag的原型定义
"u 更新taglist窗口中的tag
"s 更改排序方式,在按名字排序和按出现顺序排序间切换
"x taglist窗口放大和缩小,方便查看较长的tag
"+ 打开一个折叠,同zo
"- 将tag折叠起来,同zc
"* 打开所有的折叠,同zR
"= 将所有tag折叠起来,同zM
"[[ 跳到前一个文件
"]] 跳到后一个文件
"q 关闭taglist窗口
"<F1> 显示帮助
"但是!这些大部分可以被鼠标取代!!快捷键是浮云~~
" nerdtree
" NERDTree config
map <F2> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif
""修改树的显示图标
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
""窗口位置
let g:NERDTreeWinPos='left'
""窗口尺寸
let g:NERDTreeSize=30
""窗口是否显示行号
let g:NERDTreeShowLineNumbers=1
""不显示隐藏文件
let g:NERDTreeHidden=0
安装vim插件方法:
vim //打开空的文档
:PluginInstall //这一步需要重复两三次,因为使从github下代码,比较慢,容易出错,多试两次都能成功
4. .zshrc 配置
.zshrc 文件需要修改如下内容:
ZSH_THEME="risto"
plugins=(git zsh-autosuggestions)
bash下输入zsh,即可进入zsh。或者设置直接在/etc/profile后追加一行如下:
zsh
设置指定cscope.out的位置:
alias source_cs="export CSCOPE_DB='`pwd`/cscope.out'"
5. vim 技巧
模式 匹配
/\Cword word
/\CWord Word
/\cword word,Word,WORD,WoRd,等。
/\cWord word,Word,WORD,WoRd,等。