The legendary Vim has always had an indelible myth, in order to pursue efficiency, embedded development should be transferred to LInux, first of all, of course, learn to use the powerful Vim ~
1. The vi and vim
2. Use vim
Before you start learning how to use Vim, take a look at the three ways viM works:
- Command mode ViM is in command mode by default. In this mode, you can simply use VIM shortcut keys to operate text, such as cursor movement, text copy, move, paste, select, replace, etc.
- Insert mode
Press in command modei
Enter insert mode, you can input text as normal editor; - At the end of the line mode
Enter in command mode:
Enter last-line mode, in which the vim command can be used;
In last-line mode and insert mode, useESC
Return to command mode.
Vim gives us a great 30min tutorial, typing in terminalvimtutor
Enter the tutorial, this tutorial includes almost all commonly used VIm operations, serious after this tutorial will naturally ~
Vim is powerful not only because of its effective working mode and operation commands, but also because of the various plug-ins provided by millions of users, which make it shine.
3. Basic vim startup configuration
Many of vim’s features are not enabled by default (such as the display of line numbers), but these Settings can be enabled in the last line mode with the set nu command, which is inconvenient, so vim provides a convenient way to create the startup script file vimrc. If this configuration is used by the current user, you can create it using vim ~/. Then add the commands that need to be executed before VIM starts, that is, the viM Settings you need. Here are some of my configurations for reference:
"author@ mculover666 "bash option
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"Enable line number displayset nu
"Set F1 to show/not show line number"Not compatible with VIset nocompatible
"Turn on syntax highlighting"The current mode is displayed at the bottomset showmode
"Command mode display input command set showcmd"Mouse supportset mouse=a
Set t_Co=256"Use UTF-8 encodingset encoding=utf-8
"Enable filetype checking and load the corresponding indent rule filetype indent on"The current line of the cursor is highlightedset cursorline
"Automatically highlight another matching bracket set showmatch"When you enter search mode, each character you enter automatically jumps to the first matching resultset incsearch
"Automatically switch working directory according to edit file set autochdir"Open file monitoring, external changes after the promptset autoread
"When searching, highlight matching results set hlSearch"When you enter, the next line is indent the same as the previous lineset autoindent
"TAB indent =4 set tabStop =4"All TAB indent valuesset shiftwidth=4
Set expandTab = expandTab;How many Spaces does TAB convert toset softtabstop=4
"bash option end
"--------------------------
Copy the code
4. Configure the VIM plug-in
4.1. Preparations
- Vim version > 7.4
- Installation of the
git
4.2. Vim plug-in manager Vundle
Vundle is a plug-in for VIm, you can directly manage plug-ins in the configuration file VIMRC, convenient and practical ~
- Use Git to download vundle from Github and place it in vim’s bundle path:
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Copy the code
- in
~/.vimrc
Add configuration to:
"vundle options
"-------------------------------------
"Filetype check fileType off"Enable auto - complete fileType Plugin indent onset rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
call vundle#end()
"vundle option end
"--------------------------------------
Copy the code
- Run vim and type in last-line mode
PluginInstall
Copy the code
- Vundle is successfully installed. Only the configuration file is required when installing the plug-in
vimrc
addPlugin < plug-in name >
And then runPluginInstall
The plug-in can be installed.
4.3. NERDTree plugin — NERDTree
Add a line to the vundle configuration:
Plugin 'scrooloose/nerdtree'
Copy the code
Then open vim and type “PluginInstall” in the last line mode to install.
" 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 * ifargc() == 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 * if argc() == 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
4.4. Autoprompt plugin — YouCompleteMe
4.4.1. Preparations
- Example Query the VIM version
Enter in vim’s last-line mode:version
:
- The major version needs to be greater than 7.4
- If the motherboard is equal to 7.4,
Include patches
The last number of the number needs to be 1578 or higher
- Query the Python version supported by Vim
- The input
echo has('python')
If the output is 1, python2 is used later - The input
echo has('python3')
If the output is 1, python3 is used later
I’m using python3~ here
4.4.2. Install YouCompleteMe in Vundle
As with other plug-ins, add the following line to ~/. Vimrc:
Plugin 'Valloric/YouCompleteMe'
Copy the code
Then open Vim again. This plug-in takes a long time to download (2h), so you need to wait patiently. Wait for the YouCompleteMe installation to complete and start the Clang installation
4.4.3. Install Clang + LLVM
Then choose the software source corresponding to your system, which I used hereUbuntu 16.04:
- Copy the software source configuration found above and add it to
/etc/apt/source.list
File, and then useapt update
Update software source; - Perform retrieval of archive signatures
wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
Copy the code
- Install clang, LLD, LLDB (version 7)
Sudo apt-get install clang-6.0 lldb-6.0Copy the code
4.4.4. Compile the yCM_core library next
- Install cmake
sudo apt-get install cmake
Copy the code
- Install python – dev
sudo apt-get install python-dev python3-dev
Copy the code
- Create the compile directory and generate the makefile
cd ~/.vim/bundle/YouCompleteMe/
mkdir ycm_build
cd ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
Copy the code
- Compile ycm_core library
cmake --build . --target ycm_core --config Release
Copy the code
- Build regular expression modules to use regular expressions to improve Unicode support, and compilation
ycm_core
Library is similar
cd ../
mkdir regex_build
cd regex_build
cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex
cmake --build . --target _regex --config Release
Copy the code
4.4.5) configuration
- Copy the.ycm_extra_conf.py file:
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
Copy the code
- Then configure the startup profile
vim ~/.vimrc
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
Copy the code
With the YouCompleteMe plugin installed and configured successfully, write a test.c test: