Introduction to Linux file viewing/editing methods

Source: Internet
Author: User

Cat Command Introduction

The original meaning of the cat command is connection (concatenate), which is used to connect multiple file contents and output to the standard output stream (the standard output stream defaults to the screen). In practice, we often use it to display the contents of a file. If you are familiar with the type command under MS-DOS, it is not difficult to master the cat command. A common example of this command is as follows:

Cat File1.txt Displays the contents of the File1.txt file;

Cat File1.txt File2.txt Displays the contents of the file1.txt and file2.txt files;

Cat-n File1.txt Displays the contents of the File1.txt file while displaying the line number;

VI Command Introduction

VI is the abbreviation of "Visual Interface", its status on Linux as if the edit program on DOS. It can perform many text operations, such as output, delete, find, replace, block operations, and so on, and users can customize them according to their own needs, which is not available in other editing programs.

VI is not a typesetting program, it is not like word or WPS can be the font, format, paragraph and other properties of the orchestration, it is just a text editing program.

VI There are no menus, only commands, and commands are numerous. Confined to space, this article only describes commonly used commands.

Vi has three basic modes of operation: command line mode, text input mode and last line mode.

Command-line mode:

At any time, no matter what mode the user is in, just click the "ESC" key, you can get VI into the command line mode, when the VI command in the shell environment to start the VI editor, is also in this mode.

In this mode, users can enter a variety of legal VI commands for managing their own documents. Any character entered from the keyboard is interpreted as an edit command, and if the input character is a valid VI command, VI completes the corresponding action after accepting the user command (but note that the command entered is not displayed on the screen). If the input character is not a legal command of VI, VI will ring the alarm.

Text input mode:

Enter the input command in command mode i, attach command A, open command O, modify command C, replace command R or Replace command s can enter the text input mode. In this mode, any characters entered by the user are saved by VI as the contents of the file and displayed on the screen. In the text input process, if you want to return to the command mode, press the "ESC" key.

Last-line mode:

In command mode, the user presses the ":" Key to enter the last row mode, at which point VI displays a ":" As a prompt in the final row of the display window (usually the last line of the screen), waiting for the user to enter a command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to a file) after the last command is executed, VI automatically returns to the command mode.

If you change your mind when you enter commands in the last line mode, you can press the "ESC" key or use BACKSPACE to delete all the commands you have entered, then click Backspace to make VI return to command mode.

VI's entry and exit

In shell mode, type VI and the file name you want to edit to enter VI. For example:

VI Example.txt

You can edit the Example.txt file. If the file exists, the content of the file is displayed in the editing interface and the cursor is positioned on the first line of the file, and if the file does not exist, there is nothing in the editing interface. If you need to put your cursor in the nth line of the file after entering the VI editing interface, add the "+n" parameter after the VI command. For example, to start at line 5th of the Example.txt file, use the following command:

VI +5 example.txt

When you exit VI, you need to enter the exit command "Q" in the last line mode. If in the text input mode, first press "ESC" key to enter the command mode, and then enter ":" Into the last line mode in the last line mode, you can use the following Exit command:

: Q exits directly. If you modify the contents of the document in text input mode, you cannot exit.

: Wq exit after saving.
: X with "Wq".

: q! -Do not save content and force exit.

Display line numbers in VI

In the last line mode, enter the following command.

Set number

Enables VI to display line numbers in the editing interface.

In the last row mode, you can use the following "Nu" command (abbreviated with number) to display the line numbers of the line where the cursor is located and the contents of the line.

Cursor Movement action

In a full-screen text editor, the movement of the cursor is undoubtedly the most frequently used operation. Users can only skillfully use these commands to move the cursor to quickly and accurately edit at the desired location.

The cursor movement in VI can be either in command mode or in text input mode, but the methods of operation vary.

In text input mode, the cursor can be moved directly using the four arrow keys on the keyboard, and there are many ways to move the cursor in command mode. Not only can use four arrow keys to move the cursor, you can also use the H, J, K, l these four keys instead of four arrow keys to move the cursor, this can avoid the different keyboard definitions of the various machine contradictions, and the use of the hand can not leave the letter keyboard position can complete all operations, thereby improving work efficiency.

