CentOS Learning Note--VI Program Editor

Source: Internet
Author: User

VI Program Editor

Linux often needs to modify some of the configuration files, then need an editor, almost all of the Linux version provided VI this editor.

File contents for Cat commands

What if we want to check the contents of a file? There are quite a few interesting commands to share: The most commonly used commands to display the contents of a file are cat and more and less! Also, if we want to look at a very large file (hundreds of MB), but we only need a few lines back-end, then what is the good? Oh! With tail ah, in addition, TAC This command can also be achieved! All right, let's talk about the use of each command!

    • Cat starts displaying file contents from the first line
    • The TAC begins with the last line, and you can see that the TAC is backwards written by cat!
    • NL display, the output line number!
    • More page-by-page display file contents
    • Less is similar to more, but better than more, he can page forward!
    • Head's just a few lines.
    • Tail only look at the tail a few lines
    • OD reads the contents of the file in binary way!
VI Program Editor

Basically, vi is divided into three modes, namely, "General Mode", "edit mode" and "command-order Mode". The three modes of action are:

    • General mode:
      Open a file with VI directly into the general mode (this is the default mode). In this mode, you can use the "up or down" button to move the cursor, you can use "delete character" or "Delete whole line" to process the contents of the file, you can also use "copy, paste" to process your data.
    • Edit mode:
      In general mode can be deleted, copied, pasted and so on, but can't edit the contents of the file! Wait until you press "I, I, O, O, A, a, R, R," and any one of the letters before entering edit mode. Watch out! Usually in Linux, when you press these keys, the words "INSERT or REPLACE" appear at the bottom left of the screen, so you can edit them. If you want to return to normal mode, you must press the "ESC" button to exit edit mode.
    • Instruction-column Command mode:
      In the general mode, enter ":/? "Any one of the three buttons, you can move the cursor to the bottom row." In this mode, you can provide "search data" action, and read, save, a large number of substitution characters, leave VI, display line number and so on the action is achieved in this mode!

To put it simply, we can think of these three patterns as the icons below to indicate:

Notice the above illustration, you will find that the general mode can be changed with the editing mode and the command column mode, but the editing mode and the command column mode is not interchangeable with each other Oh! This is very important!

    • Use VI to enter the general mode;
[[Email protected] ~] # VI test.txt
The direct input "VI file name" can enter the general mode of VI. Please note that you must add a file name after VI, regardless of the file name exists or not! The whole picture is divided into two parts, the upper half and the bottom line can be regarded as independent.
    • Press I to enter edit mode to start editing text

In the general mode, just press I, O, a and other characters can enter the editing mode! In edit mode, you can find the –insert-in the status bar of the lower left corner, that is, you can enter any character hint! At this time, the keyboard except for [ESC] This button, the other keys can be regarded as the general input button, so you can make any edits!

    • Press the [ESC] button back to normal mode

Well, assuming I've given him an edit in the style above, how do I get out? Yes! That's right! Just press the [ESC] button for him! Immediately you will find the –insert– in the lower left corner of the screen is missing!

    • In general mode, press: Wq after storage leave VI

OK, we want to archive, save and leave the command is very simple, enter ": Wq" can archive away! (Notice, press: The cursor will move to the bottom line!) You can then enter "Ls-l" after the hint character to see the test.txt file we just created! The whole diagram looks something like this:

As a result, your file Test.txt has been set up! It is important to note that if your file permissions are not correct, for example,-r--r--r--, then you may not be able to write, can you use the "force write" method at this time? OK! Use ": wq! "Add an exclamation point!" However, you need to pay special attention to Yo! That is in the "your permission can change" situation can be set up!

General mode available button description, cursor movement, copy paste, search substitution, etc.
How to move the cursor
h or LEFT ARROW key (←) The cursor moves one character to the left
J or DOWN ARROW key (↓) Move the cursor down one character
K or Up ARROW key (↑) Move the cursor up one character
L or right ARROW key (→) The cursor moves one character to the right
If you put your right hand on the keyboard, you will find that the HJKL are arranged together, so you can use the four buttons to move the cursor. If you want to move multiple times, such as moving down 30 lines, you can use the "30j" or "30↓" combination of keys, that is, plus the number of times you want to do (number), press the action!
[Ctrl] + [f] Screen "Down" to move one page, equivalent to [PAGE DOWN] key (common)
[Ctrl] + [b] Screen "Up" to move one page, equivalent to [PAGE UP] key (common)
[Ctrl] + [d] Screen "Down" to move half a page
[Ctrl] + [d] Screen "Down" to move half a page
[Ctrl] + [u] Screen "Up" move half page
+ Cursor moves to the next column of non-whitespace
- Cursor moves to the previous column of non-whitespace
N<space> That n means "number", for example 20. When you press the number and then press the SPACEBAR, the cursor moves the n characters of the line to the right. For example 20<space>, the cursor moves 20 character distances to the back.
0 or function key [Home] This is the number "0": Move to the front of the line prompt (common)
$ or Function key [End] Move to the last face of this line prompt (common)
H The first character of the line that the cursor moves to the top of the screen
M The first character of the line that the cursor moves to the center of the screen
L The first character of the line that the cursor moves to the bottom of the screen
G Move to the last line of this file (common)
NG N is a number. Move to the nth row of this file. For example 20G moves to line 20th of this file (mates: Set Nu)
Gg Move to the first line of this file, equivalent to 1G Ah! Common
N<enter> N is a number. Move the cursor down n rows (common)
Search and replace
/word Look under the cursor for a string called Word. For example, in the file search Vbird This string, you can enter/vbird! Common
? word Look for a string with the string name word on top of the cursor.
N This n is the English key. Represents " repeating the previous search action ". For example, if we have just executed/vbird to search for Vbird this string, then pressing N will continue to search down the next string named Vbird. If the Vbird is executed, then pressing N will continue to search for the string named Vbird!
N This N is the English key. In contrast to N, the previous search action for "reverse". For example/vbird, pressing N means "up" to search for Vbird.
Using/word with N and n is extremely helpful! Can let you repeat to find some of the keywords you search!
: n1,n2s/word1/word2/g

N1 and N2 are numbers. Look for the string word1 between N1 and N2, and replace the string with Word2! For example, search for vbird between 100 and 200 rows and replace it with Vbird:
": 100,200s/vbird/vbird/g". Common

: 1, $s/word1/word2/gc Look for the word1 string from the first line to the last line and replace the string with Word2! and display the prompt character before the substitution to the user to confirm (confirm) whether need to replace! Common
Delete, copy and paste on
X, X In one line of words, X is the backward deletion of a character (equivalent to the [Del] key), and X is to delete a character (equivalent to [backspace], which is the backspace) (common)
Nx N is a number that continuously deletes n characters backwards. For example, I want to delete the 10 characters consecutively, "10x".
Dd Delete the entire column where the cursor is located (common)
Ndd N is a number. Delete the down n column where the cursor is located, for example, 20DD to delete 20 columns (common)
d1g

Remove all data from the first row of the cursor

Dg

Delete all data from the last row of the cursor

d$

Delete the last character of the row where the cursor is located

D0

That is 0 of the number, delete the cursor at the top of the line, and the first character

Yy

The row where the cursor is copied (common)

Nyy

N is a number. Copy the down n column where the cursor is located, for example, 20yy to copy 20 columns (common)

y1g

Copy all data from the column to the first column of the cursor

YG

Copy all data from the column to the last column of the cursor

Y0

Copy all data from the same character as the cursor to the beginning of the line

y$

Copy all data at the end of the line with the same character as the cursor

P, p

p to paste the copied data on the next line of the cursor, and p for a row on the cursor! For example, I currently have the cursor on line 20th and have copied 10 rows of data. When P is pressed, the 10 rows of data will be affixed to the original 20 lines, i.e. 21 lines. But what if we press P? Then the original 20th Guild was pushed into 30 rows. Common

J

Combine data from the cursor column and the next column into the same column

C

Repeatedly deleting multiple data, such as deleting 10 rows down, [10CJ]

U

Restores the previous action. Common

[Ctrl]+r

Redo the last action. Common

This u with [ctrl]+r is a very common instruction! One is recovery, the other is redo once ~ Use these two function keys, your editor, hey! Very happy!

.

Don't doubt it! This is the decimal point! To repeat the meaning of a previous action. If you want to repeat the deletion, repeat the paste, and so on, press the decimal point "." It's all right! Common

CentOS Learning Note--VI Program Editor

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.