3.2 Linux Vim editor

vi is a powerful full-screen text editing tool that has always been the default document editor for UNIX-like operating systems. vim is an enhanced version of the vi text editor, which extends many practical functions based on the vi editor.

Enter “Vim file name”: if the file exists, Vim will open the file; if the file does not exist, Vim will create the file.

1. Command mode

In this mode, basic cursor movement and a large number of shortcut key operations can be achieved.

a. Turn page

ctrl+f/page down

Turn down one page

ctrl+b/page up

turn one page up

ctrl+d

Turn down half a page

ctrl+u

Turn up half a page

b. Cursor movement

The simplest way to move the cursor in Vi/Vim is to use the direction keys (up, down, left, right), but this method is inefficient. A more efficient way is to use shortcut keys. All shortcut keys are in command mode. Use directly.

Cursor movement between words:

b

Move cursor one word to the left

nb

Move the cursor n words to the left

w

Move cursor one word to the right

nw

Move the cursor n words to the right

Cursor movement in the current line:

0/home/^

Move the cursor to the beginning of the line where the cursor is located

$/end

Move the cursor to the end of the line where the cursor is

h

Move the cursor one position to the left

j

Move the cursor down one line

k

Move the cursor up one line

l

Move the cursor one position to the right

fx

Move the cursor to the next x character in the current line

fx

Move the cursor to the previous x character in the current line

Cursor movement on the current page:

H

Move the cursor to the first character of the first line of the current page

M

Move the cursor to the first character of the middle line of the current page

L

Move the cursor to the first character of the last line of the current page

Cursor movement in the current file:

gg

Move the cursor to the beginning of the last line of the file

G

Move the cursor to the beginning of the first line of the file

nG

Move the cursor to the beginning of the specified line

c. Shortcut key operation

p

Copy the line and paste it below the line where the cursor is.

The character is copied and pasted to the right of the character where the cursor is.

P

Copy the line and paste it above the line where the cursor is.

The character copied is pasted to the left of the character where the cursor is.

u

Undo the previous action

U

Cancel all edits made to the current row

x

Delete the character under the cursor

X

Delete the character before the cursor

yy

Copy a line

J

Remove newline characters to merge two lines into one

dd

Delete/cut a line

d$

Delete the content from the cursor to the end of the line

d^

Delete the content from the cursor to the beginning of the line

nx

Delete n characters consecutively backwards

nyy

Copy n lines below the line under the cursor

ndd

Cut/delete n lines below the line under the cursor

y/d 1G

Copy/cut, delete the content from the line where the cursor is to the first line

y/dG

Copy/cut, delete the content from the line where the cursor is to the last line

y/d $

Copy/cut, delete the content from the character where the cursor is to the end of the line

y/d 0

Copy/cut, delete the content from the character before the cursor to the beginning of the line

d. Search for matches

/keyword(search down) ?keyword(search up)

n

Repeat the previous operation (“/” downward, “?” upward) search

N

Use n as the basis and search in reverse direction by n

2. Insert mode

Basic text editing functions can be implemented in this mode.

i

Insert before the character currently under the cursor

a

Insert after the character currently under the cursor

I

Insert at the beginning of the line currently under the cursor

A

Insert at the end of the line where the cursor is currently located

o

Insert a new blank line below the current cursor line and start inserting

O

Insert a new blank line above the current cursor line and start inserting

r

Replace the character under the cursor once

R

Enter continuous replacement mode, press esc to exit

3. Last line mode

In this mode, specific functions can be implemented by entering specific commands, such as saving, exiting, etc.

:q!

force quit

:wq|:x|:x!|shift + zz

Save and exit

:w

save

:w b.txt

Save as b.txt

:e /etc/passwd

Open new file for editing

:r /etc/passwd

Read the contents of other files in the current file to the current cursor

:set number | :set nu

Enter in command line mode

:set ignorecase

Enter in command line mode

Tip: When Vim prompts the error message E32: No file name, it means that you have not set a file name for the file, and you need to follow w with the file name.

Replacement: Enter command mode to complete the replacement function: [Replacement range] sub/old content/new content[/g]

  • Replacement range:
    • The replacement range is optional. By default, only the content of the current line is replaced.
    • % Find and replace in the entire file content
    • n,m searches and replaces the file content within the specified number of lines.
  • /g” will replace all matching content in each line within the replacement range. When “/g” is omitted, only the first matching content in each line will be replaced.
:s/root/admin/ # Replace the first occurrence of root in the current line under the cursor with admin, otherwise do not replace it.
:s/root/admin/g # Replace all root characters in the current line under the cursor with admin
:3,5 s/sbin/bin/g # Replace all sbin between lines 3 to 5 with bin
:% s/nologin/fault/g # Replace nologin with fault in all lines
4. Vi/Vim skills
a. Automatic completion
  • If the input content has appeared before, you can use the “Crtl + N” shortcut key to achieve automatic completion.
b. Execute shell command
  • Execute a shell command without exiting the Vim editor. This can be achieved through “:!{command}”, for example, “:!ls” to view the name of the file in the current directory .
c. Multi-viewport editing
  • Split windows are particularly important when editing multiple documents at the same time. Enter “:split” in command mode. This command splits the window horizontally. For vertical splitting, you can use the “:vsplit” command. Enter the “:close” command to close the current window.
    • :split second.txt: Split the window and open a new file.
    • Ctrl + w + h: Jump to a window on the left
    • Ctrl + w + l: Jump to a window on the right
    • Ctrl + w + j: Jump to the previous window
    • Ctrl + w + k: Jump to the next window