The following command completes the cursor movement in command-line mode:

h The cursor moves left, and if you enter the number n before pressing the H command, the cursor shifts n characters to the left;

L MOVE the cursor right, if you enter the number n before pressing the L command, the cursor shifts n characters to the right;

J cursor up, if you enter a number n before pressing the J command, the cursor moves up n characters;

K cursor down, if you enter a number n before pressing the K command, the cursor xia n characters;

0 (0) Move the cursor to the beginning of the line;

$ cursor moves to the end of the line;

H cursor moves to the first line displayed on the screen (not necessarily the file header);

The cursor moves to the last line displayed on the screen (not necessarily the end of the file);

The M cursor moves to the middle line of the screen;

NG cursor moves to nth row;

W or W moves the cursor right to the beginning of the next word;

E or E If the starting position of the cursor is within a word (that is, not at the end of the word), the command will move the cursor to the end of the word, or the command will move the cursor to the ending of the next word if the starting position of the cursor is at the bottom of the word.

B or B if the cursor is within the word (that is, not the first word), the command will move the cursor to the beginning of the word, and if the cursor is at the beginning of the word, the command will move the cursor to the beginning of the previous word;

The ctrl+g Status command displays the status of the currently edited document. Includes the file name being edited, whether it has been modified, the current line number, the number of lines in the file, and the percentage of the line preceding the cursor.

In addition, you can also complete the move of the cursor throughout the file through the following command in the last line mode:

: n the cursor moves to the nth row of the file;

: $ cursor moves to the last line of the file;

Text Insert operation

In command mode, any character entered by the user is interpreted by VI as a command, and if the user wants to use the input character as a textual content, the work mode of VI should first be switched from the command mode to the text input mode. The way to switch is to use the following command:

1. Insert command

VI provides two INSERT commands: I and I.

(1). The I command inserts the text starting before the cursor position, and you can use the key to delete the wrong input during the insertion. At this point VI is in the Insert State, the screen is the most downward display "--insert--" inserted words.

(2). I command This command to move the cursor to the beginning of the current line and then insert the text before it.

2. Attach (Append) command

VI provides two additional insert commands: A and A.

(1). A command is used to append new text after the cursor's current position, the newly entered text is placed after the cursor, and the original text after the cursor moves backwards, and the cursor can be anywhere on a line.

(2). A command unlike the a command, the a command moves the cursor to the end of the line where the row is located, starting from there to insert new text, and when you enter a command light, the label automatically moves to the end of the line.

The A and a commands are the only way to insert text into the end of a line.

3. Turn on the Open command

Whether it's an insert command or a append command, the inserted content starts at a location in the current line. If we want to insert some new rows before or after a row, you should use the Open command.

VI provides two open commands: O and O.

(1). o Command This command will open a new line below the line where the cursor is located, and place the cursor at the beginning of the line, waiting for the input text. Note that when you use delete characters, you can only delete characters from the beginning of the insertion mode, which does not work for previous characters.

(2). o command in contrast to the O command, the o command inserts a row above the line where the cursor is located and places the cursor at the beginning of the line, waiting for the input text.

Text modification Operations

In command mode, you can use the various commands provided by VI to modify text, including deletion, copying, substitution, and substitution of text content.

1. Text Delete/move

When editing text, it is often necessary to delete some unwanted text, we can use the key to delete the wrong or unwanted text, but there is a limit is when you delete the wardrobe, and then want to delete the contents of the above line is not possible.

In command mode, VI provides many of these commands for deleting commands. Most of it starts with D. Commonly used are:

(1). Delete a single character

X Delete the character at the cursor. If you precede x with a number n, delete the n characters to the right from where the cursor is located.

X Delete the character in front of the cursor, and if you precede x with a number n, delete the n characters that begin at the left of the character preceding the cursor.

Obviously these two commands are a quick way to remove fewer characters.

(2). Delete multiple characters

