2017-12-19linux Basic Knowledge VI Editor

Source: Internet
Author: User
Tags exit in line editor

we described the regular expressions (BRE, ERE) in the previous section, with their two regular expression metacharacters written in a somewhat different way, and we also talked about the relationship between grep and Egrep and Fgrep and how to use it, based on the "PATTERN" specified by the user To filter the text by line-by-row matching, output the matched rows, and then introduce the text-viewing processing tools, such as WC, cut, Sort, Uniq, diff, Patch. So let's talk about the Vim editor in this chapter.

One, text editor

VI Editor is a plain text editor, so-called plain text is not any modified text information, and our web pages and office software is different, the latter is a text markup language, such as. html and. Xml can be decorated, can change the font size, underline and change the color of the font, etc. This allows you to keep the format until the next time you open it.
The plain text editor does not have a markup language. This is simply reserved character information, even if the font size change is also in a special way, it selects a field to change the font size also change all fields (for example: Windows Notepad).

1.1 Text editor kinds

In Linux, there are two types of text editors, one is the line editor, the other is the full-screen editor, the row editor represents SED, and in the full-screen editor, one is the Nano, the other is VI, and VI means (vistual Interface) Called the visual interface, there is an enhanced version of VI, which we call VIM (VI improved).

Second, VIM editor

If not illustrated, here Vi/vim is universal, Vim is a modal editor, each mode function is not the same, in general, its basic common pattern has the following:

Edit mode, command mode (copy, delete, etc.); input mode: Edit the text based on the original text and modify the input; last line mode: Built-in command line interface;

2.1 Open File

The Vim editor is opened in the same way as the previous nano, if the file is created automatically when there is no file on the local or specified path, and the information is retained in this file after editing, the basic usage is as follows:

# vim [OPTIONS] [FILE ...]

When a file is opened, the cursor defaults to the first column in the first row in the upper-left corner, and we can specify the cursor with an option to position the beginning of the line where the cursor is positioned, with the following options:

+#: After opening the file, the cursor is directly at the beginning of line #; +/pattern: Once the file is opened, the cursor is positioned directly at the beginning of the line where the PATTERN matches the first one;

   then when the file is opened, it is in edit mode by default, and if we want to modify the file, we need to switch from edit mode to input mode. There are several ways to provide choices:

    edit mode: default mode      edit mode  -->  input mode:         i:insert,  Enter;        a:append,  at the cursor position Enter at the rear of the cursor;         o: Inserts a new line below the cursor location;         i: Enter at the beginning of the line where the cursor is located;         a: Enter the line at the end of the line where the cursor is located;         o: Opens a new line above the cursor location; 

When you go back to edit mode from input mode, you can return to edit mode using the ESC key.

Input mode--edit mode ESC

When we convert from edit mode to last line mode, we use the ":" to perform the conversion.

Edit Mode---Last-line mode: (This is a built-in command-line interface)

If you go back to edit mode in the last line mode, you also need to use the ESC key to convert, and if the last line mode has a command, you need to double-press the ESC key two times.

Last-line mode--edit mode ESC

2.2 Closing Files

When we know the above patterns, then now know how to close the file, in edit mode using the shortcut key ZZ can be saved and exited, or in the last line mode to exit, there are many ways to exit in the last row mode.

: Q exit;: q! Forced exit: Wq save and exit;: W:q: x Save and exit;: W/path/to/somefile can understand Save as

2.3 Cursor Jump

The cursor jump can actually be understood as the page of the Man Manual, or as a jump between words, depending on the jump range, we can be broadly divided into the following:

    cursor jump:         between characters jump:         h: Left         j: Lower          k:         l: Right                  # command: Jumps the number of characters specified by #;              between words jump:         W: The first word of the next word;         e: The ending of the current or subsequent word;         b: The first word of a current or previous word;                 # comand: Jumps the number of words specified by #;             End of Line jump:         ^: Jumps to the first non-whitespace character at the beginning of a row;         0: Jump to the beginning of the line;         $: Jump to end of line;              Jump:         #G: Jump To line specified by #;         1g, gg: Jumps to the first line;         g: last line;              inter-sentence jump:        )           (              Inter-section jump:         }        {

2.4 Turning Screen

We are in the man command, if you want to turn to the next screen, to use the F key, flip up a screen to use the B key, but in vim, the flip screen operation than the man command is different:

Flip screen: ctrl+f: Flip a screen to the end of the file, Ctrl+b: Flip a screen to the file header, Ctrl+d: Half screen to the end of the file, Ctrl+u: Half screen to the file header; Enter: Turn backward by line;

Iii. Vim's editing commands

With Vim's edit command, we can perform a large-scale operation on the file, and the editing commands usually include the following editing operations:

    character editing:     x: Remove the character;     the cursor at the #x: remove the # characters from the beginning of the cursor;         XP: Swap the character where the cursor is located with the character position behind it         Replace command (replace):     r: Replaces the character at which the cursor is located;        rchar             Delete command:     d: Delete command, can be combined with the cursor jump character, Implement range deletion;         d$: Deletes content from the current character to the end of the line;         d^: Deletes the contents from the current character to the beginning of the line;         DW: Deletes the first word from the current character to the next word;         de: Removes in from the current character to the next word;         DB: The first word of the current or previous word;            # command         DD: Delete the line where the cursor is currently;              #dd: Delete the line from where the cursor is locatedStart Total # line;                Paste command (p,  Put, patse):     p: The contents of the buffer are pasted below the line where the current cursor is located if the entire row is the same, or paste to the rear of the current cursor;     p: The contents of the buffer are pasted at the top of the line where the current cursor is located, if it is an entire row, or paste to the front;        copy (yank,y) where the current cursor is located:     y: Copy, working behavior similar to D command;    y^:    y0:         ye:    yw:    ya:          #COMMAND         yy: Copy a whole line           #yy: Copy # line;

3.1 Changing the command

Changing the command function is the ability to convert from edit mode to input mode, because changing what is required is the user's intervention, and secondly it can also implement the delete operation.

Changing command (change, c) Edit mode--input mode for delete operation; c $ c^ C0 CB CE CW #COMMAND cc: Delete the line where the cursor is located and convert to output mode; #cc:

Iv. Other editing operations

The above is the operation of the line, in Windows, click the left mouse button we can cross a field to do the editing operation, in the Vim editor can also be implemented, we can call it visualization mode.

Visual Mode: V: selected by character; V: selected by row, combined with edit command: D, c, y

Next is the undo operation, if the file we edit the error or delete the error, you can undo the operation to restore, it should be noted that if the Save and exit, then undo the original point.

Undo Action: U: Undo previous operation; #u: Undo Previous # Operations

If we undo the error, we can undo the previously undone operation, or we can call it a forward restore.

Undo previous undo Operation: Ctrl+r

If an operation can be performed more than once, the previous edit operation can also be performed repeatedly:

Repeat the previous edit operation:.

If you also want to learn more about Vim, in Linux with the Vim tutorial, the command for this tutorial is: Vimtutor

2017-12-19linux Basic Knowledge VI Editor

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.