CentOS learning notes-vi program editor, centos learning notes-vi

Source: Internet
Author: User

CentOS learning notes-vi program editor, centos learning notes-vi
Vi program editor

In Linux, you often need to modify some configuration files. In this case, you need an editor. Almost all Linux versions provide the vi editor.

File Content Check cat command

What should we do if we want to check the content of a file? There are quite a few interesting commands to share: the most commonly used commands for displaying file content can be said to be cat, more, and less! In addition, if we want to view a very large file (several hundred MB), but we only need a few lines of words at the backend, what should we do? Haha! Use tail. In addition, the tac command can also be used! Okay. Let's talk about the purpose of each command!

  • Cat displays the file content starting with the first line
  • The tac is displayed from the last line. We can see that the tac is written in cat!
  • When nl is displayed, the line number is output!
  • More one-page display of File Content
  • Less is similar to more, but it is better than more. He can flip the page forward!
  • Head only looks at the first few rows
  • Tail only looks at its tail.
  • Od reads File Content in binary mode!
Vi program editor

Basically, vi is divided into three modes: "General mode", "edit mode", and "command column command mode 』. The functions of these three modes are as follows:

  • General mode:
    Open an archive in vi and enter the normal mode (this is the default mode ). In this mode, you can use the top, bottom, and left buttons to move the cursor. You can use the delete character 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 you cannot edit the file content! It takes you to press "I, I, o, O, a, A, r, R" and other letters to enter the editing mode. Note! In Linux, when you press these buttons, "INSERT or REPLACE" appears in the lower left corner of the screen before editing. To return to normal mode, you must press the "Esc" button to exit the editing mode.
  • Command column command mode:
    In normal mode, enter 『:/? You can move the cursor to the bottom line. In this mode, you can search for information, while reading, saving, replacing a large number of characters, leaving vi, displaying a row number, and so on are achieved in this mode!

Simply put, we can think of the three modes as the icons below:

Note the figure above, you will find that the general mode can be switched to the edit mode and the command column mode, but the edit mode and the command column mode cannot be switched to each other! This is very important!

  • Use vi to enter the General mode;
[root@www ~]# vi test.txt
Enter the "vi file name" to enter the General vi Mode. Note that you must add a file name after vi, regardless of whether the file name exists or not! The entire screen is divided into two parts. The upper half and the bottom row can be regarded as independent.
  • Press I to enter the editing mode and start editing the text
  

In normal mode, you only need to press the I, o, a and other characters to enter the editing mode! In the editing mode, you can find that the "-INSERT-" text appears in the status bar in the lower left corner. That is, you can enter any characters! At this time, except for the [Esc] button on the keyboard, other buttons can be regarded as general input buttons, so you can edit any of them!

  • Press the [ESC] button to return to normal mode.
  

Okay. Now that I have edited the style above, how can I exit? Yes! That's right! Just press the [Esc] button for him! Immediately you will find that the INSERT-in the lower left corner of the screen is missing!

  • In normal mode, press: wq to save and exit vi
  

OK. We have to archive the file. The command for saving and leaving the disk is simple. Enter ": wq" to archive and exit! (Note: Press: The cursor will move to the bottom line !) Enter "ls-l" after the prompt character to see the test.txt file we just created! The entire figure is a bit like below:

In this way, your file test.txt has been created! Note that if your archive permission is incorrect, such as-r --, you may not be able to write data. Can you use "Force write? Yes! Use ": wq! "Add an exclamation point! But pay special attention to it! That can be set up only when your permissions can be changed!

  • Part 1: Description of buttons available in general mode, move the cursor, copy and paste, and replace search
How to move the cursor
H or the left arrow (←) Move a character to the left
J or the down arrow (arrow) Move the cursor down a character
K or the up arrow (arrow) Move the cursor one character up
L or right arrow (→) Move the cursor one character to the right
If you place your right hand on the keyboard, you will find that hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, move 30 rows down, you can use a combination of "30j" or "30 Gbit/s", that is, after adding the number of times (number) You want to move, press the action!
[Ctrl] + [f] The screen moves one Page Down, which is equivalent to a [Page Down] button (commonly used)
[Ctrl] + [B] The screen moves one Page Up, which is equivalent to a [Page Up] button (commonly used)
[Ctrl] + [d] Move half of the screen down
[Ctrl] + [u] Move half of the screen "up"
+ Move the cursor to the next column with a non-space character
- Move the cursor to the previous column of a non-space character
N <space> That n represents a number, for example, 20. Press the number and then press the Space key. The cursor will move the n characters in the line to the right. For example, if the value is 20 <space>, the cursor moves the distance of 20 characters to the backend.
0 or function key [Home] This is the number "0": Move to the top character of this line (commonly used)
$ Or function key [End] Move to the last character of this line (commonly used)
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 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 (frequently used)
NG N is a number. The Nth row of the file. For example, 20 GB will be moved to the 20th rows of this file (can be used with: set nu)
Gg Moving to the first line of this file is equivalent to 1 GB! (Common)
N <Enter> N is a number. Move the cursor down n rows (commonly used)

Search and replace

/Word Search for a string named word under the cursor. For example, to search for the vbird string in the file, enter/vbird! (Common)
? Word Search for a string named word over the cursor.
N This n is an English button. 『Repeat the previous search action』. For example, if we just run/vbird to search for the vbird string, press n to continue searching for the next string named vbird. If it is execution? Press n to search for a string named vbird!
N This N is an English button. Opposite to n, This is the previous search action for "reverse. For example, after/vbird, press N to search for vbird "up.
It is very helpful to use/word with n and N! You can repeat to find some keywords you are searching!
: N1, n2s/word1/word2/g N1 and n2 are numbers. Search for the word1 string between line n1 and line n2 and replace it with word2! For example, if you search for vbird between 100 and 200 and replace it with VBIRD:
": 100,200 s/vbird/VBIRD/g 』. (Common)
: 1, $ s/word1/word2/g Search for the word1 string from the first row to the last row and replace it with word2! (Common)
: 1, $ s/word1/word2/gc Search for the word1 string from the first row to the last row and replace it with word2! The prompt characters are displayed before replacement to confirm (confirm) whether to replace! (Common)
Delete, copy, and attach
X, X In a row, x deletes a character (equivalent to a [del] Key), and X deletes a character (equivalent to a [backspace] or a backspace key) (commonly used)
Nx N is a number and n characters are deleted consecutively. For example, I want to delete 10 characters in a row, "10x 』.
Dd Delete the entire column of the cursor (commonly used)
Ndd N is a number. Delete the n-down column where the cursor is located. For example, 20 dd deletes 20 columns (commonly used)
D1G Delete all data from the cursor to the first row
DG Delete all data from the cursor to the last row
D $ Delete the cursor to the last character of the row
D0 That is the number 0, where the cursor is deleted, to the beginning of the row.
Yy Copy the row of the cursor (commonly used)
Nyy N is a number. Copy the n-down column where the cursor is located. For example, if 20yy is used, 20 columns are copied (commonly used)
Y1G Copy all data from the cursor column to the first column
YG Copy all data from the column where the cursor is located to the last column
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, P P: to paste the copied data to the next row of the cursor, P is to paste the row on the cursor! For example, I have already copied 10 rows of data with the cursor in 20th rows. After you press p, the 10 rows of data will be pasted after the original 20 rows, that is, from 21 rows. But what if I press P? Then the original 20th rows will be pushed to 30 rows. (Common)
J Combine the cursor column with the data in the next column into the same column
C Multiple data records are deleted repeatedly, for example, 10 rows are deleted down and [10cj]
U Restore the previous action. (Common)
[Ctrl] + r Redo the previous action. (Common)
This u and [Ctrl] + r are very common commands! One is restoration, the other is redo once ~ With these two buttons, you can edit them! Happy!
. Don't doubt! This is the decimal point! It means to repeat the previous action. If you want to repeat the deletion, repeat, and so on, just press the decimal point! (Common)
  • Part 2: Description of available buttons for switching from normal mode to edit mode
Enter the insert or replace edit mode
I, I Insert mode ):
I is "insert from the current cursor", I is "insert from the first non-space character in the current row 』. (Common)
A, Insert mode ):
A is "insert from the next character of the current cursor", and A is "insert from the last character of the row where the cursor is located 』. (Common)
O, O Insert mode ):
This is the case of the English letter o. O is to insert a new row in the next row where the current cursor is located. O is to insert a new row in the previous row where the current cursor is located! (Common)
R, R Enter Replace mode ):
R will replace the character where the cursor is located only once; R will replace the text where the cursor is located until ESC is pressed; (commonly used)
In the buttons above, the words "-- INSERT --" or "-- REPLACE --" will appear in the lower left corner of the vi image. Let's know the action by name !! Note that we have mentioned above that when you want to input characters in the file, you must see INSERT or REPLACE in the lower left corner before entering the characters!
[Esc] Exit edit mode and return to normal mode (commonly used)
  • Part 3: Description of the buttons available for switching from general mode to command column Mode
Commands for storing and removing command Columns
: W Write edited data to Hard Disk Files (frequently used)
: W! If the file attribute is "read-only", the file is forcibly written. However, whether the file can be written is related to the permission on the file!
: Q Exit vi (commonly used)
: Q! If you have modified an archive and do not want to store it, use it! To force exit without storing files.
Note that the exclamation point (!) In vi, it often means "forced ~
: Wq Save and exit. If it is: wq! It is forced to save and leave (commonly used)
ZZ This is an uppercase Z! If the file is not changed, it will not be stored and left. If the file has been changed, it will be stored and left!
: W [filename] Save edited data to another file (similar to storing new files)
: R [filename] Read the data of another archive in the edited data. Add the file "filename" to the end of the row where the cursor is located.
: N1, n2 w [filename] Store content from n1 to n2 as filename.
:! Command The result of Executing command in command column mode is displayed when vi is temporarily removed! For example
『:! Ls/home> to view the ls output file information under/home in vi!
Vim Environment Change
: Set nu Displays the row number. After the row is set, the row number is displayed in the prefix of each row.
: Set nonu Opposite to set nu, the row number is canceled!
Note that in vi, "Numbers" are meaningful! A number usually indicates repeated times! It may also mean what to go. For example, to delete 50 rows, use "50dd", right! Add the number before the action ~ What if I want to move 20 rows down? That is, "20j" or "20 Jun.

    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.