Home System Tutorial LINUX 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)

Jan 02, 2024 pm 10:10 PM
linux vim text editor

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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

How to start monitoring of oracle How to start monitoring of oracle Apr 12, 2025 am 06:00 AM

The steps to start an Oracle listener are as follows: Check the listener status (using the lsnrctl status command) For Windows, start the "TNS Listener" service in Oracle Services Manager For Linux and Unix, use the lsnrctl start command to start the listener run the lsnrctl status command to verify that the listener is started

How to set up a recycling bin in Debian system How to set up a recycling bin in Debian system Apr 12, 2025 pm 10:51 PM

This article introduces two methods of configuring a recycling bin in a Debian system: a graphical interface and a command line. Method 1: Use the Nautilus graphical interface to open the file manager: Find and start the Nautilus file manager (usually called "File") in the desktop or application menu. Find the Recycle Bin: Look for the Recycle Bin folder in the left navigation bar. If it is not found, try clicking "Other Location" or "Computer" to search. Configure Recycle Bin properties: Right-click "Recycle Bin" and select "Properties". In the Properties window, you can adjust the following settings: Maximum Size: Limit the disk space available in the Recycle Bin. Retention time: Set the preservation before the file is automatically deleted in the recycling bin

How to restart the apache server How to restart the apache server Apr 13, 2025 pm 01:12 PM

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

See all articles