DD deletes the entire line where the cursor is located. You can add a number n before DD to delete the contents of the current row and its n-1 rows.

The D or d$ two command function, is to delete the content from the beginning of the cursor to the end of the line.

D0 deletes the contents from the beginning of a character to the beginning of the cursor.

DW Deletes a word. If the cursor is in the middle of a word, it is deleted from the cursor position to the ending. As with the DD command, you can add a number n before the DW. Represents the deletion of n specified words.

If the user accidentally deleted the operation, it does not matter VI, provides the command to recover the misoperation, and can move the recovered content, placed in the text anywhere. The RESTORE command uses "NP", where n is the number of times to recover. For example, by using the DD command to delete a line of content and then using the "2p" command, the deleted content is reinserted two times.

With the use of DD command and P command, it is convenient to implement the movement of text lines.

2. Text Copy command

yy copies the entire line where the cursor is located. You can add a number n before yy to copy the contents of the current line and its subsequent n-1 rows.

After the text line is copied, you can paste the text line anywhere by using the "P" command described above.

The DD, YY, and P commands function in VI, similar to the cut, copy, and Paste commands for graphics editing programs in Windows.

3. Cancel the previous command (Undo)

Canceling the previous command (Undo), also known as the Undo command, is a useful command that can cancel the effect of a previous misoperation or inappropriate operation on a file and revert it to the state before such a misoperation or inappropriate operation was performed.

Cancel the previous command in two forms, type the characters in command mode U and u their function is to cancel the command just entered, revert to the original situation. The lowercase u and upper case u differ in detail, the difference being that the function of the uppercase U command is to revert to the situation before the Misoperation command, that is, if you use the U command after inserting the command, delete the content just inserted, and if you use the U command after you delete the command, it is equivalent to inserting the content you just deleted at the cursor Here, all the commands that modify the text are treated as insert commands, that is, the U command can only cancel the previous operation, if you undo the previous operation with the U command, when you press the U key again, it is not to undo the previous step, but to undo the action of the U command, that is, to revert to the state before the first U command As a result, nothing has been done. The function of the lowercase u command is to restore the current row to the state before it was edited, regardless of how many times the line was edited.

4. Repeat command (Redo)

Repeating commands is also a very common command. In text editing, you often encounter the need to mechanically repeat some operations, you need to use a duplicate command. It allows the user to easily perform one more complex command that was just completed earlier.

The repeat command works only in command mode, and pressing the "." Key in this mode is available. When a repeating command is executed, the result is dependent on the current position of the cursor.

Text Search and replace operations

When editing a text, you can search for or locate a specific word or part of a word. The VI editor has the ability to search forward or backward for a specified pattern.

"/" is the VI Editor's Mode search command. When you type "/", a row appears at the bottom of the screen, and a ramp appears at the beginning of the bottom row, and the cursor is placed behind the ramp. At this point, you can type the mode you want to search at the cursor position. When the input is complete and the ENTER key is pressed, the editor begins to search the end of the file for the mode you typed from the location where the cursor is located. If the input mode is found, the cursor will remain in the found mode. After finding a matching pattern, if you still need to search for the next matching pattern, use the command "n" and use the "n" command if you need to find the last matching pattern.

In addition, the "?" command is also a pattern search command. The difference between "/" and "/" is from the cursor when
Search at the end of the file, while the "?" command searches the file header at the current location of your cursor.

The text substitution operation requires the use of the last line mode, i.e. all text substitution operations commands begin with ":".

The format of the text substitution command is:

: S/TEXT1/TEXT2 is used to replace the first "Text1" that the paragraph in which the cursor is located with "Text2";

: s/text1/text2/g is used to replace all "Text1" of the paragraph in which the cursor is located with "Text2";

: The m,ns/text1/text2/g is used to replace all searched "Text1" from the start of the M line to the end of the N line with Text2. Where you can use $ to represent the last row, or "1,$", to replace all eligible characters in the document.

Reprint please indicate the article source: "https://www.centos.bz/2011/10/linux-file-view-edit/"

Introduction to Linux file viewing/editing methods

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.