Detailed explanation of VI commands in Linux

Time:2021-10-13

VI editor is the Standard Editor under all UNIX and Linux systems. Its power is not inferior to any latest text editor. Here is a brief introduction to its usage and a small part of instructions. Since the VI editor is exactly the same for any version of UNIX and Linux systems, you can learn more about VI anywhere else. VI is also the most basic text editor in Linux. After learning it, you will walk freely in the world of Linux.

Basic concepts of VI

Basically VI can be divided into three states: 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, deletion of characters, words or lines, moving and copying a section and entering insert mode, or to last line mode.

   2) Insert mode

Text input can only be made in insert mode. Press “ESC” to return to command line mode.

    3) Last line mode

Save the file or exit VI, or set the editing environment, such as finding string, listing line number, etc.

However, in general, when using VI, we simplify it into two modes, that is, the last line mode is also counted into the command line mode.

Command to enter VI

VI filename: open or create a new file and place the cursor at the beginning of the first line
VI + n filename: open the file and place the cursor at the beginning of line n
VI + filename: open the file and place the cursor at the beginning of the last line
VI + / pattern filename: open the file and place the cursor at the first string matching the pattern
VI – R filename: a system crash occurred when editing with VI last time. Recover filename
VI filename…. filename: open multiple files and edit them in sequence

Move cursor class Command

h: Move the cursor one character to the left
l: Move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl + P: move the cursor up one line
J or Ctrl + N: move the cursor down one line
Enter: move the cursor down one line
W or W: move the cursor right one word to the beginning
B or B: move the cursor left one word to the beginning
E or E: move the cursor right one word to the end of the word
): move the cursor to the end of the sentence
(: move the cursor to the beginning of the sentence
}: move the cursor to the beginning of the paragraph
{: move the cursor to the end of the paragraph
Ng: move the cursor to the beginning of line n
N +: move the cursor down n lines
N -: move the cursor up n lines
N $: move the cursor to the end of line n
H: Move the cursor to the top line of the screen
M: Move the cursor to the middle line of the screen
50: The cursor moves to the last line of the screen
0: (note the number zero) the cursor moves to the beginning of the current line
$: the cursor moves to the end of the current line

Screen rollover commands

CTRL + U: flip the first half of the screen to the file
CTRL + D: flip half the screen to the end of the file
CTRL + F: flip to the end of the file
Ctrl+b; Flip the first screen of the file
NZ: scroll the nth line to the top of the screen. If n is not specified, scroll the current line to the top of the screen.

Insert text class Command

i: In front of the cursor
1: At the beginning of the current line
a: After cursor
A: At the end of the current line
o: Create a new row below the current row
O: Creates a new line above the current line
r: Replace current character
R: Replace the current character and subsequent characters until you press ESC
s: Replaces the specified number of characters with the entered text, starting at the current cursor position
S: Deletes the specified number of lines and replaces them with the entered text
NCW or NCW: modifies the specified number of words
NCC: modify a specified number of rows

Delete command

NDW or NDW: delete the N-1 words starting from and after the cursor
Do: Delete to the beginning of the line
D $: Delete to end of line
NDD: delete the current line and its subsequent n-1 lines
X or X: delete a character, X deletes the character after the cursor, and X deletes the character before the cursor
CTRL + U: delete the text entered in the input mode

Search and replace commands

/Pattern: search for pattern from the beginning of the cursor to the end of the file
? Pattern: searches the file header for pattern from the beginning of the cursor
n: Repeat the previous search command in the same direction
N: Repeat the last search command in the opposite direction
: S / P1 / P2 / G: replace all P1 in the current row with P2
: N1, N2S / P1 / P2 / G: replace all P1 in lines N1 to N2 with P2
: g / P1 / S / / P2 / G: replace all P1 in the file with P2

Option settings

All: lists the settings of all options
Term: sets the terminal type
Ignore: ignore case in search
List: displays tab stops (Ctrl + I) and end of line flags ($)
Number: displays the line number
Report: displays the number of modified by line oriented commands
Terse: displays a short warning message
Warn: when going to another file, if the current file is not saved, the no write message will be displayed
Nomagic: allows the use of special characters without a “\” in the search mode
Nowrapscan: prevents VI from starting from the other end when the search reaches both ends of the file
MESG: allow VI to display the information written by other users to their own terminals with write

Last line command

: N1, N2, CO N3: copy the contents from line N1 to line N2 to line N3
: N1, N2, m N3: move the content from line N1 to line N2 to line N3
: N1, N2 D: delete the content between N1 and N2 lines
: W: save current file
: e filename: open the file filename for editing
: X: save the current file and exit
: Q: exit VI
:q!: Exit VI without saving the file
:! Command: execute shell command command
:n1,n2 w! Command: take the contents from line N1 to line N2 in the file as the input of command and execute it. If N1 and N2 are not specified, it means that the whole file content is taken as the input of command
:r! Command: put the output result of the command on the current line

Register operation

“? NYY: save the contents of the current line and its next N lines to the register”, where “is a letter and N is a number
“? NYW: save the current line and its next n words into the register”, where “is a letter and N is a number
“? NYL: save the current line and its next n characters to the register”, where “is a letter and N is a number
“? P: take the contents of the register” and place it at the cursor position. Here “can be a letter or a number
NDD: delete the current line and N lines of text below it, and put the deleted content into deletion register 1.

Use of VI

——————————————————————————–

1、 Insert text

┌──┬────────────┐
│ command │ description │
├──┼────────────┤
│ I │ insert text before the current character │
├──┼────────────┤
│ I │ insert text at the beginning of the line │
├──┼────────────┤
│ a │ add text after the current character │
├──┼────────────┤
│ a │ add text at the end of the line │
├──┼────────────┤
│ o │ insert an empty line after the current line │
├──┼────────────┤
│ o │ insert an empty line before the current line │
├──┼────────────┤
│ R │ enter text by overwriting │
└──┴────────────┘

2、 Move cursor

┌─────┬───────────┐
│ command │ description │
├─────┼───────────┤
│ J or down arrow │ move down one line │
├─────┼───────────┤
│ K or up arrow │ move one line up │
├─────┼───────────┤
│ h or left arrow │ move one character left │
├─────┼───────────┤
│ L or right arrow │ shift right one character │
├─────┼───────────┤
│ w │ move one word right │
├─────┼───────────┤
│ w │ move a word separated by a space to the right │
├─────┼───────────┤
│ B │ move one word left │
├─────┼───────────┤
│ B │ move one space separated word left │
├─────┼───────────┤
│ 0 │ move to the beginning of the line │
│ Ctrl-F │ page forward │
├─────┼───────────┤
│ ctrl-b │ page back │
├─────┼───────────┤
│ ng │ to line n │
├─────┼───────────┤
│ g │ to the last line │
└─────┴───────────┘

3、 Replace text

┌─────┬──────┐
│ command │ description │
├─────┼──────┤
│ $│ to end of line │
├─────┼──────┤
│ (│ to the beginning of the sentence │
├─────┼──────┤
│) │ to the end of the sentence │
├─────┼──────┤
│ {│ to the beginning of the paragraph │
├─────┼──────┤
│} │ to the end of the paragraph │
└─────┴──────┘

4、 Delete text

┌───┬───────────┐
│ command │ description │
├───┼───────────┤
│ R │ replace one character │
├───┼───────────┤
│ C │ modify the text until the ESC key is pressed │
├───┼───────────┤
│ CW │ modify next word │
├───┼───────────┤
│ CNW │ modify the next n words │
└───┴───────────┘

5、 Text editing

┌──┬──────────────────────┐
│ Mingsi │ description │
├──┼──────────────────────┤
│ YY │ move a line of text to the default buffer │
├──┼──────────────────────┤
│ yn │ move the next word to the default buffer │
├──┼──────────────────────┤
│ ynw │ move the following n words to the default buffer │
├──┼──────────────────────┤
│ P │ if the default buffer contains a line of text, the current │
│ │ inserting an empty line after a line will delete the sound in the default buffer │
│ │ paste into this line; If the default buffer contains │
│ │ multiple words, paste these words to the right of the cursor. │
├──┼──────────────────────┤
│ P │ if the default buffer contains a line of text, the current │
│ │ inserting an empty row before a row will delete │ in the default buffer
│ │ paste into this line; If the default buffer contains │
│ │ multiple words, paste these words to the left of the cursor

  │
└──┴──────────────────────┘

6、 Save exit

┌───────────┬───────────────┐
│ command │ description │
├───────────┼───────────────┤
│ zz │ save and exit │
├───────────┼───────────────┤
│: W filename │ write file │
├───────────┼───────────────┤
│: W │ write file │
├───────────┼───────────────┤
│: X │ save (if the current file has been modified) and exit │
├───────────┼───────────────┤
│:q! │ quit without saving the file │
├───────────┼───────────────┤
│: Q │ exit VI │

VI common skills

VI command can be said to be the most commonly used command for editing files in the UNIX / Linux world, but because of its large command set, many people are not used to using it. In fact, you only need to master the basic commands and use them flexibly, you will find its advantages and gradually enjoy using this method. This article aims to introduce some of the most commonly used commands and advanced application skills of vi.

1、 Introduction to basic commands

—-1. Cursor command

k. J, h, l — up, down, left and right cursor movement commands. Although you can use the four cursor keys on the right side of the keyboard in Linux, it is very useful to remember these four commands. These four keys are the basic position of the right hand on the keyboard.
Ng — jump command. N is the number of lines. The command immediately jumps the cursor to the specified line.
CTRL + G — report on the number of rows and columns where the cursor is located.
w. B — make the cursor skip a word forward or backward.

—-2. Edit command

i. A, R — insert character commands before, after and at the cursor (I = insert, a = append, r = replace).
CW, DW — command to change (replace) / delete the word where the cursor is located (C = change, d = delete).
x. D $, DD — delete one character, delete all characters from the cursor to the end of the line, and delete the whole line.
—-3. Find command
—- /string、? String — a command to find the corresponding string backward or forward from the cursor.
—-4. Copy command
—-YY, P — command to copy a line to the clipboard or take out the contents of the clipboard.

2、 Common problems and application skills

—-1. Read the contents in / etc / passwd in a new file and take out the user name part.

—- vi file
—-: R / etc / passwd read in / etc / passwd at the cursor in the open file
—-:% s /:. * / / g delete all parts in / etc / passwd from the colon to the end of the line after the user name.
—-You can also read the contents of the file after the specified line number. For example, use the command “: 3R / etc / passwd” to read all the contents of / etc / passwd from line 3 of the new file.
—-We can also delete all blank lines and comment lines starting with # in the file by using the following methods.
—- #cat squid.conf.default | grep -v ^$ | grep -v ^#

—-2. After opening a file for editing, you know that the logged in user does not have write permission to the file and cannot save it. You need to save the changes to a temporary file.

—- vi file
—-: w / TMP / 1 save all the changes made, or save some of them to a temporary file. For example, only save the contents between lines 20 to 59 into a file / TMP / 1. We can type the following command.
—- vi file
—- :20,59w /tmp/1

—-3. Edit a file with VI, but delete large paragraphs.

—-First, open the file with the editing command “VI file”, then move the cursor to the line to be deleted, press Ctrl + G to display the line number, and then press Ctrl + G at the end to display the line number at the end of the file.
—-: 231045d assuming that the line numbers obtained twice are 23 and 1045, delete all the contents in this period. You can also mark the start and end lines to be deleted with Ma and MB commands, and then delete them with “: A, BD” command.

—-4. Add some strings at the beginning or end of each line or several lines of the whole file.

—- vi file
—-: 3, $s / ^ / some string / insert “some string” at the beginning of the first to last lines of the file.
—-:% s / $/ some string / g add “some string” at the end of each line of the entire file.
—-:% s / string1 / string2 / g replace “string1” with “string2” throughout the file.
—-: 3,7s / string1 / string2 / only replace “string1” in lines 3 to 7 of the file with “string2”.
—-Note: where s is substitute,% represents all rows, and G represents global.

—-5. Edit two files at the same time, copy the text in one file and paste it into another file.

—- vi file1 file2
—-YY copy the line at the cursor of file 1
—-: n switch to file 2 (n = next)
—-P paste the copied line at the cursor of file 2
—-: nswitch back to file 1

—-6. Replace the path in the file.

—-Use the command: “% s # / usr / bin # / bin #g” to replace all paths in the file / usr / bin with / bin. You can also use the command ‘:% s / / usr / bin / / bin / g’, where ” is an escape character, indicating that the subsequent “/” character is a meaningful character, not a separator.

The above is the full description of VI commands in Linux introduced by Xiaobian. I hope you like it.

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]