First of all, LET me share a screenshot of my Vim. All the functions of the basic IDE can be realized. Although I use the IDE most in my daily work, AS a developer, I have to worry about it.

Vim, as an editor on a par with Emacs, doesn’t need any more praise. The learning curve is steep, but it’s easy to learn. Vim is simple to use, and the muscle you develop when you use it makes writing code enjoyable. After learning the basic vim use, everyone will go to use the road of plug-ins, or use the industry popular plug-ins, or build their own wheels, so many plug-ins in the past management is very confusing, fortunately, we have the plug-in manager Vundle, the following official from Vundle with you to create a practical vim work environment.

Vundle

Since many of our plugins will be downloaded from Github, make sure git is installed on your PC. Google it for details. Second, make sure that the vim version on the local machine is >7.4. You can run vim –version to view the vim version on the current machine, and mine will show:

Compiled Dec 6 (2013 Aug 10, Compiled Dec 6 2016 12:07:41)Copy the code

If you do not have Vim installed, or if the version is earlier than 7.4, you can install or update by running the following command: MacOS

brew update
brew install vim
Copy the code

Linux

apt-get install vim   # ubuntu
yum install vim       # centos
Copy the code

Vundle is vim’s plug-in manager. Vundle allows you to manage plug-ins in configuration files, and it is very convenient to find, install, update or delete plug-ins. It also helps you automatically configure the execution path of the plug-in and generate help files. Here also introduces another plug-in manager, named, the ghost, interested in self research, but compared to Vundle, or a weak line, so we only introduce the best.

Run the following command to install Vundle:

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Copy the code

Vimrc is vim’s Settings file. We will add a lot of Settings to it later. If we don’t change the Settings, it may not exist at first. In summary we create or open the file using vim. vimrc and add the following:

set nocompatible              " required filetype off " required
dd
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
call vundle#end() " required
filetype plugin indent on    " required
Copy the code

Then run :PluginInstall in Vim (or vim +PluginInstall +qall in Bash). From now on, just add Plugin ‘XXX’ and run :PluginInstall to automatically install the Plugin.

NERDTree

On the right side of my figure above, which is similar to the directory tree in the IDE, you can see the structure of the project more clearly, using a plugin called NERDTree.

Since we introduced Vundle above, NERDTree is a natural installation:

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'scrooloose/nerdtree'
call vundle#end() " required
filetype plugin indent on    " required
Copy the code

We added scrooloose/ NerdTree, which requires only the author name and project name of the Github repo, and execute PluginInstal to install the plugin. Let’s add the following Settings to.vimrc:

" NERDTree config
" open a NERDTree automatically when vim starts up
autocmd vimenter * NERDTree
"open a NERDTree automatically when vim starts up if no files were specified autocmd StdinReadPre * let s:std_in=1 autocmd VimEnter * if argc() == 0 && ! exists("s:std_in") | NERDTree | endif
"open NERDTree automatically when vim starts up on opening a directory
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * ifargc() == 1 && isdirectory(argv()[0]) && ! exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
"map F2 to open NERDTree
map <F2> :NERDTreeToggle<CR>
"close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Copy the code

Enter vim directly to open NERDTree, open a directory will open NERDTree, when all files are closed only NERDTree will automatically exit, also set the shortcut key F2 to switch to open or close NERDTree.

Let’s talk about some of the operations in NERDTree

NERDTree usually divides the interface into left and right panes, so you can use

to jump between panes (this means pressing Ctrl+W twice in a row). By the way, when you have a large number of panes on your desktop, you can open multiple panes horizontally and vertically in Vim. We can also perform a left/down/up/right jump with

. In each pane, you can close the pane by typing :q or :wq.
+h>
+w>
+w>
+w>

Here are some more advanced operations in the directory tree:

