Êíèãà: Learning GNU Emacs, 3rd Edition
3.1.1 Incremental Search
3.1.1 Incremental Search
Incremental search starts to work from the moment you type the first character of the search string. Many users like the efficiency of incremental searches, and they like the highlighting as well. Emacs highlights all occurrences of the search string in aqua blue (if your display supports it) and uses purple to highlight the string at the cursor position (the current match).
Type: C-s m
Emacs highlights all the words that start with m.
To start an incremental search, type C-s and then type the text you want to find. Emacs temporarily enters Isearch mode. Notice how this search works: Emacs looks for each character as soon as you type it. For example, if you are searching for the word meter, in an incremental search Emacs finds the next m as soon as you type the m; it finds the next me as soon as you type the e; it finds the met as soon as you type the t; and so on. Sooner or later, you either find what you want, or Emacs is unable to find anything. If you find what you want, press Enter; doing so stops the search at the current place in the file. If Emacs can't find anything that matches your search string, it prints the message Search failed
at the bottom of your screen and then it beeps.
Here's what happens when we search for the word meter; the numbers show how the cursor moves with each new letter in the search string.
Type: C-s meter
Emacs moves the cursor from one position to another as you type the letters of the search string.
In this incremental search, Emacs moves the cursor from position 1 to 2, to 3, and so on, as you type the search string meter. Also, note that Isearch
appears on the mode line.
What happens if you find the string you're looking for but not the right occurrence of the string? Let's say you're searching for the word eschatology and you find the word, but you're still not in the right place. Simply press C-s again to find the next occurrence of the current search string. Emacs uses the same search string; you don't have to retype it.
Remember to press Enter when you've found the text you want. Forgetting to stop the search (by pressing Enter or with any other cursor movement command) is a common mistake: you type a few things, and suddenly Emacs is off looking at some completely different part of the file. What has happened? Emacs thinks you're still searching, and it has just added the characters you've typed to the search string.
If you type a letter in your search string incorrectly, press Del: Emacs moves back to the first instance of the reduced string in the file. If you keep pressing Del to delete characters from the search string, you'll see Emacs cycle back through the file to previous matches.
To cancel a search (that is, to give up searching), type C-g. This command brings you back to the place where the search began.
To search backward through a file, use C-r, which works exactly like C-s except that it searches in the opposite direction. It puts the cursor at the beginning of the text you find. Just as you can do when repeating C-s, you can press C-r to make the search go in the other direction without retyping the search string.
To avoid typing your search string, you can copy text from the buffer into the search string. To copy text from the cursor position through the next space or punctuation mark into the search string, type C-s C-w (it may help to think of C-s C-w as "search a word"). To copy text from the cursor to the end of the line into the search string, type C-s C-y. Notice that the text that is yanked is always converted to lowercase; this conversion ensures that the search will be case-insensitive. You can also copy text from the kill ring to the search string by typing C-s M-y. After you've given this command, you can press M-p to see previous items from the kill ring. M-n takes you to the next item if you've gone back with M-p.
Once you're in an incremental search, certain keys (such as Enter and Del) have different functions than they normally do. This situation may sound confusing, but it's actually fairly easy to get used to. Table 3-1 shows a summary of key functions during incremental search.
Table 3-1. Incremental search commands
Keystrokes | Command name | Action |
---|---|---|
C-s Edit ? Search ? Incremental Search ? Forward String | isearch-forward | Start incremental search forward; follow by search string. Also, find next occurrence (forward) of search string. |
C-r Edit ? Search ? Incremental Search ? Backward String | isearch-backward | Start incremental search backward; follow by search string. Also, find next occurrence (backward) of search string. |
Enter | isearch-exit | In an incremental search , exit the search. |
C-g | keyboard-quit | In an incremental search , cancel the search. |
Del | isearch-delete-char | In an incremental search, delete character from search string. |
C-s C-w | isearch-yank-word | Start an incremental search with the word the cursor is on as the search string. |
C-s C-y | isearch-yank-line | Start an incremental search with the text from the cursor position to the end of the line as the search string. |
C-s M-y | isearch-yank-kill | Start an incremental search with text from the kill ring as the search string. |
C-s C-s | isearch-repeat-forward | Repeat previous search. |
C-r C-r | isearch-repeat-backward | Repeat previous search backward. |
- 3.2.6 Regular Expressions for Search and Replacement Operations
- 3.1 Different Kinds of Searches
- 3.1.2 Simple Searches
- 3.1.3 Word Search
- Table A-2. Searching and replacing
- Table E-6. Search and replace commands
- Table E-7. Regular expression search commands
- Searches
- Searching with Beagle
- Finding Files by Searching with find
- Searches for a String in Input with grep