|
vi/vim basic usage This article covers the basics of how to use vi (vim), but it's basically enough for the average user! The difference between i/vim is that they are both multi-modal editors, the difference is that vim is an upgraded version of vi, which is not only compatible with all the instructions of vi, but also has some new features in it. For example, syntax is highlighted, and visualization operations can be run not only in the terminal, but also on X window, Mac OS, and Windows.
vi editor is the standard editor for all Unix and Linux systems, and it is not inferior to any modern text editor, here is just a brief introduction to its usage and a small number of instructions. Since the vi editor is identical for all versions of Unix and Linux, you can learn more about it anywhere else where vi is introduced. Vi is also the most basic text editor in Linux, and after learning it, you will be able to navigate the Linux world without any hindrance. [Simply, you can use the up, down, left, right, and left direction arrows and the delete, backspace keys to move and delete, whether in command mode or insert mode]
1. The basic concept of VI Basically, vi can be divided into three states, namely command mode, insert mode, and last line mode, and the functions of each mode are distinguished as follows: 1) Command Mode) Control the movement of the screen cursor, the deletion of characters, words or lines, the movement to copy a section and enter the Insert mode, or to the last line mode. 2) Insert mode Only in Insert mode, you can do text input, press the "ESC" key to return to command line mode. 3) Last line mode Save or exit the file, and you can also set the editing environment, such as finding strings and listing line numbers...... etc.
However, we generally simplify vi into two modes when using it, that is, the last line mode is also included in the command mode (command mode).
2. Basic operation of vi a) Enter VI After entering vi and file name in the system prompt, enter the vi full-screen editing screen: $ vi myfile. However, one thing to note is that after entering vi, you are in "command mode", and you need to switch to "Insert mode" to enter text. First-time users of vi will want to use the up, down, left and right keys to move the cursor first, but the computer keeps beeping, making itself half angry, so after entering vi, don't move around, switch to "Insert mode"!
b) Switch to Insert mode to edit the file Click the letter "i" under "Command Line Mode" to enter "Insert mode", and then you can start typing text.
c) Insert toggle You are currently in "Insert mode", so you can only keep typing text if you find that you have typed the wrong word! If you want to use the cursor keys to move back and delete the word, you need to click the "ESC" key to go to "command mode" and then delete the text.
d) Exit VI and save the file Under Command Mode, click the ":" colon key to enter the Last line mode, for example: : w filename (enter "w filename" to save the article with the specified file name) : wq (enter "wq", save and exit vi) : q! (Enter q!, force quit vi without saving)
3. Command mode function key 1). Insert mode Press "i" to switch to insert mode "insert mode", press "i" to enter insert mode to enter the file from the current position of the cursor; After pressing "a" to enter insertion mode, the text is entered from the next position of the current cursor; After pressing "O" to enter insertion mode, you can insert a new line and enter text from the beginning of the line.
2). Switch from insert mode to command line mode Press the "ESC" button.
3). Move the cursor vi can be moved up, down, left, and right directly with the cursor on the keyboard, but the regular vi uses the lowercase letters "h", "j", "k", and "l" to control the cursor to move one space left, down, up, and right respectively. Press "Ctrl" + "B": Move the screen to "Back" one page. Press "Ctrl" + "F": Move the screen one page to "Forward". Press "Ctrl" + "U": Move the screen half a page to "Back". Press "Ctrl" + "D": Move the screen half a page to "Forward". 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 line" of the row where the cursor is located. Press "^": Move to the "Line Head" of the row where the cursor is located Press "w": The cursor jumps to the beginning of the next word Press "e": The cursor jumps to the suffix of the next word Press "b": Cursor back to the beginning of the previous word Press "#l": The cursor moves to the # position of the row, e.g., 5L, 56L.
4). Delete text "x": Each time you press it, delete the character "behind" where the cursor is located. "#x": For example, "6x" means to delete the "after" 6 characters where the cursor is. "X": Uppercase X, each time you press it, delete the character "before" where the cursor is located. "#X": For example, "20X" means to delete the "front" 20 characters where the cursor is. dd: Delete the row where the cursor is located. "#dd": Delete the # row from the row where the cursor is located
5). Copy "yw": Copy the character from the cursor to the suffix into the buffer. "#yw": Copy the # word to the buffer "yy": Copy the row where the cursor is located to the buffer. "#yy": For example, "6yy" means copying 6 lines of text "down" from the line where the cursor is located. "p": Paste the characters in the buffer to the location of the cursor. Note: All copy commands related to "y" must work with "p" to complete the copy and paste function.
6). Replace "r": Replace the character where the cursor is located. "R": Replace the character where the cursor goes until the "ESC" key is pressed.
7). Reply to the last action "u": If you execute a command by mistake, you can immediately press "u" to go back to the previous operation. Press "u" multiple times to perform multiple replies.
8). Changes "cw": Change the word where the cursor is located to the suffix "c#w": For example, "c3w" means changing 3 words
9). Skip to the specified line "Ctrl" + "G" lists the row number of the row where the cursor is located. "#G": For example, "15G", which means moving the cursor to the beginning of the 15th line of the article.
4. Introduction to the Last line mode command Before using "last line mode", remember to press the "ESC" key to make sure you are in "command mode", and then press the ":" colon to enter "last line mode".
A) List the line numbers "set nu": After entering "set nu", the line number will be listed in front of each line in the document.
B) Skip to a line in the document "#": The "#" sign indicates a number, enter a number after the colon, and then press enter to jump to the line, such as entering the number 15, and then enter, you will jump to the 15th line of the article.
C) Find characters "/keyword": Press the "/" key first, then enter the character you are looking for, if the keyword you find for the first time is not what you want, you can keep pressing "n" to find the keyword you want. 「? Keyword": First press "?" key, and then enter the character you are looking for, if the keyword you are looking for for the first time is not what you want, you can keep pressing "n" to move forward until you find the keyword you want.
D) Save the file "w": Enter the letter "w" in the colon to save the file.
E) Leave vi "Q": Press "Q" to exit, if you can't leave VI, you can follow "Q" with a "!" Forced to leave vi. "qw": It is generally recommended to use it with "w" when leaving, so that you can save the file when you exit.
5. vi command list 1) The following table lists some of the functions of keys in command mode:
h Move the cursor left by one character l Move the cursor to the right by one character k cursor moves up one line j cursor moves down one line ^Move the cursor to the beginning of the row 0 number "0", the cursor moves to the beginning of the article G cursor moves to the end of the article $Cursor moves to the end of the row Ctrl+f to flip the screen forward Ctrl+b to flip the screen backwards Ctrl+d to flip the screen halfway Ctrl+U flips back half the screen i inserts the character before the cursor position The last character of a at the location of the cursor begins to increase o Insert a new line and enter from the beginning of the line ESC retreats from input state to command state x removes the character after the cursor #x删除光标后的#个字符 X (uppercase X), remove the character in front of the cursor #X删除光标前面的#个字符 dd deletes the row where the cursor is located #dd删除从光标所在行数的#行 yw copies a word where the cursor is #yw复制光标所在位置的#个字 yy copy a row where the cursor is located #yy复制从光标所在行数的#行 ppaste uCancel the operation cw changes a word where the cursor is located #cw更改光标所在位置的#个字
2) The following table lists some of the commands in line command mode w filename stores the file being edited as filename wq filename stores the file being edited as filename and exits vi Q!abandon all modifications and exit vi set nu shows the line number
/或?查找,在/后输入要查找的内容 n and/or? If what you are looking for is not the keyword you are looking for, press n or backward (with / conjunction) or forward (with?) Conjunction) and continue searching until it is found.
The figure summarized by the master:
End!
|