I. Basic editing functions
1. Copy, cut, and paste
Copy a specific segment: move the cursor to the header of the text to be copied, press "V", move the cursor back, and the character where the cursor is crossed will be highlighted, after moving to the end of the text to be copied, press "Y" to copy all the highlighted text to the clipboard.
Copy a row: move the cursor to the row to be copied, and press "YY" to copy the row to the clipboard.
Cut a specific section: move the cursor to the header of the text to be cut, press "V", move the cursor back, and the character where the cursor is crossed will be highlighted, after moving to the end of the text to be cut, press "Y" to copy all the highlighted text to the clipboard.
Cut a row: move the cursor to the row to be cut, and press "DD" to copy the row to the clipboard.
Paste the content of the clipboard to the cursor: move the cursor to the position to be pasted, and press "p ".
2. search and replace
Search for the string "example":/example.
Search the string "example ":? Example.
After the first string is found, press "N" to search for the Next string and press "N" to search for the Next string.
Full text replacement: Replace the full text old with new: % S/old/new/g
Replace the full text old with new, but each request is confirmed: % S/old/new/C
3. Undo and redo
Undo: Press "U ".
Redo: press Ctrl + R ".
4. Save and save
Save: "W ".
Save as: "W filename ".
Ii. syntax highlighting and format setting
First, import the vimrc sample file: read $ vimruntime/vimrc_example.vim in VI. Save it to your user directory and the file name is. vimrc. The file contains syntax on. Enable the syntax highlighting function.
There are several useful settings in this file:
Set ruler "the cursor position is displayed at any time.
Set showcmd "displays incomplete commands.
Set tabstop = 4 "tab key indent 4 cells.
3. Keyword completion
CTRL + N: Find the matching keyword to complete the unspelled keywords. If there are multiple matching items, press Ctrl + N each time to switch to the next item. If you are used to adding keywords with the tab key, you can add the following code to the. vimrc file:
Function! Clevertab ()
If strpart (Getline ('.'), 0, COL ('.')-1) = ~ '^/S * $'
Return "/<tab>"
Else
Return '/<C-N>"
Endfunction
Inoremap <tab> <C-R> = clevertab () <CR>
4. edit a project
We usually develop more than one source program file in a program. to edit the source code in the entire project, we need to switch between files frequently. In this case, you can run the following command:
: E filename
Switch to the target file without exiting VI.
After editing is complete, make is required. Enter the make command directly. If there is an error, after the command is executed, it will automatically jump to the first error. There are several other commands that can be located between error lines:
: CC error message.
: The next error message is displayed in CN.
: CP displays the previous error message.
: CL: list all errors.
V. Others
1. Matching brackets.
Including {} [] () Matching. Press % on a bracket to automatically jump to the matching bracket. Then press % to jump back.
2. Get online help.
When editing the program source code, if the usage of a system function is not very clear, move the cursor to the function and press "K", the man help information of the function will jump out.
3. Return to shell temporarily.
Enter the command Sh.
4. execute shell commands in VI.
Enter :! CMD, for example :! Ls.
Practical Tips: Set syntax highlighting in the vim Editor
If you want Vim to enable the support for syntax highlighting by default (only in terminals that support colors), edit (implemented in Ubuntu 7.10 amd version, most of the time, you only need to use sudo apt-Get install Vim to upgrade and install Vim to implement this function. However, this problem often occurs in Linux. The following method is more direct)
Sudo Vim ~ /. Vimrc
Content:
If & t_co> 1
Syntax enable
Endif
Vim STARTUP configuration
When I am not used to VI, I feel that it is inconvenient to use it to view the code. In fact, the VI function is very powerful, and you can get a friendly editor by slightly configuring it. At startup, VIM reads/usr/share/Vim/vim61/. vimrc (Global vimrc) and ~ /. Vimrc (User-Defined vimrc ,~ /Is the root directory home/usrname/after you log on, so you can create an RC file as needed) and configure Vim settings based on the file content.
The following is an example vimrc file:
Set nocompatible
Set BS = 2
Set columns = 80
Set background = dark
Set tabstop = 8
Set wrapmargin = 8
Set nobk
Set cindent
Set shiftwidth = 8
Syntax on
Set ruler
Set noexpandtab
The specific settings are as follows:
Set nocompatible: This setting prevents Vim from working in a highly compatible way with vi. This setting needs to be at the beginning of each vimrc file, thus affecting many of the subsequent settings;
Set BS = 2: this will affect the work of the backspace key. For details, refer to: Help 'bs ';
Set columns = 80: set the number of rows on the screen;
Set background = Dark: displays in a comfortable color on a dark background;
Set tabstop = 8: number of spaces skipped by the tab key. tabstop = 8 is the default value. to be compatible with the editor on windows, we recommend that you select tabstop = 4;
Set wrapmargin = 8: set where to start line feed on the right of the window;
Set nobk: disable file backup;
Set cindent: Enable indent;
Set shiftwidth: Set the indent length. shiftwidth = 8 is the default value. We recommend that you select shiftwidth = 4 for compatibility;
Syntax on: Enable syntax structure highlighting;
Set r: enables Vim to display the current number of columns in the lower right corner;
Set noexpandtab: Enable Vim to insert a tab rather than a string of spaces at the tab.
Read the help information in VIM to obtain a lot of VIM parameter information. In vim, enter: help to get help, or: Help usr_toc.txt to read the user manual directory.
Highlight PHP code
After entering vim, enter the following command in normal mode to enable PHP code highlighting:
: Syntax enable
: Source $ vimruntime/syntax/PHP. Vim
From: http://www.linuxpk.com/43172.html