search
HomeSystem TutorialLINUXLearn 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

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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);

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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);

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

Input "? Search object" in command mode to search from top to bottom, press n to search (next);

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

2) Replacement operation

Line replacement

: s/replaced object/new object/gc, press y after the prompt to complete the replacement.

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

Full text replacement

: %s/replaced object/new object/gc, follow the line prompts and press y to complete the replacement.

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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;

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

Enter ":set nonu" in the last line mode to turn off the display of file line numbers;

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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.

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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;

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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;

Learn the basic operations of using vim editor on Linux (copy, paste, replace, line number, undo, multi-file operations)

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!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

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.

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.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

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.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

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.

Boost Productivity with Custom Command Shortcuts Using Linux AliasesBoost Productivity with Custom Command Shortcuts Using Linux AliasesApr 12, 2025 am 11:43 AM

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

What is Linux actually good for?What is Linux actually good for?Apr 12, 2025 am 12:20 AM

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.

Essential Tools and Frameworks for Mastering Ethical Hacking on LinuxEssential Tools and Frameworks for Mastering Ethical Hacking on LinuxApr 11, 2025 am 09:11 AM

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

How to learn Linux basics?How to learn Linux basics?Apr 10, 2025 am 09:32 AM

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.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

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

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

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool