Friday, September 29, 2006

Trick in query-replace-regexp

使用 query-replace-regexp 的小技巧

query-replace-regexp C-M-%

eg. a). delete a blank line

C-M-% ^\s-* C-q C-j [RET][RET]

^ means beginning of a line.
\s- means any white space.
C-q C-j is to insert a new line in the minibuffer. Likely wise, you can use C-q C-i to insert a TAB, etc.
Note, there should be no space between "^\s-*" and "C-q C-j" in the this example!

eg. b). Insert a series of continued number in the beginning of each line.

C-M-% ^ [RET] \# [RET]

\# means the number of replacements, starting with zero.

eg. a). change (2) (3) (4) (5) ... into (4) (5) (6) (7) ...

C-M-% (\([0-9]+\)) [RET] (\,(+ \#1 2)) [RET]

\#1 means convert the match \1 into number. The match \1 is string by default.
\, followed by a lisp expression. So from here you can do almost whatever you want. Yeah!
del.icio.us Tags:

No comments: