-->
Previous | Table of Contents | Next |
Finding a word, a phrase, or a number in a file can be difficult if you have to read through each line yourself. Like most editors and word processors, emacs has a command that allows you to search for a string of characters and, if you want, replace those characters with others. You can search forward or backward from your current position in the buffer. You also can continue searching. emacs starts searching from the beginning of the buffer file when it reaches the end, and vice versa. Table 28.2 summarizes the commands for searching. In each case, emacs searches for the string you specify, in the direction you specify, and positions the cursor at the beginning of the string.
Command | Action |
---|---|
<Ctrl-s> | Search forward from current position |
<Ctrl-r> | Reverse search from current position |
<Ctrl-x><s> | Repeat search forward |
<Ctrl-x><r> | Repeat search in reverse |
<Esc><r> | Replace all instances of first typed string in mini-buffer with second typed string, ending each string with <Esc> |
<Esc><Ctrl-r query> | Before performing the replacement, answer in the mini-buffer with one of the following: <Ctrl-g>: Cancel operation <!>: Replace the rest <?>: Get a list of options <.>: Replace and exit to where command was initiated <,>: Replace the rest without asking <y> or Spacebar: Replace and continue with replace operation <n>: Dont replace but continue with operation |
Searching
When you type the search command, it appears in the mini-buffer. To search forward for the string sales > 100K in a file, use this command:
<Ctrl-s>sales > 100K
This command starts an incremental search through the buffer. Notice that as you type the characters for the search string, emacs positions the cursor on the sequence of characters. If emacs cant find the text, it displays the search failed message. If the string is in the buffer, emacs positions the cursor under the first s in the word sales. When you find the first occurrence of the string, you must press <Esc> to stop the search; otherwise, emacs continues looking for a match as you enter other text. emacs refers to these types of searches as incremental searches ; emacs searches as you enter the search string.
emacs can also perform non-incremental searches if you preface the search string with the <Esc> key and press the <Return> key at the end, as shown here:
<Ctrl-s><Esc>sales > 100K
If youre searching a large file and realize that youve entered the wrong search string, emacs searches the entire file. To stop the search, press <Ctrl-g>.
TROUBLESHOOTING:
I typed a string I know exists in the file, but emacs cant find it. The most common cause for this error is that you typed the string incorrectly. emacsand computers in generaldont do a good job of thinking; emacs has a terrible time figuring out what you really mean. If youre looking for the string vegi-burger but you type vigi-burger, emacs cant find what you want (unless you happened to misspell vegi-burger in the buffer and it matches the search string). Check the search string carefully before you press <Return>.
Replacing
Although searching for text can help you locate a particular word or section of text, many times you want to replace the found text. An example is if you find a spelling error and want to correct the entire buffer rather than one mistake at a time. For example, to replace every occurrence of the word misstake with mistake, press <Esc><r>. The mini-buffer prompts for the string to search for; enter misstake. emacs then prompts for the replacement string; enter mistake. emacs proceeds through the file, looking for the string misstake and replacing it with mistake. emacs also tries to match capitalization as best as possible. Thus, if misstake appears as Misstake, emacs replaces it with Mistake.
Maybe you dont want to replace every occurrence of a search string with the replacement string, in which case you can instruct emacs to query before replacing the string. To have emacs query and replace, press <Esc><Ctrl-r>.
For example, if you want to selectively replace the name of your operating system, Linux, with its ancestor UNIX, press <Esc><Ctrl-r>. emacs responds in the mini-buffer with Query replace:. Now enter the search string Linux. The Query replace Linux with: prompt appears. Enter the replacement string UNIX. emacs begins the search and states, Query replacing Linux with UNIX. If you want to terminate the search-and-replace operation, press <Ctrl-g>.
Previous | Table of Contents | Next |