Linux Study Notes (4)-Use of the text editor vi and Study Notes vi

Source: Internet
Author: User

Linux Study Notes (4)-Use of the text editor vi and Study Notes vi

  • Three editing modes of vi
  • Command mode)

    In this mode, you can control the movement of the cursor, delete characters, delete rows, and copy and move a paragraph.

  • Insert mode)

    Only in this mode can you enter characters

  • Last line mode)

    In this mode, you can save the file or exit Vi, set the Editor Environment, search for strings, and list the row numbers.

    Note: however, vi is generally regarded as two modes, and the last line mode is also included in the command line mode.

  • Vi initial experience

    Type vi file.txt on the terminal

    # Vi file.txt

    If you are not familiar with it, you must start directly and press the keyboard for input. If the input is ineffective, you can start to worry about it .. In fact, when you enter the preceding command, it is in the command mode. To switch to the input mode, you only need to press the letter "I". Then you can enter it. to switch to the command mode, you only need to press the "ESC" key.

    Save files

    In command line mode, click ":" to enter the last line mode, for example:

    : W filename (save the file with filename as the file name)

    : Wq (save disk and release vi)

    : Q! (Force exit vi without saving the disk)

  • Command mode function key
  • Switch to input mode

    After you press "I" to enter the input mode, the input file starts from the current position.

    After you press "a" to enter the input mode, the text is entered from the next position where the current cursor is located.

    After you press "o" to enter the input mode, a new row is inserted and text is entered from the beginning of the line.

  • Switch from input mode to command mode

    Press "ESC"

  • Move cursor

    Vi can be directly moved up, down, left, and right with the cursor on the keyboard, but the regular vi uses lowercase letters h, j, k, and l to control the cursor's left, bottom, Up, And right shifting to one cell.

    Press "ctrl" + "B": screen to "back" to move one page

    Press "ctrl" + "f": Move the screen to "Front"

    Press "ctrl" + "u": Move the screen to "Rear" half page

    Press "ctrl" + "d": Move the screen to "Front" half page

    Press number 0: Move to the beginning of the article

    Move to the end of the article by G

    Move to the end of the row where the cursor is located by $

    ^: Move to the beginning of the row where the cursor is located

    Press # l: move the cursor to the # position of the row, such as 5l

  • Delete text

    Press x: Delete the "Next" character at the cursor position each time.

    Press # x: for example, 6x to delete the next six characters in the cursor position

    Press X: uppercase X. Each time the cursor is deleted, the first character of the cursor is deleted.

    Press # X: Delete the # characters before the cursor

    Press dd to delete the row where the cursor is located.

    Press # dd: delete from the row where the cursor is located # Row

  • Copy

    Press yw to copy the character from the cursor position to the end of the word to the buffer zone.

    Press # yw: Copy # words to the cache

    Press yy: copy the row where the cursor is located to the buffer

    Press # yy: Start from the row where the cursor is located and count down # copy the row to the buffer zone

    Press p: paste the characters in the buffer zone to the cursor position. All commands related to "y" must work with "p" to complete the copy and paste function.

  • Replace

    Press r to replace the character at the cursor

    Press R: Replace the character at which the cursor is located. Press ESC.

  • Undo the last operation

    Press u: If you mistakenly execute a command, you can immediately press "u" to return to the previous operation. You can run the "u" command multiple times.

  • Change

    Press "cw": change the word at the cursor to the ending point.

    Press "c # w": for example, "c3w" to change three words

  • Jump to the specified row

    Press ctrl + g to list the row number of the cursor.

    Press # G. For example, "15G" to move the cursor to the first row of the article.

Introduction to last line mode

Before using "last line mode", remember to press the "ESC" key to confirm that you are already in "command mode" and then press 「: the colon (:) returns the last line mode 」.

  • List row numbers

    "Set nu": After "set nu" is entered, the row number is listed before each row in the file.

  • Jump to a row in the file

    "#": "#" Indicates a number. Enter a number after the colon and press enter to jump to the row. For example, enter the number 15 and press Enter, this will jump to the second line of the article.

     

  • Search for characters

    "/Keyword": press the "/" key and enter the character you want to search for. If the key word you want for the first time is, you can always press "n" until you find the keyword you want.

    「? Keyword: First press 「?」 Key, and then enter the character you want to search for. If the key word you want for the first time is not what you want, you can press "n" until you find the keyword you want.

  • Save files

    "W": Enter "w" in the colon to save the file.

     

  • Leave vi

    Q: Press q to exit. If you cannot exit vi, you can follow q with 「!」 Force exit vi.

    "Wq": it is generally recommended that you use "w" when you exit, so that you can save files when you exit.


How to Use the linux Command vi Text Editor

If you want to edit a document, you can directly edit it. If you have document AA, you can use vi AA. [Note: it must be in the directory where AA is located]
If you do not have a document and want to edit it, You can directly edit vi aa]
You can also create a document touch aa before editing vi aa.
The editor has three modes: Command Line Mode 2 last line mode 3 input mode
Press Esc to enter the command line mode and the system default mode.
In input mode, you can press o I a to exit and enter the last line and command line mode.
You can press ctrl + to exit the editor or save the exit document.
Q! [Force exit and do not save] q [exit and do not save] wq [You can add a new one after exiting and saving !]
There are a lot of commands in input mode and command line mode. If you want to know which ones you can talk to me
For example, copy (yy) paste (p) Delete (d) and so on

In linux, how does one edit a file without opening a text editor (vi?

There are many Linux text editors ...... Use whatever you want ...... You can also use echo> to output text to a file.
 

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.