Skip to content

Instantly share code, notes, and snippets.

@john24318
Last active September 24, 2020 01:59
Show Gist options
  • Save john24318/eed43d80e181d2c0bd5ad3b2c21b435b to your computer and use it in GitHub Desktop.
Save john24318/eed43d80e181d2c0bd5ad3b2c21b435b to your computer and use it in GitHub Desktop.
vimrc for c/c++
" Install required tools on Linux
" sudo apt-get install ctags
" sudo apt-get install cscope
" sudo apt-get install vim-gnome
" sudo apt-get install silversearcher-ag
" set the runtime path to include Vundle and initialize
filetype off
set nocompatible
set backspace=indent,eol,start
set mouse=a
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set smartindent
set showmatch
set ruler
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'
Plugin 'vim-scripts/taglist.vim'
Plugin 'wesleyche/SrcExpl'
Plugin 'wesleyche/Trinity'
Plugin 'scrooloose/syntastic'
Plugin 'jiangmiao/auto-pairs'
Plugin 'Valloric/YouCompleteMe'
Plugin 'nathanaelkane/vim-indent-guides'
Plugin 'easymotion/vim-easymotion'
Plugin 'flazz/vim-colorschemes'
Plugin 'mileszs/ack.vim'
call vundle#end()
filetype plugin indent on
" Vundle 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
"<F7> show taglist, nerdtree, srcexpl
nmap <F7> :TrinityToggleAll
"<F8> show taglist
nmap <F8> :TlistToggle<CR><CR>
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Auto_Update=1
let Tlist_Use_SingClick=1
set ut=100
"<F9> show nerdtree
nmap <F9> :NERDTreeFind<CR><CR>
let NERDTreeWinPos=1
let NERDTreeMouseMode=2
let NERDTreeShowHidden=1
"<F10> show srcexpl
nmap <F10> :SrcExplToggle<CR>
let g:SrcExpl_pluginList = [
\"__Tag_List__",
\"_NERD_tree_",
\]
" vim color scheme
colorscheme desert
"colorscheme molokai
set t_Co=256
syntax on
" customize backward-tab (conflict with YCM)
nmap <S-Tab> <<
imap <S-Tab> <C-d>
let g:ycm_key_list_previous_completion = ['<C-TAB>', '<Up>']
" copy to clipboard in visual mode
vmap <C-c> "+y
" ack with ag
let g:ackprg = 'ag --nogroup --nocolor --column'
cnoreabbrev Ack Ack!
nmap <Leader>a :Ack!<Space>
" cscope & ctags
set tags=./tags,./TAGS,tags;~,TAGS;~
set cscopetag
set csto=0
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set cscopeverbose
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
" update ctags
function! DelTagOfFile(file)
let fullpath = a:file
let cwd = getcwd()
let tagfilename = cwd . "/tags"
let f = substitute(fullpath, cwd . "/", "", "")
let f = escape(f, './')
let cmd = 'sed -i "/' . f . '/d" "' . tagfilename . '"'
let resp = system(cmd)
endfunction
function! UpdateTags()
let f = expand("%:p")
let cwd = getcwd()
let tagfilename = cwd . "/tags"
let cmd = 'ctags -a -f ' . tagfilename . ' --c++-kinds=+p --fields=+iaS --extra=+q ' . '"' . f . '"'
call DelTagOfFile(f)
let resp = system(cmd)
endfunction
autocmd BufWritePost *.cpp,*.h,*.c call UpdateTags()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment