Tips for VIM replacement

Source: Internet
Author: User

1. Convert Enum in C language to Java constant:

G/^ \ s * $/d
G/^/S // public static final int/

Leti = 1000
G/,/S // \ = '. I.'; '/| let I = I + 1

2. # define macro in C to convert Java constants:

% S/^ # define [A-Z, 0-9, _] * \ Zs \ s \ ze/=/
G/# define/S // public static final int/
G/$/S //;/

 

 

Some advanced replacement techniques in VIM

1. Replace Variables
In a regular expression, use the \ (and \) symbol to enclose a regular expression. Then, use variables such as \ 1 and \ 2 to access the content in \ (and.
Example:
· Change data1 data2 to data2 data1
-----------------------------------
: S/\ (\ W \ + \) \ s \ + \ (\ W \ + \)/\ 2 \ t \ 1
-----------------------------------
· Change Doe and John to john doe
-----------------------------------
: % S/\ (\ W \ + \), \ (\ W \ + \)/\ 2 \ 1/
-----------------------------------
2. Use Function replacement
Usage:
---------------------------
: S/replacement string/\ = Function Type
---------------------------
Note:
· There can be multiple function types, and the return value can be a string connector. connect, such as line (". ") returns the matched row number (: Help Line (). submatch (n) can reference the content of \ 1 and \ 2. submatch (0) references the entire content of the matching;
· The function can also be a String constant, which is caused by double quotation marks. Function expressions can also be arbitrary expressions that need to be caused by parentheses, such as (3 + 2*6 );
· The function type can also be the content in the register, accessed through "@ register name", such as @ A (no quotation marks are required, but still need to be connected using );
Example:
· Convert the following eight rows of data0 to data0 in sequence ~ 7 (the preceding number is the row number)
Double data0;
Double data0;
Double data0;
Double data0;
Double data0;
Double data0;
Double data0;
Double data0;
If Perl is installed, I used Perl to do this:
---------------------------------------------------------------------------
: R! Perl-n-Le "If (S/^ (double \ s + Data) \ D;/$1 $ K;/) {$ K ++; print $ _;} "%: P ---------------------------------------------------------------------------
%: P indicates the file name that contains the complete path.
Now we can do this with vim:
First, move the cursor to line 125 (line (".") to return the row number of the current matched row, line ("'a") to return the row number of Mark ):
---------------------------------------------------------------------------
Ma
: % S/^ \ (double \ s \ + data \) \ D \ (; \)/\ = submatch (1 ). (line (". ")-line (" 'a ") + 1 ). submatch (2)/g
---------------------------------------------------------------------------
Or
First, move the cursor to line 125 ("'v7j
: % S/^ \ (double \ s \ + data \) \ D \ (; \)/\ = submatch (1 ). (line (". ")-line ("'
---------------------------------------------------------------------------
3. start and end of matching
\ Zs and \ ze can be used to specify the start and end of replacement in the replacement operation. In the above example, they can be implemented with shorter operations:
---------------------------------------------------------------------------
Ma
: % S # ^ double \ s \ + data \ Zs \ D \ ze # \ = (line (".")-line ("'a") + 1) # G
---------------------------------------------------------------------------
In addition, you can use the VIM expression to implement the above functions:
---------------------------------------------------------------------------
: Let n = 0 | G/^ double \ s \ + data \ Zs \ D/S // \ = N/| Let N + = 1
---------------------------------------------------------------------------
Where | is used to separate unused commands;
· G executes the specified ex command in the line matching the following pattern
· \ Zs indicates that the matching starts from this
· S // \ = N: Replace the matching mode. If the matching mode is null, the matching mode is successful,
It also indicates that the replacement is an expression;
4. Advanced incremental replacement
Put the following sentence in _ vimrc
-------------------------------
Let G: I = 0
Function! INC (increment)
Let G: I = G: I + A: Increment
Return G: I
Endfunction
------------------------------
· The preceding problems can be solved as follows:
-----------------------------------------------------
: Let I =-1 | % S/^ double \ s \ + data \ Zs \ D \ ze/\ = Inc (1 )/
-----------------------------------------------------
· You can also generate digital sequences, such as generating 1 ~ A numerical sequence separated by 5 at 100:
--------------------------------------
: Let I = 0 | 'a, 'B S/^/\ = Inc (5 )/
--------------------------------------
Http://hi.baidu.com/%C1%EE%BA%FC%D2%BB%B6%FE/blog/item/5ae257d82a7cb8e339012fc3.html

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.