VI and VIM editor use

Source: Internet
Author: User

VI and VIM
1. Introduction:

The VI Editor is the standard editor for all UNIX and Linux systems, and he is the equivalent of a notepad in the Windows system, which is no less powerful than any of the latest text editors. He is a tool we cannot use without a Linux system. Because of any version of UNIX and Linux systems, the VI editor is identical. Vim has the ability of program editing, it can distinguish the correctness of grammar by font color, and it is convenient to program design. Because the program is simple, the editing speed is quite fast. Vim can be used as an upgraded version of VI, and he can display some special information in a variety of colors. Vim will automatically execute the syntax judgment of the program according to the file extension or the beginning information in the file, and then display the program code and general information in color. Vim adds a lot of additional features, such as support for regular expression search, multi-file editing, block copying, and so on. This is a great feature for the modification of some of the configuration files on Linux.

Use of 2.VI:

  Basically VI can be divided into three states, namely the general mode, the editing mode and the command line mode, the function of each mode is distinguished as follows:

  General mode:
Open a file with VI directly into the general mode (this is the default mode). In this mode, you can use the left and right buttons to move the cursor, you can use the deletion of characters or delete the entire line to process the contents of the file, you can also use copy, paste to process your file data.

Description of buttons available in general mode
Move cursor
"H, J, K, L", respectively, control the cursor left, bottom, up and right one grid
Press "ctrl+b" screen to move one page to "back"
Press "ctrl+f" screen to move one page toward "front"

"n<space>" cursor moves n characters to the right
"Home" moves to the front of the line prompt: 0 digits, but not with numbers on the numeric keypad
"End" moves to the last face of this line prompt: $, I'm not testing it.
"W" cursor jumps to the beginning of the next word
"E" cursor jumps to the end of the next word

The "H" cursor moves to the first character of the line at the top of the screen
The "M" cursor moves to the first character of the line in the middle of the screen
The "L" cursor moves to the first character of the line at the bottom of the screen

"G" moves to the last line of this file
"NG" moves to the nth row of this file (mates: Set Nu)
"GG" moves to the first line of this file, equivalent to 1G
"n<enter>" cursor moves down n rows

Find and replace
"/word" looks down at the cursor for a string called word
"word" looks up a string with a name called Word to the cursor
"N" represents the action of repeating the previous lookup
"N" is exactly the opposite of N and a find action for "reverse" before the line

": n1,n2s/word1/word2/g" N1 and N2 are numbers, find N2 between the N1 and Word1 lines and replace the string with Word2

": 1, $s/word1/word2/g" finds the Word1 string from the first row to the last line and replaces the string with Word2
": 1, $s/word1/word2/gc" finds the Word1 string from the first row to the last line, replaces the string with Word2, and prompts the user to confirm replacement before replacing

Delete, copy, and paste

"X" removes one character (equivalent to "Del" key) for backward
"X" to remove one character (equivalent to "backspace" key)
"NX" continuously deletes n characters backwards

"DD" deletes the cursor in the row
"NDD" Removes the downward n-line of the cursor
"D1G" removes all data from the line to the first row of the cursor
"DG" removes all data from the last row of the cursor

"d$" deletes the cursor where the last character of the line
"D0" deletes the first character of the line at the cursor location

"YY" the line where the cursor is copied
"Nyy" Copy the Down n column where the cursor is located

"Y1G" copies all data from the row to the first row of the cursor
"YG" copies all data from the row to the last row of the cursor

"Y0" copies all data from the same character as the cursor to the beginning of the line
"y$" Copies all data at the end of the line with the same character as the cursor

"P" Pastes the copied data on the next line of the cursor
"P" is the last line attached to the cursor

"U" restores the previous operation
"Ctrl+r" Redo last action

"." is to repeat the previous action

General mode switch to edit mode available button description
"I, i" Enter edit mode:
I is "inserting from the current cursor position"
I start inserting "at the first non-whitespace space where I am currently in line"

"A, a" enters edit mode (Insert mode):
A is "insert from the next character where the cursor is currently located"
A is "insert at the last character of the line where the cursor is located"

"O, o" enters edit mode:
O to "insert a new line at the next line where the cursor is currently located"
O inserts a new row for the previous line at the current cursor position

"R, R" enters the replacement mode:
R will only replace the one character where the cursor is located once
R will always replace the text of the cursor until ESC is pressed;

"ESC" exits edit mode and returns to normal mode

  Edit mode:
In general mode can be deleted, copy, paste and other operations, but cannot edit the contents of the file, only when you press "I, I, O, O, A, a, R, R" and so on any one letter will enter the editing mode. The word "Insert or REPLACE" appears at the bottom left of the screen, so you can edit it. If you want to go back to normal mode, you must press ESC to exit edit mode.

Command-line mode:

  Enter ":/? Any of the three, you can move the cursor to the bottom row. In this mode, you can provide search, read, save, replace characters, leave VI, display line number and other actions are done in this mode! 

General mode switch to command-line mode available button description
": W" saves the edited content
": w!" forces the file to be written but is related to your permissions on the file
": Q" Leave VI
": q!" do not want to save changes forced to leave
": Wq" left after saving
": X" left after saving
"ZZ" If the file is not changed, then do not save leave, if the file has been modified, then save and leave

": w filename" saves the edited data to another file (similar to save)
": R filename" in the edited data, read into the data of another file. The contents of the file "filename" will be appended to the line of the cursor.

": n1,n2 w filename" Saves the contents of N1 to N2 as filename for this file.
【:! Command "temporarily leave the display result of commands in the VI-to-line mode! For example ":! Ls/home "You can see in VI the file information under/home with LS output!

": Set nu" Displays line numbers
": Set Nonu" In contrast to set Nu, to cancel a row

3.vim cache file, restore and open warning message
We know that some common editing software, there is a recovery function, that is, when your system for some reason to cause similar to the situation of the machine, you can also use this recovery function to the previous unsaved data back. Our vim also has this function. When we use VIM to edit, Vim will be in the directory with the edited file, and then create a file named. filename.swp. If your system is disconnected for some reason and the file you are editing is not saved, this time. FILENAM.SWP will be able to send a rescue function.

Example 1:

1.vim Test.txt

[Email protected]_man ~]# vim test.txtctlnameage

2.ctrl+z put in the background to stop the VIM process

[[Email Protected]_man ~]# Ps-ef |grep vimroot2495   2181  0  One: $pts/0    xx:xx:xxVim Test.txtroot2497   2181  0  One: $pts/0    xx:xx:xxgrep vim[[email Protected]_man~]# Kill-9 2495

3. We use the ls-l command to check the directory, we will find a. test.txt.swp file, this file is a cached file

Let's edit vim Test.txt again.

There will be some information
Problem one: There may be other people or programs editing this file at the same time:
Problem two: In the previous vim environment, VIM interruption (crashed) may be caused by some unknown cause:

There are six command entries in the lower right corner, which can be described as follows:

(O) Pen read-only: Opening this file becomes read-only and can be used when you simply want to review the contents of the file and do not want to edit the behavior. Generally speaking, in class, if you are logged in to the classmate's computer to see his profile, the results found that students in fact, he himself in the editing, you can use this mode;
(E) dit anyway: If you open the file you want to edit in a normal way, it will not load the contents of the scratch disk. If two people are editing this file, it is easy to change each other's files and so on.
(R) Ecover: The content of the scratch disk is loaded, and is used when you want to save the unsaved work. But when you get back and save the vim, you still have to manually delete the temporary archive yourself.
(D) Elete it: You are sure that the temporary archive is useless! The scratch disk will be deleted before you open the file.
(q) Uit: Press Q to leave Vim without any action to return to the command prompt character.
(A) Bort: Ignore this editing behavior and feel very similar to quit!

Features of the 4.vim

 In fact, most of the Linux distributions now replace VI with VIM. Why use VIM? Because Vim has the function of color display, it also supports many program syntax (syntax) and corresponding prompt information. To see if your VI is being replaced by VIM, you can use the alias command to see if there is an alias vi= ' Vim ' line.

Block selection:

"V" character selection, will be the place where the cursor will be reversed white selection
"V" line selection, the line of the cursor will be reversed white selection
"Ctrl + V" Block selection, you can select the data in a rectangular way (obtained vertical column)
"Y" will copy the anti-white place
"D" will remove the anti-white place

Multi-file editing:

When you use Vim, you may encounter the need to copy a section of a file into another file, and Vim will not be able to keep it when it is closed. Or it is copied in other ways.
"Vim file1 file2"

": N" edits the next file
": N" Edit Previous file
": Files" lists all the files currently in this vim editor

Multi-window function:

There are two files that need to be viewed against.
": SP filename" to open a new window, if there is a filename, to open a new file in a new window, otherwise two Windows is the same file content (synchronized display).

"Ctrl+w+j"
Press the "ctrl+w+↓" button by pressing "CTRL", then pressing the W to release all the keys, then pressing the J (or down ARROW key), the cursor can be moved to the lower window.

"Ctrl+w+k"
"Ctrl+w+↑", but the cursor moves to the window above.

Vim environment settings and logging (~/.VIMRC, ~/.viminfo):

If we use the VIM software to find a string inside a file, the string will be reversed, and the next time we edit the file with Vim again, the string anti-white case of the lookup is still there. Also, when we repeatedly edit the same file, when we enter the file for the second time, the cursor is at the beginning of the last line that we left. This feature may be convenient, but there are also inconvenient times. How could that be? This is because our VIM will take the initiative you have done the behavior of the log down, the record action file is: ~/.viminfo, do not want to use this function, directly delete ~/.viminfo. As long as you have used vim, your home directory will have this file. This file is automatically generated, and the actions you've made in Vim can be found inside this file. Interested friends can view the contents of the file on their own.

However, the default environment for VIM is not the same for each different release version. For example, some versions do not have a high-brightness anti-white when they find keywords, and some versions actively help you indent (so-called indentation, that is, when you press Enter to edit a new line, the cursor is not aligned at the beginning of the line, but in the first non-whitespace space on the previous row). In fact, these can be set on their own, the following we will look at the environment of vim settings.
Vim environment setting parameters are many, if you want to know the current setpoint, you can enter in the general mode ": Set all" To check, because the project is too many, we are here to list only some of the more commonly used in some simple set of values, to provide you with reference.

: Set All "Displays all currently set values for the environment parameter
: Set Hlsearch "high brightness anti-white (high brightness search)
: Set Nohlsearch "Cancel high brightness anti-white (high brightness search)
: Set backspace=2 "At the time of editing can be deleted at any time with backspace key (0, 1, only for the characters just entered valid)
: Set autoindent "Auto Indent
: Set noautoindent "Cancel Auto indent
: Set ruler "to show the status of the last row
: Set Showmode "The state of the lower left corner of the line
: Set nu "Display line number
: Set Nonu "Cancel line number
: Set Bg=dark "shows different shades of background
: Syntax on "Grammar check, color display
: Syntax off "close grammar check

Now that we know what we have done, we can write our own VIM operating environment.
The overall vim set value is usually placed in the/ETC/VIMRC this file, do not recommend that everyone to modify him. We create a. vimrc file in our home directory, where we can write our own content.

[Email protected] ~]# vim ~/The . VIMRC content is as followsSetHlsearch"High Brightness anti-whiteSetBackspace=2 "can be deleted at any time with backspace keySetAutoindent"Auto IndentSetRuler"the status of the last row can be displayedSetShowmode"The state of the line in the lower left cornerSetNu"line numbers can be displayed at the front of each lineSetBg=dark"show different shades of backgroundSyntax on"Perform a grammar check, color display"double quotation marks for this file (") Represents a comment

Description

After you save the VIM, you will have your own VIM operating environment the next time you use Vim.
As a reminder, each line in this file is preceded by a ":" Effect.

VI and VIM editor use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.