How to delete text matching in VIM

Source: Internet
Author: User

For example:

1. How to delete other rows by retaining only Matching content rows?

: V/pattern/d

: Help: V


2. How to keep Matching content for each row and delete other content in this row

: % S/^. * \ (pattern \). * $/\ 1/g


3. Delete rows containing specific strings

: G/pattern/d


4. delete a row containing a specific string. A prompt is displayed before each deletion.

: % S/^. * pattern. * \ n/C


5. Processing string:/123/456/789/109/example.txt. How can I delete it to the last one/and then get example.txt?

0dte

Explanation: 0 to the beginning of the line

Delete DTE to the First E


6. Processing string:/123/456/789/EF/109/example.txt. How can I delete it to the last one/and then get example.txt?

$ T/D0

Explanation: $ to the end of the line

T/search for the first one from the back/

D0 Delete to the beginning of a row

D/ex and press ENTER

Explanation: D Delete

/Ex found the first ex

Delete full row matching

If the character string starting with a specific row is datetime, you can delete this row in batches by using Vim to open the log file. In command mode, enter the following on the keyboard:

: 0, % S/^ datetime. * // G

In normal mode (Press ESC to enter), press V to enter visualization mode, and then press the left and right keys on the keyboard or H, and press the L key to select text.
Other related commands:
V: select by character. Frequently used mode, so try it yourself.

V: select by row. This is especially useful when you want to copy or move many lines of text.

CTRL + V: select by block. Very powerful. This function is available only in a few editors. You can select a rectangle and the text in the rectangle is highlighted.

It is worth noting that if Vim uses an automatic line feed, VIM regards the content you entered before you press enter, instead of several lines you see, when selecting by block, the block is selected based on the rows in vim.

When selecting a mode, use the arrow keys and commands described above (motion ). For example, vwww will highlight the three words behind the cursor. Vjj will highlight the current line and the following two lines.


Select all methods

V + G + $ select all, but only when the cursor is in the first line of the first letter, and ggvg does not need to be positioned in the first 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: info-contact@alibabacloud.com 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.