banner
老孙

老孙博客

资深网民孙先生
mastodon
email

Basic usage methods of the Vi editor

The vi editor is the standard editor for all Unix and Linux systems, and its power is no less than that of any modern text editor. Here, we will simply introduce its usage and a small portion of its commands.

Since the vi editor is the same for any version of Unix and Linux systems, you can learn more about it anywhere else that introduces vi.

Vi is also the most basic text editor in Linux, and once you learn it, you will be able to navigate freely in the world of Linux.

  1. Basic Concepts of vi

Basically, vi can be divided into three modes: command mode, insert mode, and last line mode. The functions of each mode are as follows:

  1. Command mode

Control the movement of the screen cursor, delete characters, words or lines, move or copy a certain section, and enter insert mode or last line mode.

  1. Insert mode

Only in insert mode can you enter text. Press the "ESC" key to return to command mode.

  1. Last line mode

Save or exit vi, or set the editing environment, such as searching for strings, listing line numbers, etc. However, in general, we simplify vi into two modes, including last line mode in command mode.

  1. Basic Operations of vi

a) Entering vi

Enter vi and the file name at the system prompt to enter the vi full-screen editing interface: $ vi myfile. However, please note that after entering vi, you are in "command mode". You need to switch to "insert mode" to enter text. People who use vi for the first time may want to use the up, down, left, and right arrow keys to move the cursor, but the computer keeps beeping, making them frustrated. So after entering vi, don't move around randomly, switch to "insert mode" first!

b) Switching to insert mode to edit the file

In "command mode", press the letter "i" to enter "insert mode", and then you can start entering text.

c) Switching in insert mode

You are currently in "insert mode", so you can only enter text continuously. If you find that you have entered a wrong character and want to use the arrow keys to move back and delete the character, you need to press the "ESC" key to switch to "command mode" before deleting the text.

d) Exiting vi and saving the file

In "command mode", press ":" to enter "Last line mode", for example:

w filename (enter "w filename" to save the article with the specified file name)

wq (enter "wq" to save the file and exit vi)

q! (enter q! to exit vi without saving)

  1. Function Keys in Command Mode
  1. Insert mode: Press "i" to switch to insert mode, press "i" to start entering text from the current cursor position; Press "a" to switch to insert mode, and start entering text from the position after the current cursor position; Press "o" to switch to insert mode and insert a new line, starting from the beginning of the line.

  2. Switching from insert mode to command mode: Press the "ESC" key.

  3. Moving the cursor: vi can directly use the arrow keys on the keyboard to move up, down, left, and right, but the standard vi uses lowercase letters "h", "j", "k", "l" to control the cursor to move left, down, up, and right by one position. Press "ctrl" + "b": Move the screen "back" one page. Press "ctrl" + "f": Move the screen "forward" one page. Press "ctrl" + "u": Move the screen "back" half a page. Press "ctrl" + "d": Move the screen "forward" half a page. Press the number "0": Move to the beginning of the article. Press "G": Move to the end of the article. Press "$": Move to the end of the line where the cursor is. Press "^": Move to the beginning of the line where the cursor is. Press "w": Jump to the beginning of the next word. Press "e": Jump to the end of the next word. Press "b": Jump back to the beginning of the previous word. Press "#l": Move the cursor to the #th position of the line, such as: 5l, 56l.

  4. Deleting text: Press "x" to delete the character after the cursor each time. Press "#x": For example, "6x" means deleting the 6 characters after the cursor. Press "X": Capital X, delete the character before the cursor each time. Press "#X": For example, "20X" means deleting the 20 characters before the cursor. Press "dd": Delete the line where the cursor is. Press "#dd": Delete # lines starting from the line where the cursor is.

  5. Copying: Press "yw" to copy the characters from the current cursor position to the end of the word to the buffer. Press "#yw": Copy # words to the buffer. Press "yy" to copy the line where the cursor is to the buffer. Press "#yy": For example, "6yy" means copying 6 lines of text from the line where the cursor is. Press "p" to paste the characters in the buffer at the current cursor position. Note: All copy commands related to "y" must be used with "p" to complete the copy and paste function.

  6. Replacing: Press "r" to replace the character at the current cursor position. Press "R" to replace the characters from the current cursor position until the "ESC" key is pressed.

  7. Undoing the previous operation: Press "u" to immediately undo the previous command. Press "u" multiple times to undo multiple times.

  8. Changing: Press "cw" to change the word at the current cursor position to the end of the word. Press "c#w": For example, "c3w" means changing 3 words.

  9. Jumping to a specific line: Press "ctrl" + "g" to display the line number of the current cursor position. Press "#G": For example, "15G" means moving the cursor to the beginning of the 15th line of the article.

  1. Introduction to Commands in Last Line Mode

Before using "last line mode", please remember to press the "ESC" key to make sure you are in "command mode", and then press ":" to enter "last line mode".

A) Listing line numbers: "set nu": After entering "set nu", line numbers will be displayed in front of each line in the file.

B) Jumping to a specific line in the file: "#": "#" represents a number. After entering a number after the colon and pressing Enter, you will jump to that line. For example, entering the number 15 and then pressing Enter will jump to the 15th line of the article.

C) Searching for characters: "/keyword": Press the "/" key first, and then enter the character you want to search for. If the first keyword you find is not what you want, you can press "n" continuously to search for the keyword you want. "?keyword": Press the "?" key first, and then enter the character you want to search for. If the first keyword you find is not what you want, you can press "n" continuously to search for the keyword you want.

D) Saving the file: "w": Enter the letter "w" after the colon to save the file.

E) Exiting vi: "q": Press "q" to exit. If you cannot exit vi, you can follow "q" with a "!" to force exit vi. "qw": It is generally recommended to use "w" together when exiting, so that the file can be saved when exiting.

  1. vi Command List

  2. The table below lists the functions of some keys in command mode:
    h: Move the cursor one character to the left
    l: Move the cursor one character to the right
    k: Move the cursor one line up
    j: Move the cursor one line down
    ^: Move the cursor to the beginning of the line
    0: The number "0", move the cursor to the beginning of the article
    G: Move the cursor to the end of the article
    $: Move the cursor to the end of the line where the cursor is
    Ctrl+f: Scroll forward one page
    Ctrl+b: Scroll backward one page
    Ctrl+d: Scroll forward half a page
    Ctrl+u: Scroll backward half a page
    i: Insert characters at the cursor position
    a: Add characters starting from the position after the current cursor position
    o: Insert a new line and start entering text from the beginning of the line
    ESC: Switch from input mode to command mode
    x: Delete the character after the cursor
    #x: For example, "6x" means deleting 6 characters after the cursor
    X: Delete the character before the cursor
    #X: For example, "20X" means deleting 20 characters before the cursor
    dd: Delete the line where the cursor is
    #dd: Delete # lines starting from the line where the cursor is
    yw: Copy the character from the current cursor position to the end of the word to the buffer
    #yw: Copy # words to the buffer
    yy: Copy the line where the cursor is to the buffer
    #yy: For example, "6yy" means copying 6 lines of text from the line where the cursor is
    p: Paste the characters in the buffer at the current cursor position
    u: Cancel the operation
    cw: Change the word at the current cursor position to the end of the word
    #cw: Change # words at the current cursor position

  3. The table below lists some commands in line mode:
    w filename: Save the currently edited file as filename
    wq filename: Save the currently edited file as filename and exit vi
    q!: Discard all modifications and exit vi
    set nu: Display line numbers
    / or ?: Search, enter the content you want to search after /
    n: Used together with / or ?, if the searched content is not the desired keyword, press n or forward (with /) or backward (with ?) to continue searching until the desired keyword is found

For the first time using vi, there are a few points to note:

  1. After opening a file with vi, you are in "command mode". You need to switch to "insert mode" to enter text.

Switching method: Press the letter "i" in "command mode" to enter "insert mode", and then you can start entering text.

  1. After editing, you need to switch from insert mode to command mode to save the file. Switching method: Press the "ESC" key.

  2. Save and exit the file: Enter in command mode! (Don't forget the : before wq)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.