[CNMP series] explanation of VIM editor and cnmpvim
Origin
I used Linux Kernel Driver R & D in college. I used the Vim editor to write C language. Vim was not as powerful as today, and there were not so many plug-ins at that time, I just think this editor can satisfy all I want, view the Linux kernel code, debug the C program, debug the assembly code, write the Shell script, run the scheduled test task, and so on, you cannot leave this editor. Probably because I was forced to install the Linux system in order to save hundreds of operating system fees, I realized that this was a guide to Linux, and get deeper and deeper.
In the past, some people used to ridicule programmers and say that PHP is the best language in the world. Today, we bring you a heavyweight player in the CNMP series, the Vim editor in Linux.
There is no doubt that Vim is the best editor in the world. It has the title of the god of editors, formerly known as Vi. Powerful functions and highly customizable features.
It is not difficult to be familiar with Vim, but it is difficult to change some of your habits. For example, when you use an editor, you must put your right hand on the mouse, the right hand is taken away from the mouse only when the cursor code is started. This reminds me that mac is the most suitable computer for programmers, not because his mouse is at the bottom of the keyboard. However, to be honest, mac touchpad is really easy to use!
Remember this sentence: [at the beginning of vim's design, the entire text editing was completed with a keyboard instead of a mouse.] Almost every key on the keyboard has a fixed usage, in addition, the producer of vim wants the user to complete most of the editing work in normal mode (that is, the command mode. Only the user can enter the command.
Insert mode and command mode
Compared with other editors, Vim has two modes. The first one is common and most editors have the insert mode, the second mode is the command mode that distinguishes it from other editors.
The Skillful Use of the command mode can make you feel that the mouse is not applicable to the correctness.
Switch Mode
After entering Vim, press the keyboard
I: insert I to the left of the current character: Insert a to the beginning of the current line: Insert A to the right of the current character: insert o at the end of the current line: insert a new line O under the current row: Insert a new line above the current row
These characters can enter the insert mode. In the insert mode, Press ESC to return to the command mode.
The most common way to switch between the two is to enter vim, press [a] to enter the edit mode, and then press [ESC] to enter the command mode.
Screen Jump Control Command
[Command mode]
<Ctrl-f>: Move the screen down. <Ctrl-d>: Move the half screen down. <Ctrl-B>: Move the screen up. <Ctrl-u>: Move the half screen up.
Number of rows to jump
[Command mode]
NumG: move the cursor to the specified row (num)
Cut, copy, and paste
[Command mode]
D: paste the selected content to the clipboard. Y: copy the selected content to the clipboard. Dd: Cut the current row. Yy: copy the current row. P: paste the clipboard content in the cursor Area
Undo
[Command mode]
U: undo the edited content.
High energy prompt !!! High energy prompt !!! High energy prompt !!! Never use <ctrl + z> In vim.
Common commands
The following describes some of the commands you may use in your daily work.
SEARCH Command
/Text Search text, search for the next one by njian, and search for the previous one by njian.
Replacement command
S/old/new/Replace new with old, replace the first matching s/old/new/g of the current row with old to replace new, replace all matching % s/old/new/Replace new with old, replace % s/old/new/g with old, replace all the matches in the entire file: 10th s/^ // g add four spaces in front of each line in 20th row to indent.
Exit command
: Wq: Save and exit: q! Force exit and ignore all changes: e! Discard all modifications and open the original file.
So far, you can edit what you want to edit, which is not much different from the general editor ~
Vim plugin
Next we will go to the topic. A major feature of vim is its high scalability! Installation and Use of plug-ins
On the left side is the Vim plug-in File Manager NERDTree. I will give you a detailed answer later.
The right side is part of my vimrc file.
Plug-in Manager
When talking about plug-ins, the first thing we think of is the manager, which is similar to the package manager, Python pip, PHP Composer, CentOS yum, Ubuntu apt-get, and Mac brew, isn't it cute.
Vim also has its own plug-in manager, which is the well-known [vundle] and the oldest Vim plug-in manager.
Download and install vundle as follows
#git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
This command will download the Vundle plug-in Manager and place it in your Vim editor bundles folder. Now, you can use the. vimrc configuration file to manage all extensions.
Vimrc
The vim configuration file, the functions you want, including the download and installation of plug-ins, are all configured in this directory, and then input in command mode
:PluginInstall
You can complete the installation of the plug-in. Don't forget the previous.
First, let's take a look at whether the vimrc file exists in your home directory. It does not exist by default. If you have edited it before, you can. It doesn't matter. Let's repeat it from the beginning.
#ls ~/.vimrc
【~] Is the home Directory of the corresponding user in Linux.
If you don't have one, you can touch one or use vim to directly edit it.
#touch ~/.vimrc
Edit the above. vimrc file and add the downloaded Vundle configuration to the top of the configuration file:
set nocompatible " requiredfiletype off " required" set the runtime path to include Vundle and initializeset rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()" alternatively, pass a path where Vundle should install plugins"call vundle#begin('~/some/path/here')" let Vundle manage Vundle, requiredPlugin 'gmarik/Vundle.vim'" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
Note: "The beginning is a comment.
Plug-in Installation
Install a plug-in first. Let's start with the File Manager.
Add a line in the above vimrc File
Plugin 'scrooloose/nerdtree'
In command mode, enter ": PluginInstall]
If the following interface appears, the installation is successful!
The leftmost window is the display page of plug-in installation. All successfully installed plug-ins are displayed here. How can we jump between several split windows? Press <ctrl + ww>. Remember, it's two million yuan.
Close a window and jump to that window. In command mode, enter [: q] to close the window.
What? Open a directory without folders? Because you haven't opened NERDTree yet, enter NERDTree in the command line to open the folder management directory. If you want to open it, the current folder will be displayed. In vimrc, enter the following two lines.
autocmd VimEnter * NERDTree let NERDTreeShowBookmarks=1
Code auto-completion
YouCompleteMe-one developed by Google's C ++ engineer Valloric (Val Markovic), which is specialized in Automatic completion tools for C series (C, C ++, C, the advantage is that it is fast and supports variable jump, function Association jump, and code error detection. It can be said that it is the most powerful completion tool, and the disadvantage is that it is too difficult to configure, it is not supported by other languages except C. it is unfriendly for those who are new to Vim, but the documentation is complete.
Git Version Control
Only Git highlighting is supported, and the speed is very good.
Plugin 'vim-gitgutter'
Update and install plug-ins
After editing the vimrc file, you must enter the following command in command mode to update and install the plug-in.
:source %:PlugInstall
Change the code under git management. Add a new line and a small + number will appear on the left side.
Common vimrc configuration commands
Most "modern" integrated development environments (IDES) provide a means to fold methods (methods) or classes (classes) and display only the definition of classes or methods, instead of all the code.
You can.vimrcAdd the following code to enable this function:
" Enable foldingset foldmethod=indentset foldlevel=99
You must enterzaTo fold (and cancel the fold ).
Let the Tab key in Vim be equivalent to four standard space characters. Make sure that each line of code contains no more than 80 characters and files are stored in unix format, avoid file conversion issues when pushing to Github or sharing with other users.
set tabstop=4set softtabstop=4set shiftwidth=4set textwidth=79set expandtabset autoindentset fileformat=unix
Support UTF-8 Encoding
set encoding=utf-8
Enable line number display:
set nu
Syntax highlighted
syntax on
All about vim is mentioned here. There are actually a lot of vim, including linux. In fact, there are a lot of things that let us learn. I mean learning is not just about using tools, it is code-level learning, especially the source code of the Linux kernel, which can make you learn a lot and turn it into an idea. It can also be said that it is an algorithm. It is really brilliant. People who have read it know it ~~~