key describe
o Opening a file, directory, or bookmark has the same effect as pressing enter on the corresponding node
go Open the file, but the cursor remains in the directory
t Open the selected node on a new TAB
T Same as t, but the cursor remains in the directory
i Open the file in a new pane
gi Same as I, but the cursor stays in the directory
s Open a file in the horizontal pane
gs Same as s, but the cursor remains in the directory
A Enlarge the NERDTree window
p Jump to the root node
P Jumps to the parent node of the current node
K Jumps to the first node in the current directory
J Jumps to the last node in the current directory
u Set the upper-layer directory as the root node
C Example Set the current node to the root node

For more shortcuts, help Nerdtree for detailed documentation

YouCompleteMe

This famous Plugin has more than 10,000 stars on Github, which is proof of its popularity. I have tried several plugins before, but none of them are as smart as YCM, so we still use Vundle to install YCM and add this Plugin:

Plugin 'Valloric/YouCompleteMe'
Copy the code

But when vim opens a file, an error occurs:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library not detected; you need to compile YCM before using it. Follow the instructions in the documentation
Copy the code

The most complicated part of the YCM is its installation. There are always many problems. The correct installation is described in detail below:

  1. Make sure your version of Vim is7.4If not, you can install it from the source code. Of course, if vIM8.0 is available, you can also choose to install it. Secondly, make sure that your Vim supports Python2 and Python3 scripts. This can be done in Vim::echo has('python') || has('python3')If 1 is displayed, then yes, otherwise you need to install the Vim version that supports Python;
  2. To install YCM, use Vundle to install, which we’ve already covered.
  3. CLang is an open source C/C++/ objective-C /Objective-C++ compiler. YCM uses CLang to support powerful semantic analysis, which gives a more accurate complement or jump. However, use the latest libclang version, at least 3.9. Official download address, you can choose to download binary files, can also be compiled from the source code, but the compilation is really slow, it is recommended to directly under binary, pay attention to the system.
# for ubuntu14.04Wget HTTP: / / http://releases.llvm.org/3.9.0/clang+llvm-3.9.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz#for macOSWget HTTP: / / http://releases.llvm.org/3.9.0/clang+llvm-3.9.0-x86_64-apple-darwin.tar.xzCopy the code

Download and decompress:

xz -dClang + LLVM 3.9.0 x86_64 - Linux - gnu - ubuntu 14.04. Tar. Xz tar - XVF clang + LLVM 3.9.0 x86_64 - Linux - gnu - ubuntu 14.04. The tar# Same command on MacOS
Copy the code
  1. Next, we need to compile oneycm_coreFor YCM, so that it can quickly generate semantic analysis to complete or function variables quickly jump. First we need to installcmakeTo generate amakefilesFile:
 #ubuntu
 sudo apt-get install cmake  
#macOS
brew install cmake 
Copy the code

Second, you need to install the Python header:

 sudo apt-get install python-dev python3-dev 
It should be installed by default on the MAC
Copy the code

We default you have used Vundle installed YCM in ~ /. Vim/bundle/YouCompleteMe. Let’s create a directory to compile:

cd ~
mkdir ycm_build
cd ycm_build
Copy the code

Let’s generate the makeFiles first, if we don’t care about C language support:

cmake -G " Unix Makefiles" ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
Copy the code

Of course, we’ve already downloaded Clang3.9, so it’s better to do this:

