dimanche 2 mai 2010

Search and Replace

search for works没有搞懂

以在下面一段文字中搜索第五行的ab为例:
------
1 a
2 ab
3 abc
4 abcd
5 ab

M-<
%emacs搜索范围从光标位置处开始知道buffer结束。
%为了让搜索覆盖全局,需要使用 M-< 把光标调整到 buffer开始处。
C-s (C-r)
%向前搜索 (C-r向后搜索)
I-search:\
%如果刚开始的时候只输入想搜索单词的前面部分,光标会停留到第一个符合条件的位置,即 1 a C-s (C-r)
%光标移动到下一个符合条件的位置 2 ab
C-w
%which will append the rest of the word to the search string
C-s
%go to 3 abc with this modified search string
C-s
%go to 4 abcd with this modified search strin
C-s
%go to 5 ab with this modified search strin Enter %press the Enter key to exit the incremental search.

Exiting a Search
C-g C-g

Using an Old Search String
C-s (C-r)
M-p (M-n)
%To do this, press either C-s or C-r to search through your search history, and then press M-p to search backward in the history of search strings. If you get too far back, you can press M-n to search forward once again. When you find the string that you want to use, you can modify it if it is not the exact string you want to search for. When you are happy about the string, you can start the search by pressing the Enter key.

Copy the text have marked
C-s (C-r)
M-y
%If you have marked some text and you want to append this to your search string, you can press M-y. This also means that if you have some marked text that you want to search for, you simply press C-s M-y.

Listing Lines that Match a Pattern
C-2 M-x occur RET
ab RET
%If you give this function a numerical prefix (that is, by pressing C-3 before the function) then that many lines of context are shown.
%The screen is split in two--in the upper part your original buffer is shown, and in the lower part, the special occur buffer is shown. If you press Enter or the middle mouse button over one of these matches, Emacs will proceed to this location in your original buffer.

Search and Replace
M-%
Options when Replacing

For each occurrence of the search string, Emacs offers to do one of several actions. Previously, you saw two of them: Replace this occurrence and replace the rest of the occurrences.

The following choices exist:

Space or y--This replaces the current match and continues to the next one.

Delete or n--This skips the current match and continues to the next one. In other words, it doesn't replace the current match.

Enter or q--This terminates the query-replace operation and, therefore, doesn't replace any more matches. The matches already replaced will remain replaced.

!--This replaces the remaining occurrences in the rest of the buffer without asking for permission.

, (the comma character)--This replaces the current match but doesn't continue to the next one. That way, you can get the match replaced and see whether Emacs does it correctly. To continue on to the next match, press y. If you dislike the replacement, you can either abort the query-replace and undo, or use recursive editing to fix it. (Recursive editing is described in the following section)

C-g--This aborts the search-and-replace operation.

^ (the caret character)--This moves the cursor back to the previous replacement. (You can go back as many steps as you want.) To continue your replacement press y. If you want to correct the previous replacement, (you must use recursive editing).

C-l--This centers the line with the match in the middle of the window. This enables you to see the text around the match.

C-r--This enters recursive editing, which is described in the following section.

Recursive Editing

When you use query-replace in your buffer, you might find that there is an error of some kind in the surrounding text of the word being replaced. You can do several things about that.

You can try to remember the location and go back to it, when you are finished with your search-and-replace. Unfortunately, this method works only if you have a superb brain like Einstein; otherwise, you will forget the location when you are finished with your search-and-replace. An alternative is to write the location down. This is better than the first solution, but if your document is large, you might end up with many such notes on your paper, and when you get back to the location, you will find that you have forgotten what the problem was.

The ultimate solution is to fix the problem immediately and continue your search-and-replace afterward.

Totally aborting your search-and-replace operation is not a good idea, because this means that you might have to tell Emacs to skip several replacements when you start search-and-replace again. What you really need is to tell Emacs to pause the search-and-replace operation, fix the problem, and ask Emacs to continue after you finish.

Fortunately, Emacs offers exactly that capability. It is called recursive editing .

To enter recursive editing from your search-and-replace operation, press C-r. When you finish that, you can work with Emacs as you normally do. (You can, in fact, start another search-and-replace operation while the first is pausing; watch out if you do, because things might get messy).

When you enter recursive editing, the modeline includes a pair of square brackets around the parentheses to indicate that you are in recursive editing.

To exit recursive editing and continue with your search-and-replace press C-M-c.

Aucun commentaire:

Enregistrer un commentaire