How to modify and save files by using the vi command in linux
Access the vi command
Vi filename: open or create a 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 a line
Vi/pattern filename: open the file and place the cursor at the first string matching pattern
Vi-r filename: a system crash occurred when vi was being edited last time, restoring filename
Vi filename... filename: open multiple files and edit them one by one.
Move cursor 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 a row
J or Ctrl n: Move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move one word to the end of the word to the right of the cursor.
): 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 rows
N-: Move n rows above the cursor
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
L: Move the cursor to the screen line
0: (note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
Screen tumble command
Ctrl u: half screen at the beginning of the file
Ctrl d: Flip the half screen to the end of the file
Ctrl f: flip the screen to the end of the file
Ctrl + B; open a screen at the beginning of the file
Nz: Rolls row n to the top of the screen. if n is not specified, the current row is rolled to the top of the screen.
Insert text commands
I: before the cursor
I: At the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: replace the current character
R: replace the current and subsequent characters until you press ESC.
S: starting from the current cursor position, replace the specified number of characters with the input text
SAVE command
Press ESC to jump to the command mode, and then:
: W save the file but do not exit vi
: W file saves the changes to the file and does not exit vi.
: W! Force save, do not launch vi
: Wq: Save the file and exit vi.
: Wq! Force save the file and exit vi
Q: Exit vi without saving the file.
: Q! Do not save the file, force exit vi
: E! Discard all modifications and start editing the last saved file.
And press Enter. OK!
Access the vi command
Vi filename: open or create a 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 a line
Vi/pattern filename: open the file and place the cursor at the first string matching pattern
Vi-r filename: a system crash occurred when vi was being edited last time, restoring filename
Vi filename... filename: open multiple files and edit them one by one.
Move cursor 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 a row
J or Ctrl n: Move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move one word to the end of the word to the right of the cursor.
): 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 rows
N-: Move n rows above the cursor
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
L: Move the cursor to the screen line
0: (note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
Screen tumble command
Ctrl u: half screen at the beginning of the file
Ctrl d: Flip the half screen to the end of the file
Ctrl f: flip the screen to the end of the file
Ctrl + B; open a screen at the beginning of the file
Nz: Rolls row n to the top of the screen. if n is not specified, the current row is rolled to the top of the screen.
Insert text commands
I: before the cursor
I: At the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: replace the current character
R: replace the current and subsequent characters until you press ESC.
S: starting from the current cursor position, replace the specified number of characters with the input text
SAVE command
Press ESC to jump to the command mode, and then:
: W save the file but do not exit vi
: W file saves the changes to the file and does not exit vi.
: W! Force save, do not launch vi
: Wq: Save the file and exit vi.
: Wq! Force save the file and exit vi
Q: Exit vi without saving the file.
: Q! Do not save the file, force exit vi
: E! Discard all modifications and start editing the last saved file.
And press Enter. OK!