Describes how to replace or delete a copy in Linux Vi.

Source: Internet
Author: User

Vi deletion is worth learning a lot. Here we mainly introduce how to delete Linux Vi, including Vi deletion. In command mode, you can use Linux Vi to delete related commands, including Linux Vi to delete, copy, replace, and replace text.

Delete a text Linux Vi

When editing text, we often need to delete unnecessary text in Linux Vi. The key will be deleted by mistake or unnecessary text, but there is a limit that it is impossible to delete the content of the above line after the line header is deleted. In command mode, Vi provides many Linux Vi deletion commands. Most of these commands start with d. Commonly used:

Linux Vi deletes a single character

X: Delete the character at the cursor. If a number n is added before x, n characters starting from the cursor position are deleted to the right.
X: Delete the character before the cursor. If n is added before X, n characters starting from the character before the cursor are deleted.
Obviously, these two commands are shortcuts to delete a small number of characters.

Linux Vi deletes multiple characters

Dd: Delete the entire row where the cursor is located. Add a number n before dd to delete the content of the current row and the next n-1 row.
D Or d $: The two commands have the same function. They delete the content starting from the cursor position to the end of the line.
D0: Delete content from the beginning of the cursor to the beginning of the line.
Dw: delete a word. If the cursor is in the middle of a word, it is deleted from the cursor position to the end of the word. Like the dd command, you can add a number n before dw to delete n specified words.

If the user accidentally deletes the Linux Vi, it does not matter. Vi provides a command to restore the misoperation, and can move the recovered content to any part of the text. The recovery command uses 2 np, where n is the register number. This is because Vi has nine internal registers used to maintain the delete operation, which are represented by numbers 1, 2, 9, respectively, and they save the previously deleted content using the dd command. These registers form a queue. For example, the content deleted by the last dd command is put into register 1. When you use the dd command to delete text content the next time, vi transfers the content of register 1 to register 2, and register 1 will be deleted by the last dd command. Similarly, Linux Vi deletes the content that can be deleted using the dd command for the last nine times, while the content deleted using the previous dd command is discarded.

Assume that the current edited file is xu. c.
/* This is a example */
# Include
Void main ){
Int I, j;
Printf "please input a number:/n ");
Scanf "% d", & I );
J = I + 100;
Printf "/n j = % d/n", j );

Perform the following operations:
1. move the cursor to the first line of the file and press the dd command. At this time, the content of the first line of the file is deleted by Linux Vi, and the deleted content is saved in register 1;
2. Press 5j to move the cursor down to the first printf statement line;
3. Press the dd command to delete the row. In this case, register 1 will save the deleted content:
Printf "please input a number: \ n ");
The original content of register 1:
/* This is a example */

Is saved to register 2;

In the last row mode, you can also delete the file content in Linux Vi, but it can only delete the entire row. You can delete the starting line number within a specified range at a time) delete all rows. Note that Vi does not put the deleted content into the register when using this method. Therefore, if a Linux Vi deletion operation is incorrect, it cannot be restored using the 2 np command, only the u command can be used for limited recovery. Finally, we will mention how to delete the entered text in text input mode. Use You can combine the keys. At this time, the cursor returns the starting position of the insert operation, and Vi is still in the text input mode.

  1. Linux vi editor concepts
  2. Sunflower collection in linux vi Editor
  3. Describes how to use the basic commands of the linux vi editor.
  4. Linux vi command replacement instructions
  5. Tips for adding a row number to a linux vi command line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.