Learn how to use vi/vim in Linux one day

Source: Internet
Author: User
Vi and vimvi editors are standard editors for all Unix and Linux systems. they are equivalent to notepad in windows systems. they are not as powerful as any latest text editors. It is an indispensable tool for Linux. For any version of Unix or Linux ,...

Vi and vim
The vi editor is a standard editor for all Unix and Linux systems. it is equivalent to notepad in windows systems. it is not as powerful as any latest text editor. It is an indispensable tool for Linux. The vi editor is identical for any version of Unix and Linux systems. once you learn about it, you will be able to access it freely in the Linux world.
Vim is capable of editing programs. it can identify the correctness of the syntax by Font color to facilitate programming;
Because the program is simple, the editing speed is quite fast.
Vim can be used as an upgraded version of vi. it can display special information in multiple colors.
Vim will automatically execute the syntax of the program based on the file extension or the starting information in the file to determine the content of the file, and then display the program code and general information in color.
Vim includes many additional functions, such as regular expression search, multi-file editing, and block replication. This is a great feature for us to modify some configuration files on Linux.


 

Why vi?
All Unix Like systems have built-in vi text editors, while other text editors do not necessarily exist;
Some software editing interfaces call vi (for example, crontab, mongodo, edquota, and other commands );

Vi usage
Basically, vi can be divided into three states: General mode, edit mode, and command line mode. the functions of each mode are as follows:

General mode:
Open a file in vi and enter the normal mode (this is the default mode ). In this mode, you can move the cursor by pressing the upper, lower, and lower buttons. you can delete characters or delete the entire line to process the file content, you can also use copy and paste to process your file data.

Edit mode:
In normal mode, you can delete, copy, and paste files, but cannot edit the file content. only when you press [I, I, o, O,, a, r, R] and other letters before entering the editing mode. At this time, INSERT or REPLACE appears in the lower left of the screen. To return to normal mode, you must press Esc to exit the editing mode.

Command line mode:
Enter 【:/? You can move the cursor to the bottom line. In this mode, actions such as searching, reading, saving, replacing characters, leaving vi, and displaying row numbers are completed in this mode!

Description of buttons available in normal mode
Move cursor
H, j, k, l, respectively, control the cursor to move the cursor to the left, down, up, and right
Press ctrl + B to move the screen to the back page
Press ctrl + f to move the screen to the front page

[N ] Move the cursor n characters to the right
[Home] move to the top character of this line: 0, but cannot use a number on the keypad
[End] move to the last character of this line: $. it seems that I cannot perform the test.
[W] jump the cursor to the beginning of the next word
[E] move the cursor to the end of the next word

[H] move the cursor to the first character in the line at the top of the screen
[M] move the cursor to the first character in the middle line of the screen
[L] move the cursor to the first character in the row at the bottom of the screen

[G] move to the last line of the file
[NG] moves to the nth line of the file (can be used with: set nu)
[Gg] move to the first line of the file, equivalent to 1G
[N ] Move the cursor down n rows

Search and replace
[/Word] looks down at the cursor for a string named word
【? Word: search for a string named word from the cursor up.
[N] indicates repeating the previous search action.
[N] is the opposite of n. It is the inverse action for the row.

[: N1, n2s/word1/word2/g] n1 and n2 are numbers. search for the string word1 between line n1 and line n2 and replace the string with word2.

[: 1, $ s/word1/word2/g] Searches for the word1 string from the first row to the last row, and replaces the string with word2.
[: 1, $ s/word1/word2/gc] Searches for the word1 string from the first row to the last row, replaces the string with word2, and prompts the user to confirm whether to replace the string before replacement.

Delete, copy, and paste

[X] deletes a character backward (equivalent to the [del] key)
[X] deletes a character (equivalent to the [backspace] Key) from the forward)
[Nx] deletes n consecutive characters backward.

[Dd] deletes the row where the cursor is located.
[Ndd] delete n rows down where the cursor is located
[D1G] deletes all data from the row where the cursor is located to the first row.
[DG] deletes all data from the cursor to the last row.

[D $] delete the cursor to the last character of the row
[D0] removes the cursor from the beginning of the row.

[Yy] copy the row where the cursor is located
[Nyy] copy the n column down where the cursor is located

[Y1G] copies all data from the row where the cursor is located to the first row.
[YG] copies all data from the row where the cursor is located to the last row.

[Y0] copy the character of the cursor to all data at the beginning of the row.
[Y $] copy all data from the character with the cursor to the end of the row.

[P] Paste the copied data in the next row of the cursor
[P] is the row that is attached to the cursor.

[U] restore the previous operation
Ctrl + r redo the previous operation.

[.] Repeats the previous operation.

Description of the buttons available for switching from normal mode to edit mode
[I, I] enters the Editing mode:
I is [insert from the current cursor]
I: [start to insert the first non-space character in the current row]
 
[A, A] Insert mode ):
A: [insert from the next character of the current cursor]
A is [insert from the last character in the row where the cursor is located]

[O, O] enters the Editing mode:
O is [insert a new row at the next row where the current cursor is located]
O inserts a new row into the previous row where the current cursor is located

[R, R] enters the replacement mode:
R will only replace the character where the cursor is located once
R will replace the text of the cursor until you press ESC;

[Esc] exit the editing mode and return to the normal mode.

Common mode switch to command line mode available button description
[: W] Save the edited content
[: W !] Write the file forcibly, but it is related to the permission on the file.
[: Q] leaving vi
[: Q !] Do not want to save the modification and force exit
[: Wq] save and exit
[: X] save and exit
[ZZ] If the file is not changed, it is not saved and left. if the file has been changed, it is saved and left.

[: W filename] Save the edited data to another file (similar to another file)
[: R filename] reads data from another file in the edited data. Add the content of the file filename to the end of the row where the cursor is located.

[: N1, n2 w filename] save the content from n1 to n2 as filename.
【:! Command: displays the result of executing command in command line mode! For example 【:! Ls/home: you can view the ls output file information under/home in vi!

[: Set nu] displays the row number.
[: Set nonu] is opposite to set nu.

Vim cache file, warning information during restoration and enabling
We know that some commonly used editing software has a recovery function, that is, when your system causes a situation similar to a computer for some reason, you can also use this restoration function to retrieve unsaved data. Our VIM also has this function.

When we use vim for editing, vim creates a file named. filename. swp in the directory of the edited file. If your system is disconnected for some reason and the file you edited has not been saved, at this time,. filenam. swp will be able to send a rescue function.

Let's demonstrate it.
Vim man. config
Ctrl + z put in the background for execution
We stopped the VI process.
One method
Ps aux | grep vi
Kill-9 n

Another method
Jobs
Kill-9% 1

Run the ls-l command to check the directory. a. man. config. swp file is displayed, which is a cached file.

Let's edit it again.
Vim man. config
At this time, some information will appear.
Question 1: someone else or program may be editing this file at the same time:
Problem 2: In the previous vim environment, vim interruption (crashed) may be caused by unknown reasons ):

There will be six command items in the lower right corner. Their functions are described as follows:
(O) pen Read-Only: open this file to become a Read-Only file, which can be used when you just want to view the file content and do not want to edit the file. In general, if you log on to a classmate's computer to view his configuration file during the lesson, you will find that you can use this mode when you are actually editing the file;
(E) dit anyway: open the file you want to edit in the normal way, and the content of the temporary storage disk will not be loaded. If two people are editing this file, it is easy to change the file of the other party.
(R) ecover: load the content of the temporary storage disk, which is used for the work that has not been saved before you can save it. However, you still need to manually delete the temporary archive after saving the archive and leaving vim.
(D) elete it: Are you sure that the temporary archive is useless! The temporary storage disk will be deleted before the file is enabled.
(Q) uit: Press q to exit vim without returning any action to the command prompt character.
(A) bort: ignore this editing behavior. it feels very similar to quit!

