Little VI experience

Source: Internet
Author: User

Edit:
Move the next row to the end of the current row. In Windows, it is very simple-press del at the end of the current row or BS at the beginning of the next row,
In Vi, you need to pressJ(Uppercase J, that is, Shift + J)

Saved quick launch:
Many people are used to the standard: WQ! Save and exit, but I prefer to press SHIFT + Z, that is, ZZ.


Search:
/Sometext find from the current back
? Sometext looks forward from the current time (after the two commands are executed, press n to continue searching backward and then press n to continue searching forward)
: G/sometext/

The former is directly located on the word, and the latter is only located on that line.

 

Ex command extension:

In VI, there is an ex command mode that can be used to quickly edit operations. The basic mode is:

[Range] G [lobal]/mode/[ex command] in the specified"Range", Matching"Mode"To execute"Ex command"

[Range] V [lobal]/mode/[ex command] in the specified"Range"InternalNoMatch"Mode"To execute"Ex command"

    • If "range" is not specified, the default value is all.
    • If the "ex command" is not specified, the result is displayed by default.
    • "Mode" is a regular expression.

In terms of editing the "ex command", there are roughly M (mobile) S (replace) D (delete)

Example:

: G/^/M0 moves each row from the first line to the first line. The result is that all rows are reversed in reverse order.

: G/127.0.0.1/S/HTTP/FTP/g for lines containing 127.0.0.1, replace HTTP with FTP

: G/127.0.0.1/S/^/\ t/g. Add a tab before each line to the row containing 127.0.0.1)

: G/127.0.0.1/d Delete rows containing 127.0.0.1

 

If the command content contains/characters, you can select escape \/or change the command separator/of ex to another character (for example ~ @ # $ % ).For example, to delete/Simon from a row containing/home:

: G/\/home/S/\/Simon/g Escape Character Input/

: G #/home # s #/Simon # G use # As the command separator, then the character/can be used directly

 

Search-replace:
Normally, the replacement ex command is
: % S/oldtext/newtext/g

The following is an explanation of this command
: Indicates entering the command mode of ex.
% Indicates the source of the ex commandRange.The following commands are executed in this range. There are multiple expressions in this range:
% Indicates full text
$ Indicates the end of the row.
. Indicates the current row
X, Y indicates from row X to row y, for example, 10, 20 s/oldtext/newtext/
In addition, you can use other ex search commands to express the range.
G/sometext/indicates the row containing sometext

S is exReplaceCommand
/.../Indicates the content of "search-Replace.
G indicatesReplace allBy default, only one execution is performed in seconds.

-= Example 1 =-
: 10th S/^ // Add four spaces in front of rows to 20
Can be divided into: 10, 20 s/^ //

-= Example 2 =-
: G/small/S/^/#/Add comments to all rows that contain small #
G/small/find rows containing small
/^/Indicates the first non-blank character
It can be divided into g/small/S/^ /#/


 

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.