Home Operation and Maintenance Linux Operation and Maintenance Detailed explanation of vi command under linux

Detailed explanation of vi command under linux

Jun 22, 2020 pm 01:08 PM
linux vi

The vi command is used to enter the vi editor. The vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor. This article will take you through the vi command under Linux and briefly introduce its usage.

Detailed explanation of vi command under linux

vi editor is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor.

Vi’s working mode

Basically vi can be divided into three states, namely command mode (command mode), insert mode ( Insert mode) and bottom line mode (last line mode), the functions of each mode are distinguished as follows:

1. Command line mode command mode)

Control the movement of the screen cursor, characters, words or Line deletion, moving and copying a section and entering Insert mode, or to last line mode.

2. Insert mode

Only in Insert mode, text input can be done. Press the "ESC" key to return to the command line mode.

3. Last line mode

Save the file or exit vi. You can also set the editing environment, such as searching for strings, listing line numbers, etc.

Some basic operations of vi

1. Enter vi

and enter vi and file name at the system prompt. Enter the vi full-screen editing screen:

For example:

$ vi myfile
Copy after login

But one thing to pay special attention to is that after you enter vi, you are in "command mode (command mode)". You need to Switch to "Insert mode" to enter text.

2. Switch to Insert mode to edit the file

In "Command mode (command mode)", click the letter "i" to enter "Insert mode" )", then you can start entering text.

3. Insert switching

You are currently in "Insert mode", you can only keep inputting text. If you find that you have entered the wrong word, you want to use the cursor keys to go to To move back and delete the word, you must first click the "ESC" key to go to "command mode (command mode)" and then delete the word.

4. Exit vi and save the file

In "command mode (command mode)", click the ":" colon key to enter "Last line mode", for example:

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

: wq (Enter "wq", save and exit vi)

: q! (Enter q! to force exit vi without saving)

Command mode function key

1), Insert mode

Press "i" to switch to the insert mode "insert mode". After pressing "i" to enter the insert mode, the input file starts from the current position of the cursor;
After pressing "a" to enter the insert mode, the input file starts from the current position. Start inputting text at the position next to the cursor;
After pressing "o" to enter insert mode, a new line is inserted, and text is entered starting from the beginning of the line.

2), switch from insert mode to command line mode

Press the "ESC" key.

3), move the cursor

 vi can directly use the cursor on the keyboard to move up, down, left, and right, but regular vi uses lowercase English letters "h", "j", "k", and "l" to control the cursor left, down, up, and respectively. Move one space to the right.
Press "ctrl" "b": the screen moves "back" one page.
Press "ctrl" "f": the screen moves "forward" one page.
Press "ctrl" "u": the screen moves half a page "back".
Press "ctrl" "d": the screen moves "forward" by 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 located.
Press "^": move to the "beginning" of the line where the cursor is
Press "w": the cursor jumps to the beginning of the next word
Press "e": the cursor jumps to the beginning of the next word End
Press "b": the cursor returns to the beginning of the previous word
Press "#l": the cursor moves to the #th position of the line, such as: 5l, 56l.

4) Delete text
「x」: Each time you press it, delete the character "behind" the cursor position.
 "#x": For example, "6x" means to delete the 6 characters "behind" the cursor position.
 "X": Capital X, each time you press it, delete the character "before" the cursor position.
 "#X": For example, "20X" means to delete the "first" 20 characters at the cursor position.
『dd』: Delete the line where the cursor is.
 "#dd": Delete # lines starting from the line where the cursor is located

5), copy
 "yw": Copy the characters from the cursor position to the end of the word into the buffer.
「#yw」:Copy # words to the buffer
「yy」:Copy the line where the cursor is to the buffer.
 "#yy": For example, "6yy" means to copy 6 lines of text "counting down" from the line where the cursor is located.
 「p」: Paste the characters in the buffer to the position of the cursor. Note: All copy commands related to "y" must be combined with "p" to complete the copy and paste functions.

6), Replacement
「r」: Replace the character where the cursor is.
『R』: Replace the character where the cursor is until the "ESC" key is pressed.

7), return to the previous operation
"u": If you execute a command by mistake, you can press "u" immediately to return to the previous operation. Press "u" multiple times to perform multiple replies.

8), change
"cw": Change the word where the cursor is to the end of the word
"c#w": For example, "c3w" means changing 3 characters

9), jump to the specified line

"ctrl" "g" lists the line number of the line where the cursor is located.
 "#G": For example, "15G" means moving the cursor to the beginning of the 15th line of the article.

Introduction to commands in Last line mode

Before using "last line mode", please remember to press the "ESC" key to confirm your After you are already in "command mode", press the ":" colon again 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 file.

B) Jump to a certain line in the file

"#": The "#" sign represents a number. Enter a number after the colon and press the Enter key to jump to that line. Okay, if you enter the number 15 and press Enter, it will jump to the 15th line of the article.

C) Search for characters

"/Keyword": Press the "/" key first, and then enter the character you want to find. If the keyword you are looking for for the first time is not what you want , you can keep pressing "n" to search further until you find the keyword you want.

"?Keyword": Press the "?" key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing "n" to move forward. Find the keyword you want.

D) Save the file

"w": Enter the letter "w" in the colon to save the file.

E) Exit vi

"q": Press "q" to exit. If you cannot leave vi, you can force "!" to leave vi after "q".

『qw』: It is generally recommended to use it with "w" when exiting, so that the file can be saved when exiting.

vi command list

1. The following table 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 up one line

j: Move the cursor down one line

^: The cursor moves to the beginning of the line

0: The number "0", the cursor moves to the beginning of the article

G: The cursor moves to the end of the article

$: Move the cursor to the end of the line

Ctrl f: Scroll forward

Ctrl b: Turn the screen backward

Ctrl d: Turn half the screen forward

Ctrl u: Turn half the screen backward

i: In front of the cursor position Insert characters

a: Begin to add the character after the cursor position

o: Insert a new line and input from the beginning of the line

ESC: Exit from the input state To command state

x: Delete the characters after the cursor

#x: Delete # characters after the cursor

X: (Capital X), delete the characters before the cursor

#X: Delete # characters before the cursor

dd: Delete the line where the cursor is

#dd: Delete # lines from the line where the cursor is

yw: Copy a word at the cursor position

#yw: Copy # words at the cursor position

yy: Copy a line at the cursor position

# yy: Copy # lines from the line where the cursor is

p: Paste

u: Cancel the operation

cw: Change a word at the cursor position

#cw: Change the # characters at the cursor position

2. The following table lists some instructions in the line command mode

w filename: Save the file being edited The file is filename

wq filename: Save the file being edited as filename, and exit vi

q!: Abandon all modifications, exit vi

set nu: Display the line number

/or?: Search, enter what you want to find after /

n: Used together with/or?, if the search content is not the keyword you want to find, press n or Continue searching backward (used with /) or forward (used with ?) until found.

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

1. After opening a file with vi, it is in "command mode" )", you need to switch to "Insert mode" to enter text. Switching method: Click the letter "i" in "command mode" to enter "Insert mode". At this time, you can start inputting text.

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

3. Save and exit the file: Enter: wq in command mode! (Don’t forget the one in front of wq)

(Recommended learning: linux tutorial)

The above is the detailed content of Detailed explanation of vi command under linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1236
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

See all articles