Vim functions
In fact, most of the Linux releases have replaced vi with vim. Why use vim? Because vim has the color display function, it also supports many program syntaxes and corresponding prompt information. Check whether your VI is replaced by VIM. you can use
Alias command to check whether there is a line alias vi = 'vim.

Block selection
[V] character selection, the cursor is reversed
[V] line selection, the line through the cursor will be reversed selection
[Ctrl + v] block selection, you can use a rectangle to select data (vertical column)
[Y] copy the reverse IP address
[D] delete the whitelist.

Multi-file editing
When using vim, you may encounter the need to copy a segment of a file to another file, but vim cannot keep this segment when it is closed. Or use other methods to copy.
[Vim file1 file2]

[: N] edit the next file
[: N] edit the previous file
[: Files] lists all files edited by vim.

Multi-window function
There are two files to be checked against
[: Sp filename] opens a new window. if filename is added, a new file is opened in the new window. Otherwise, the two windows are the same file (synchronously displayed ).

[Ctrl + w + j]
[Ctrl + w + ←] press ctrl + w to open all the buttons, and then press j (or the down arrow ), the cursor can be moved to the lower window.

[Ctrl + w + k]
[Ctrl + w + ←] Same as above, but move the cursor to the above window.

Vim environment settings and records (~ /. Vimrc ,~ /. Viminfo)
If we use vim software to find a character string inside a file, the string will be reversed, and the next time we edit this file with vim, the string to be searched for is still reversed. In addition, when we repeat and edit the same file, when we enter the file for the second time, the cursor starts with the line that we left last time. This feature may be convenient but inconvenient. How can this happen? This is because our vim will actively log on to the behavior you have performed. the file that records the action is :~ /. Viminfo. if you do not want to use this function, delete it directly ~ /. Viminfo. If you have used vim before, your home directory will have this file. This file is automatically generated. the actions you have performed in vim can be found in this file. If you are interested, you can view the content in the file by yourself.

However, different releases have different default environments for vim. For example, some versions do not have high-brightness anti-whitelist when keyword search is found, and some versions will take the initiative to help you scale down (the so-called scale-down, when you press Enter to edit a new line, the cursor is not at the beginning of the line, but alignment with the first non-space character of the previous line. In fact, these can be set by yourself. let's take a look at the vim environment settings.
There are many parameters set in the vim environment. if you want to know the current setting value, you can enter [: set all] in general mode for reference. because there are too many set projects, here we will only list some simple set values that are commonly used for your reference.

: Set all "display all current environment parameter settings
: Set hlsearch "high brightness anti-white (high brightness search)
: Set nohlsearch "clear high brightness anti-white (high brightness search)
: Set backspace = 2 "can be deleted at any time with the backspace key during editing (0 and 1 are only valid for the entered characters)
: Set autoindent "automatic contraction
: Set noautoindent "cancel automatic contraction
: Set ruler "to display the status of the last line
: Set showmode "the status of the row in the lower left corner
: Set nu "display row number
: Set nonu "cancel row number
: Set bg = dark "displays different background colors
: Syntax on "syntax check, color display
: Syntax off "disable syntax check

After learning about the above content, we can write our own vim operating environment.
The overall vim setting value is usually set in the/etc/vimrc file. We do not recommend you modify it. We create a. vimrc file in our home directory, where we can write our own content.
[Root @ yufei ~] # Vim ~ /. Vimrc
The content is as follows:
Set hlsearch "high brightness anti-White
Set backspace = 2 "can be deleted at any time with the backspace key
Set autoindent "automatic contraction
Set ruler "to display the status of the last row
Set showmode "the status of the row in the lower left corner
Set nu "displays the row number at the beginning of each row
Set bg = dark "displays different background colors
Syntax on "syntax check, color display
"Double quotation marks (") in this file indicate comments.

After saving and exiting vim, the next time you use vim, you will have your own vim operating environment.
Note that the effect of ":" added before each row in this file is the same.

Finally, I will give you a few good pictures to help you master vi/vim faster. Remember, don't stick to it, but focus on operations.


 

From: Yufei blog

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.