Move the downloaded clang to a LLVM directory you createdMkdir -p ycm_temp/llvm_root_dir mv ~/clang+llvm-3.9.0-x86_64-linux-gnu-ubuntu-14.04/* ~/ycm_temp/llvm_root_dir/cd  ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
Copy the code

This will generate makefiles based on the latest clang, and the next step is to compile:

cmake --build . --target ycm_core --config Release
Copy the code

This is almost finished, of course, this only for the C department of language semantic support, if you need to support other languages, you need to see the official tutorial.

Tutorial: To use the power of YCM, you need to provide libclang with your project’s compile flags, which means that libclang can parse your code so that it can give intelligent semantic analysis. There are two ways to do this: automatically generate the compile database or manually add compile flags.

  1. Automatic generation:The easiest way to do this is to use your own project’s compiler tools to generate a database of compiled data, such as the one we used earlierCMakeOf course a lot of what we use under Linux isGun MakeWe need to download oneBearTool, download the source code after installation:
Cmake <Bear source directory > make all make install# to install
make check   # to run tests
make package # to make packages
Copy the code

Then go back to your project,bear make the entire project, and generate compile_commands. Json, which YCM uses to do semantic analysis. With CMake you don’t need Bear, just add -dcmake_export_compile_commands =ON at compile time or set(CMAKE_EXPORT_COMPILE_COMMANDS ON cmakelists.txt ON to copy the generated compiled database information to the root directory. Ycm_extra_conf. py, based on your file name, automatically gives YCM some compilation options for how to parse your code. In ~ / vim/bundle/YouCompleteMe/CPP/ycm /. Ycm_extra_conf. Provides the default template in py, generally we will customize it flags array, and then copy a to ~ directory, because ycm is always in the current directory, or upper recursive directory, Find a available.ycm_extra_conf.py

Define a jump

  • Jump to Define GoToDefinition
  • Jump to the declaration GoToDeclaration
  • And both fit GoToDefinitionElseDeclaration in.vimrcShortcut keys can be defined in:
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
Copy the code

Let mapleader=”\

” let mapleader=”\

” let mapleader=”\

” let mapleader=”\

” YCM also supports semantic diagnosis:

let g:ycm_error_symbol = '> >'
let g:ycm_warning_symbol = '> *'
Copy the code

In this case, illegal statements will display an error at the beginning of the line, basically the same as the IDE.

TagBar

A typical IDE generates a structure diagram of the current file on the side, and while sublime has a thumbnail of the file, in Vim we can add a tagbar that allows us to quickly navigate to function variables and know the code as we work on a file. But make sure you have ctags before using the TagBar.

#Linux
sudo apt-get install ctags
#MacOS
brew install ctags
Copy the code

The installation

Plugin 'majutsushi/tagbar'
Copy the code

Run the install command again and set it to this in.vimrc:

" Tagbar
let g:tagbar_width=35
let g:tagbar_autofocus=1
let g:tagbar_left = 1
nmap <F3> :TagbarToggle<CR>
Copy the code

This brings up the TagBar pane by pressing F3.

Ctrap

In the original diagram, my lower pane was dedicated to searching for files, which is supported using the Ctrap plug-in. The installation

Plugin 'ctrlpvim/ctrlp.vim'
Copy the code

After executing the PluginInstall command, let’s do some Settings:

Let g:ctrlp_map = '
      
       ff' let g:ctrlp_cmd = 'CTRLP'"
      Show recently opened files Map <leader>fp :CtrlPMRU<CR>"set wildignore+=*/tmp/*,*.so,*.swp,*.zip " MacOSX/Linux" let g:ctrlp_custom_ignore = { \ 'dir': '\v[\/]\.(git|hg|svn|rvm)$', \ 'file': '\v\.(exe|so|dll|zip|tar|tar.gz)$', \ } "\ 'link': 'SOME_BAD_SYMBOLIC_LINKS'.let g:ctrlp_working_path_mode=0
let g:ctrlp_match_window_bottom=1
let g:ctrlp_max_height=15
let g:ctrlp_match_window_reversed=0
let g:ctrlp_mruf_max=500
let g:ctrlp_follow_symlinks=1
Copy the code

This allows you to use space +ff to enable search, space + FP to display recently opened files, Ctrl+ K /j to move up and down the file list, Ctrl+p/n to switch up and down the search history you entered, and more to see :help ctrlp-commands. The default search tool is grep. Now everyone knows that AG is more efficient and faster, so if you want to switch the search tool, you can change it like this:

if executable('ag')
  " Use Ag over Grep set grepprg=ag\ --nogroup\ --nocolor " Use ag in CtrlP for listing files.
  let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
  " Ag is fast enough that CtrlP doesn't need to cache let g:ctrlp_use_caching = 0 endifCopy the code

vim-powerline

This tool is mainly used to enhance the status bar, showing more information, file format, current status, path

Plugin 'Lokaltog/vim-powerline'
let g:Powerline_symbols = 'fancy'
set encoding=utf-8 
set laststatus=2
Copy the code

Some other Settings

Color match For the appearance level control, a good color match can also make work a lot of pleasure. My theme color scheme is solarized and can also be installed with Vundle. Then directly set:

syntax enable
set background=dark
colorscheme solarized
Copy the code

Some basic Settings

"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="General  
"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" historyStorage length.set history= 1000"Check filetype on"Use different indent formats for different file types. Filetype indent on allows automatic completion of the filetype plugin"Compatible with VI mode. Remove the annoying consistency mode related to VI, avoid some bugs and limitations of previous versions set nocompatible set Autoread"The file is loaded automatically after modification.set shortmess=atI       "Do not display the aid to Somali children prompt when activated."Cancel the backup."urn backup off, since most stuff is in SVN, git et.c anyway... set nobackup set nowb set noswapfile "Keep format when Postingset paste  
"- click on the cursor to copy set mouse-=a"Turn on the mouse under all modes.set selection=exclusive    
set selectmode=mouse,key  
  
" No annoying sound on errors "Remove the sound for input errorsset noerrorbells  
set novisualbell  
set t_vb=  
set tm=500    
  
"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" show and format  
"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="Display line number:set number  
set nowrap                    "Cancel line feeds. "To facilitate replication, use <F6> to enable/disable the line number display: nnoremap <F6> :setnonumber! <CR>:set foldcolumn=0<CR>  

"Bracket match set showmatch" How many tenths of a second to blink when matching brackets  
set mat=2  
  
"Setting in-text intelligent search prompts"Highlight the text hit by search.set hlsearch            
"Ignorecase when searching set ignorecase"As you type in instant searchset incsearch  
"Case sensitive set SmartCase with one or more uppercase letters"Code folding,set foldenable  
"Folding method"Manual Manual folding"Indent means fold."Expr uses expressions to define folds"Syntax uses syntax to define folding"Diff folds the text that has not changed{{{and}}} set foldMethod =syntax"Show the layers of folds on the left"set foldcolumn=4 set tabstop=4 "Set width of Tab key [equivalent space number]set shiftwidth=4  
set expandtab                "Automatically convert Tab to space [Ctrl+V + Tab to enter real Tab]"You can delete four Spaces at a time by pressing the backspace keyset softtabstop=4  
  
set ai "Auto indent set si "Smart indent  
  
"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =" status  
"= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="Display current row number column number:set ruler  
"Show the command set showcmd being entered in the status bar"Set 7 lines to the cursor - when moving vertically using J/K scroll up and down, always in the middleset so=7    
"set cursorline "Highlight the current lineCopy the code

Due to the space problem, I recommend some other good plug-ins

" Improved C++ STL syntax highlighting Plugin 'STL-improved' " recommend fetch it from https://github.com/tczengming/autoload_cscope.vim.git which support c and cpp
Plugin 'tczengming/autoload_cscope.vim'

Plugin 'CmdlineComplete'
Plugin 'xptemplate'

" Ultimate auto completion system for Vim Plugin 'neocomplcache' Plugin 'genutils' Plugin 'lookupfile' " Fast file navigation
Plugin 'wincent/Command-T'

" Preview the definition of variables or functions in a preview window Plugin 'autopreview' " Echo the function declaration in the command line for C/C++
Plugin 'echofunc.vim'

" Under linux need exec 'dos2unix ~/.vim/bundle/QFixToggle/plugin/qfixtoggle.vim' Plugin 'Toggle' Plugin 'Color-Sampler-Pack' Plugin 'txt.vim' Plugin 'mru.vim' Plugin 'YankRing.vim' Plugin 'tpope/vim-surround.git' Plugin 'DoxygenToolkit.vim' Plugin 'tczengming/headerGatesAdd.vim' Plugin 'ShowMarks' Plugin 'Lokaltog/vim-powerline'Copy the code