This guide will help you set up Vim as a C/C++ development environment similar to VSCode's C/C++ extension. It includes setting up key plugins like YouCompleteMe, Ctags, Cscope, and more. Additionally, we will cover common errors and their solutions.
Before configuring Vim, you need to install essential tools like Vim, Ctags, Cscope, and a plugin manager.
1.1) Install Vim
If Vim is not already installed, you can install it using your package manager. Make sure to use a version that supports plugins like YouCompleteMe (Vim 9.1+).
sudo apt install vim # For Ubuntu
1.2) Install Ctags
Ctags helps index your code symbols so that you can jump to their definitions.
sudo apt install exuberant-ctags
1.3) Install Cscope
Cscope allows for advanced code navigation, such as finding all references of a function.
sudo apt install cscope
1.4) Install a Plugin Manager (Vim-Plug)
Vim-Plug is a lightweight plugin manager for Vim.
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
.vimrc FileYour .vimrc file is where you will configure Vim and set up all the plugins. You can open this file for editing by running:
vim ~/.vimrc
Below is an example configuration that you can add to your .vimrc.
call plug#begin('~/.vim/plugged')
" YouCompleteMe: Code completion
Plug 'ycm-core/YouCompleteMe'
" Gutentags: Automatically manage Ctags
Plug 'ludovicchabant/vim-gutentags'
" Cscope integration
Plug 'mtth/scratch.vim'
" NERDTree: File explorer
Plug 'preservim/nerdtree'
" FZF: Fuzzy file finder
Plug 'junegunn/fzf.vim'
call plug#end()
After adding this, save and close the file, then run the following command in Vim to install the plugins:
vim
:PlugInstall
3.1) YouCompleteMe Setup (Code Completion)
YouCompleteMe is a powerful code completion plugin. After installation, it requires compilation.
cd ~/.vim/plugged/YouCompleteMe
python3 install.py --clangd-completer
.vimrc for enhanced functionality:vim
let g:ycm_auto_hover = 1 " Automatically display code completion popup
3.2) Gutentags Setup (Ctags Management)
Gutentags automatically generates and updates Ctags for your project.
Add the following configuration to your .vimrc:
let g:gutentags_project_root = ['.git', '.hg', '.svn', 'Makefile']
let g:gutentags_ctags_auto_settags = 1
Gutentags will automatically manage and update the tags file in your project directories.
3.3) Cscope Setup
Cscope allows you to navigate code and find function definitions, callers, text search, etc.
Add the following to your .vimrc to enable Cscope:
if has("cscope")
set csprg=/usr/bin/cscope
set csto=1
set cst
set nocsverb
if filereadable("cscope.out")
cs add cscope.out
endif
set csverb
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>
endif
To generate the Cscope database (cscope.out), run the following in your project root:
cscope -Rbq
3.4) NERDTree Setup (File Explorer)
NERDTree provides a side panel for file navigation.
Add this to your .vimrc:
vim
nmap <C-n> :NERDTreeToggle<CR>
Press (Control + n) to toggle the file explorer on and off.
3.5) FZF Setup (Fuzzy Finder)
FZF is a fuzzy finder that allows quick file navigation.
sangs_o.log
Add this to your .vimrc:
vim
nnoremap <C-p> :Files<CR>
Press (Control + p) to open the file finder.
4.1) Ctags Usage
Once Ctags is generated, you can navigate to function definitions in Vim:
Ctrl-]: Jump to the definition of the symbol under the cursor.
Ctrl-t: Jump back to the previous position.
4.2) Cscope Usage
You can use Cscope to perform several useful searches:
Ctrl-\ s: Find all references of the symbol under the cursor.
Ctrl-\ g: Find the definition of the symbol under the cursor.
Ctrl-\ c: Find all calls to the function under the cursor.
Ctrl-\ t: Find all occurrences of the text under the cursor.
Error: YouCompleteMe unavailable: requires Vim 9.1.0016+
This occurs when your Vim version is below 9.1. You will need to upgrade Vim to 9.1 or higher.
Upgrading Vim:
Remove the old Vim version:
sudo apt remove vim vim-runtime gvim
sudo apt autoremove
Install dependencies:
sudo apt install libncurses5-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \ libcairo2-dev libx11-dev libxpm-dev libxt-dev python3-dev ruby-dev lua5.1 lua5.1-dev \ libperl-dev git build-essential
Clone the Vim repository and build it:
cd ~
git clone https://github.com/vim/vim.git
cd vim
make distclean ./configure --with-features=huge --enable-multibyte --enable-python3interp=yes \ --enable-rubyinterp=yes --enable-luainterp=yes --enable-perlinterp=yes \ --enable-cscope --prefix=/usr/local
make
sudo make install