


Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)
Text file editing command: vim
1. Three status modes of vim
1) Command mode (default)
Note: Enter ":q!" in command mode to exit vim.
2) Editable mode (enter editable mode by entering lowercase "i" in command mode; press Esc to exit to command mode)
Remarks: Lowercase i, insert at the current cursor; lowercase a, insert after the current cursor.
3) Last line mode (enter through command mode, press Esc to exit to command mode)
Function: Execute non-text editing commands on the last line of the file, save, open the file, write the file name, etc.
For example, ":w" is used to fill in the file name;
For example, the function of ":wq" is to save and exit after filling in the file;
For example "/", search from top to bottom;
For example "?", search from bottom to top;
2. Basic operations of vim
1) Open the file
vim file name will open and enter command mode.
2) Edit file
In command mode, enter lowercase i to enter editing mode;
3) Save the file and exit
After editing is completed, press Esc to return to the command mode and enter ":w" (add the file name if there is no file name);
Enter: q to exit.
3. Vim editing operation (in command mode)
1) Copy operation
Lowercase "yy" copies a single line;
"2yy" copies the 2 lines at the cursor;
……
"nyy" copies the cursor out of n lines.
2) Paste operation
Lowercase "p" is pasted to the next line under the current cursor;
Capital "P" is pasted to the next line under the current cursor;
3) Delete operation
"dd" deletes the current line
"2dd" deletes the current two lines at the cursor;
……
4) Add line operation open
Lowercase "o", insert a blank line in the next line under the current cursor;
Capital "o", insert a blank line in the previous line at the current cursor;
4. Vim’s search and replace operation (in last line mode)
1) Search operation
Function: Find lines containing keywords;
Input "/search object" in command mode to search from top to bottom, press n to search (next);
Input "? Search object" in command mode to search from top to bottom, press n to search (next);
2) Replacement operation
Line replacement
: s/replaced object/new object/gc, press y after the prompt to complete the replacement.
Full text replacement
: %s/replaced object/new object/gc, follow the line prompts and press y to complete the replacement.
Enter: wq (save and exit).
5. Advanced operations of vim 1) Line number setting
Enter ":set nu" in the last line mode to display the line number of the file;
Enter ":set nonu" in the last line mode to turn off the display of file line numbers;
vi ~/.vimrc
2) Editing settings of ~/.vimrc
3) Concurrent operations of multiple files
This operation is convenient for copying and pasting multiple files to each other.
Command: vim file name 1 file name 2 file name 3...
Command: ":args" displays the names of multiple currently opened files at the bottom, [name of current file];
Command: ":next" switches to display the next document;
Command: ":prev" switches to display the previous document;
Command: ":first" switches to display the first document;
Command: ":last" switches the display of the last document.
4) Undo and restore operations
Note: Whether it is u to undo or ctrl r to restore, the premise is that the file cannot be saved.
Lowercase u: change before (undo the most recent action by row, step by step, in timeline units) until already at oldest change (can undo multiple steps);
Uppercase U: Undo only one step;
Ctrl r: change after (restore the undone action by row, step by step and in the timeline) until already at oldest change;
Ctrl R: Restore what was revoked, one step at a time;
How to copy a line, paste and delete a line of data in vi text editing under linux
When using vi, sometimes you want to copy a line of data directly, then paste a line or delete a line of data directly
How to copy a row of data
Put the cursor in front of the line you want to copy, and then press the yy letter key twice
Then place the cursor where you want to copy and press the p letter key on the keyboard
To delete a line, move the cursor to the line to be deleted and press the dd key twice
The following is a description of the relevant keys:
x,X: In a line, x is to delete one character backward (equivalent to the del key), and X is to delete one character forward (equivalent to the backspace key).
dd: Delete the entire line where the cursor is.
ndd: n is a number. Starting from the cursor, delete n columns down.
yy: Copy the line where the cursor is.
nyy: n is a number. Copy n lines down where the cursor is.
p,P: p means to paste the copied data to the line below the cursor, and P means to paste it to the line above the cursor.
u : Undo the previous operation
CTRL r: Redo the previous operation.
Decimal point '.': Repeat the previous action.
Vim common commands (delete, copy, paste, undo, search, insert, cursor movement, select, save, exit)
Basic operations of vim:
Select text
v Starting from the current position of the cursor, the place where the cursor passes will be selected, and then press v to end.
V Starting from the current line of the cursor, all lines passed by the cursor will be selected. Press V again to end.
ctrl v Starting from the current position of the cursor, select the rectangular area formed by the starting point and end point of the cursor, and then press Ctrl v to end.
ggVG selects all the text, where gg means jumping to the beginning of the line, V means selecting the entire line, and G means the end
Delete, copy, paste, undo
d Delete dd Delete the entire line ndd Delete n lines
x Delete a character
u Undo the last operation
ctrl R Undo (undo the undo operation)
y copy (copy to register)
ppaste (default taken from register)
Commonly used ESC first
i Insert text before cursor
a Insert text
after the cursor
o Start a new line below and change the current mode to Insert mode
O (capital O) will start a new line above the current line
:q exit
:q! Force quit
:wq Save and exit
ZZ Save and exit
/ Simple search /pp Search pp
in the file
Move command
$ Move the cursor to the end of the line 2$ Move to the end of the next line n$ Move to the end of the next n lines
^ Move the cursor to the first non-blank character of the current line
0 (number 0) moves the cursor to the first character of the current line
G Move the cursor to the last line. 33G Move the cursor to line 33
gg jump to the first line
Vim Select All Copy Paste Undo Back Operation
Delete all: After pressing the esc key, first press gg (reach the top), then dG
Copy all: After pressing the esc key, first press gg, then ggyG
Select all and highlight: After pressing the esc key, first press gg, then ggvG or ggVG
Single line copy: press esc key, then yy
Single line deletion: press esc key, then dd
Paste: After pressing esc key, then p
Copy to pasteboard: After selecting all and highlighting, ctrl shift c,
Problem that vim can only paste 50 lines:
Edit ~/.vimrc in the current user's home directory (if it does not exist, create a new file) and add a line
:set viminfo='1000,
As for why you need to enter '1000, this is actually not important. The most important thing is to enter
Press u in vim to undo an operation
u Undo the previous operation
Ctrl r restores the previous undone operation
Notice:
If you type "u" twice and your text returns to the original, it should be that your Vim is configured in Vi compatibility mode.
Redo
If you undo too much, you can type CTRL-R (redo) to roll back the previous command. In other words, it undoes an undo. To see an example of execution, enter CTRL-R twice. The character A and the space after it appear:
young intelligent turtle
There is a special version of the undo command: "U" (line undo). The line undo command undoes all previously edited lines
operations on. Enter these commands twice to cancel the previous "U":
A very intelligent turtle
xxxx delete very
A intelligent turtle
xxxxxx Delete turtle
A intelligent
Use "U" to restore row
A very intelligent turtle
Undo "U" with "u"
A intelligent
The "U" command changes itself, the "u" command undoes the operation, and the CTRL-R command redoes the operation. This is a bit messy, but no
Worry, you can switch to any state with the "u" and CTRL-R commands.
Popular text editors often have forward and back features that allow you to move back and forth between previously viewed locations in a file. In vim, use Ctrl-O to go backward and Ctrl-I to go forward.
Related help: :help CTRL-O :help CTRL-I :help jump-motions
The above is the detailed content of Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations). For more information, please follow other related articles on the PHP Chinese website!

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

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.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool