search.texi 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. @node Search, Fixit, Display, Top
  2. @chapter Searching and Replacement
  3. @cindex searching
  4. Like other editors, Emacs has commands for searching for occurrences of
  5. a string. The principal search command is unusual in that it is
  6. @dfn{incremental}: it begins to search before you have finished typing the
  7. search string. There are also non-incremental search commands more like
  8. those of other editors.
  9. Besides the usual @code{replace-string} command that finds all
  10. occurrences of one string and replaces them with another, Emacs has a fancy
  11. replacement command called @code{query-replace} which asks interactively
  12. which occurrences to replace.
  13. @menu
  14. * Incremental Search:: Search happens as you type the string.
  15. * Non-Incremental Search:: Specify entire string and then search.
  16. * Word Search:: Search for sequence of words.
  17. * Regexp Search:: Search for match for a regexp.
  18. * Regexps:: Syntax of regular expressions.
  19. * Search Case:: To ignore case while searching, or not.
  20. * Replace:: Search, and replace some or all matches.
  21. * Other Repeating Search:: Operating on all matches for some regexp.
  22. @end menu
  23. @node Incremental Search, Non-Incremental Search, Search, Search
  24. @section Incremental Search
  25. An incremental search begins searching as soon as you type the first
  26. character of the search string. As you type in the search string, Emacs
  27. shows you where the string (as you have typed it so far) is found.
  28. When you have typed enough characters to identify the place you want, you
  29. can stop. Depending on what you do next, you may or may not need to
  30. terminate the search explicitly with a @key{RET}.
  31. @c WideCommands
  32. @table @kbd
  33. @item C-s
  34. Incremental search forward (@code{isearch-forward}).
  35. @item C-r
  36. Incremental search backward (@code{isearch-backward}).
  37. @end table
  38. @kindex C-s
  39. @kindex C-r
  40. @findex isearch-forward
  41. @findex isearch-backward
  42. @kbd{C-s} starts an incremental search. @kbd{C-s} reads characters from
  43. the keyboard and positions the cursor at the first occurrence of the
  44. characters that you have typed. If you type @kbd{C-s} and then @kbd{F},
  45. the cursor moves right after the first @samp{F}. Type an @kbd{O}, and see
  46. the cursor move to after the first @samp{FO}. After another @kbd{O}, the
  47. cursor is after the first @samp{FOO} after the place where you started the
  48. search. Meanwhile, the search string @samp{FOO} has been echoed in the
  49. echo area.@refill
  50. The echo area display ends with three dots when actual searching is going
  51. on. When search is waiting for more input, the three dots are removed.
  52. (On slow terminals, the three dots are not displayed.)
  53. If you make a mistake in typing the search string, you can erase
  54. characters with @key{DEL}. Each @key{DEL} cancels the last character of the
  55. search string. This does not happen until Emacs is ready to read another
  56. input character; first it must either find, or fail to find, the character
  57. you want to erase. If you do not want to wait for this to happen, use
  58. @kbd{C-g} as described below.@refill
  59. When you are satisfied with the place you have reached, you can type
  60. @key{RET} (or @key{C-m}), which stops searching, leaving the cursor where
  61. the search brought it. Any command not specially meaningful in searches also
  62. stops the search and is then executed. Thus, typing @kbd{C-a} exits the
  63. search and then moves to the beginning of the line. @key{RET} is necessary
  64. only if the next command you want to type is a printing character,
  65. @key{DEL}, @key{ESC}, or another control character that is special
  66. within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, or @kbd{C-y}).
  67. Sometimes you search for @samp{FOO} and find it, but were actually
  68. looking for a different occurrence of it. To move to the next occurrence
  69. of the search string, type another @kbd{C-s}. Do this as often as
  70. necessary. If you overshoot, you can cancel some @kbd{C-s}
  71. characters with @key{DEL}.
  72. After you exit a search, you can search for the same string again by
  73. typing just @kbd{C-s C-s}: the first @kbd{C-s} is the key that invokes
  74. incremental search, and the second @kbd{C-s} means ``search again''.
  75. If the specified string is not found at all, the echo area displays
  76. the text @samp{Failing I-Search}. The cursor is after the place where
  77. Emacs found as much of your string as it could. Thus, if you search for
  78. @samp{FOOT}, and there is no @samp{FOOT}, the cursor may be after the
  79. @samp{FOO} in @samp{FOOL}. At this point there are several things you
  80. can do. If you mistyped the search string, correct it. If you like the
  81. place you have found, you can type @key{RET} or some other Emacs command
  82. to ``accept what the search offered''. Or you can type @kbd{C-g}, which
  83. removes from the search string the characters that could not be found
  84. (the @samp{T} in @samp{FOOT}), leaving those that were found (the
  85. @samp{FOO} in @samp{FOOT}). A second @kbd{C-g} at that point cancels
  86. the search entirely, returning point to where it was when the search
  87. started.
  88. If a search is failing and you ask to repeat it by typing another
  89. @kbd{C-s}, it starts again from the beginning of the buffer. Repeating
  90. a failing backward search with @kbd{C-r} starts again from the end. This
  91. is called @dfn{wrapping around}. @samp{Wrapped} appears in the search
  92. prompt once this has happened.
  93. @cindex quitting (in search)
  94. The @kbd{C-g} ``quit'' character does special things during searches;
  95. just what it does depends on the status of the search. If the search has
  96. found what you specified and is waiting for input, @kbd{C-g} cancels the
  97. entire search. The cursor moves back to where you started the search. If
  98. @kbd{C-g} is typed when there are characters in the search string that have
  99. not been found---because Emacs is still searching for them, or because it
  100. has failed to find them---then the search string characters which have not
  101. been found are discarded from the search string. The
  102. search is now successful and waiting for more input, so a second @kbd{C-g}
  103. cancels the entire search.
  104. To search for a control character such as @kbd{C-s} or @key{DEL} or
  105. @key{ESC}, you must quote it by typing @kbd{C-q} first. This function
  106. of @kbd{C-q} is analogous to its meaning as an Emacs command: it causes
  107. the following character to be treated the way a graphic character would
  108. normally be treated in the same context.
  109. To search backwards, you can use @kbd{C-r} instead of @kbd{C-s} to
  110. start the search; @kbd{C-r} is the key that runs the command
  111. (@code{isearch-backward}) to search backward. You can also use
  112. @kbd{C-r} to change from searching forward to searching backwards. Do
  113. this if a search fails because the place you started was too far down in the
  114. file. Repeated @kbd{C-r} keeps looking for more occurrences backwards.
  115. @kbd{C-s} starts going forward again. You can cancel @kbd{C-r} in a
  116. search with @key{DEL}.
  117. The characters @kbd{C-y} and @kbd{C-w} can be used in incremental search
  118. to grab text from the buffer into the search string. This makes it
  119. convenient to search for another occurrence of text at point. @kbd{C-w}
  120. copies the word after point as part of the search string, advancing
  121. point over that word. Another @kbd{C-s} to repeat the search will then
  122. search for a string including that word. @kbd{C-y} is similar to @kbd{C-w}
  123. but copies the rest of the current line into the search string.
  124. The characters @kbd{M-p} and @kbd{M-n} can be used in an incremental
  125. search to recall things which you have searched for in the past. A
  126. list of the last 16 things you have searched for is retained, and
  127. @kbd{M-p} and @kbd{M-n} let you cycle through that ring.
  128. The character @kbd{M-@key{TAB}} does completion on the elements in
  129. the search history ring. For example, if you know that you have
  130. recently searched for the string @code{POTATOE}, you could type
  131. @kbd{C-s P O M-@key{TAB}}. If you had searched for other strings
  132. beginning with @code{PO} then you would be shown a list of them, and
  133. would need to type more to select one.
  134. You can change any of the special characters in incremental search via
  135. the normal keybinding mechanism: simply add a binding to the
  136. @code{isearch-mode-map}. For example, to make the character
  137. @kbd{C-b} mean ``search backwards'' while in isearch-mode, do this:
  138. @example
  139. (define-key isearch-mode-map "\C-b" 'isearch-repeat-backward)
  140. @end example
  141. These are the default bindings of isearch-mode:
  142. @findex isearch-delete-char
  143. @findex isearch-exit
  144. @findex isearch-quote-char
  145. @findex isearch-repeat-forward
  146. @findex isearch-repeat-backward
  147. @findex isearch-yank-line
  148. @findex isearch-yank-word
  149. @findex isearch-abort
  150. @findex isearch-ring-retreat
  151. @findex isearch-ring-advance
  152. @findex isearch-complete
  153. @kindex DEL (isearch-mode)
  154. @kindex RET (isearch-mode)
  155. @kindex C-q (isearch-mode)
  156. @kindex C-s (isearch-mode)
  157. @kindex C-r (isearch-mode)
  158. @kindex C-y (isearch-mode)
  159. @kindex C-w (isearch-mode)
  160. @kindex C-g (isearch-mode)
  161. @kindex M-p (isearch-mode)
  162. @kindex M-n (isearch-mode)
  163. @kindex M-TAB (isearch-mode)
  164. @table @kbd
  165. @item DEL
  166. Delete a character from the incremental search string (@code{isearch-delete-char}).
  167. @item RET
  168. Exit incremental search (@code{isearch-exit}).
  169. @item C-q
  170. Quote special characters for incremental search (@code{isearch-quote-char}).
  171. @item C-s
  172. Repeat incremental search forward (@code{isearch-repeat-forward}).
  173. @item C-r
  174. Repeat incremental search backward (@code{isearch-repeat-backward}).
  175. @item C-y
  176. Pull rest of line from buffer into search string (@code{isearch-yank-line}).
  177. @item C-w
  178. Pull next word from buffer into search string (@code{isearch-yank-word}).
  179. @item C-g
  180. Cancels input back to what has been found successfully, or aborts the
  181. isearch (@code{isearch-abort}).
  182. @item M-p
  183. Recall the previous element in the isearch history ring
  184. (@code{isearch-ring-retreat}).
  185. @item M-n
  186. Recall the next element in the isearch history ring
  187. (@code{isearch-ring-advance}).
  188. @item M-@key{TAB}
  189. Do completion on the elements in the isearch history ring
  190. (@code{isearch-complete}).
  191. @end table
  192. Any other character which is normally inserted into a buffer when typed
  193. is automatically added to the search string in isearch-mode.
  194. @subsection Slow Terminal Incremental Search
  195. Incremental search on a slow terminal uses a modified style of display
  196. that is designed to take less time. Instead of redisplaying the buffer at
  197. each place the search gets to, it creates a new single-line window and uses
  198. that to display the line the search has found. The single-line window
  199. appears as soon as point gets outside of the text that is already
  200. on the screen.
  201. When the search is terminated, the single-line window is removed. Only
  202. at this time the window in which the search was done is redisplayed to show
  203. its new value of point.
  204. The three dots at the end of the search string, normally used to indicate
  205. that searching is going on, are not displayed in slow style display.
  206. @vindex search-slow-speed
  207. The slow terminal style of display is used when the terminal baud rate is
  208. less than or equal to the value of the variable @code{search-slow-speed},
  209. initially 1200.
  210. @vindex search-slow-window-lines
  211. The number of lines to use in slow terminal search display is controlled
  212. by the variable @code{search-slow-window-lines}. Its normal value is 1.
  213. @node Non-Incremental Search, Word Search, Incremental Search, Search
  214. @section Non-Incremental Search
  215. @cindex non-incremental search
  216. Emacs also has conventional non-incremental search commands, which require
  217. you type the entire search string before searching begins.
  218. @table @kbd
  219. @item C-s @key{RET} @var{string} @key{RET}
  220. Search for @var{string}.
  221. @item C-r @key{RET} @var{string} @key{RET}
  222. Search backward for @var{string}.
  223. @end table
  224. To do a non-incremental search, first type @kbd{C-s @key{RET}}
  225. (or @kbd{C-s C-m}). This enters the minibuffer to read the search string.
  226. Terminate the string with @key{RET} to start the search. If the string
  227. is not found, the search command gets an error.
  228. By default, @kbd{C-s} invokes incremental search, but if you give it an
  229. empty argument, which would otherwise be useless, it invokes non-incremental
  230. search. Therefore, @kbd{C-s @key{RET}} invokes non-incremental search.
  231. @kbd{C-r @key{RET}} also works this way.
  232. @findex search-forward
  233. @findex search-backward
  234. Forward and backward non-incremental searches are implemented by the
  235. commands @code{search-forward} and @code{search-backward}. You can bind
  236. these commands to keys. The reason that incremental
  237. search is programmed to invoke them as well is that @kbd{C-s @key{RET}}
  238. is the traditional sequence of characters used in Emacs to invoke
  239. non-incremental search.
  240. Non-incremental searches performed using @kbd{C-s @key{RET}} do
  241. not call @code{search-forward} right away. They first check
  242. if the next character is @kbd{C-w}, which requests a word search.
  243. @ifinfo
  244. @xref{Word Search}.
  245. @end ifinfo
  246. @node Word Search, Regexp Search, Non-Incremental Search, Search
  247. @section Word Search
  248. @cindex word search
  249. Word search looks for a sequence of words without regard to how the
  250. words are separated. More precisely, you type a string of many words,
  251. using single spaces to separate them, and the string is found even if
  252. there are multiple spaces, newlines or other punctuation between the words.
  253. Word search is useful in editing documents formatted by text formatters.
  254. If you edit while looking at the printed, formatted version, you can't tell
  255. where the line breaks are in the source file. Word search, allows you
  256. to search without having to know the line breaks.
  257. @table @kbd
  258. @item C-s @key{RET} C-w @var{words} @key{RET}
  259. Search for @var{words}, ignoring differences in punctuation.
  260. @item C-r @key{RET} C-w @var{words} @key{RET}
  261. Search backward for @var{words}, ignoring differences in punctuation.
  262. @end table
  263. Word search is a special case of non-incremental search. It is invoked
  264. with @kbd{C-s @key{RET} C-w} followed by the search string, which
  265. must always be terminated with another @key{RET}. Being non-incremental, this
  266. search does not start until the argument is terminated. It works by
  267. constructing a regular expression and searching for that. @xref{Regexp
  268. Search}.
  269. You can do a backward word search with @kbd{C-r @key{RET} C-w}.
  270. @findex word-search-forward
  271. @findex word-search-backward
  272. Forward and backward word searches are implemented by the commands
  273. @code{word-search-forward} and @code{word-search-backward}. You can
  274. bind these commands to keys. The reason that incremental
  275. search is programmed to invoke them as well is that @kbd{C-s @key{RET} C-w}
  276. is the traditional Emacs sequence of keys for word search.
  277. @node Regexp Search, Regexps, Word Search, Search
  278. @section Regular Expression Search
  279. @cindex regular expression
  280. @cindex regexp
  281. A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
  282. denotes a (possibly infinite) set of strings. Searching for matches
  283. for a regexp is a powerful operation that editors on Unix systems have
  284. traditionally offered.
  285. To gain a thorough understanding of regular expressions and how to use
  286. them to best advantage, we recommend that you study @cite{Mastering
  287. Regular Expressions, by Jeffrey E.F. Friedl, O'Reilly and Associates,
  288. 1997}. (It's known as the "Hip Owls" book, because of the picture on its
  289. cover.) You might also read the manuals to @ref{(gawk)Top},
  290. @ref{(ed)Top}, @cite{sed}, @cite{grep}, @ref{(perl)Top},
  291. @ref{(regex)Top}, @ref{(rx)Top}, @cite{pcre}, and @ref{(flex)Top}, which
  292. also make good use of regular expressions.
  293. The XEmacs regular expression syntax most closely resembles that of
  294. @cite{ed}, or @cite{grep}, the GNU versions of which all utilize the GNU
  295. @cite{regex} library. XEmacs' version of @cite{regex} has recently been
  296. extended with some Perl--like capabilities, described in the next
  297. section.
  298. In XEmacs, you can search for the next match for a regexp either
  299. incrementally or not.
  300. @kindex M-C-s
  301. @kindex M-C-r
  302. @findex isearch-forward-regexp
  303. @findex isearch-backward-regexp
  304. Incremental search for a regexp is done by typing @kbd{M-C-s}
  305. (@code{isearch-forward-regexp}). This command reads a search string
  306. incrementally just like @kbd{C-s}, but it treats the search string as a
  307. regexp rather than looking for an exact match against the text in the
  308. buffer. Each time you add text to the search string, you make the regexp
  309. longer, and the new regexp is searched for. A reverse regexp search command
  310. @code{isearch-backward-regexp} also exists, bound to @kbd{M-C-r}.
  311. All of the control characters that do special things within an ordinary
  312. incremental search have the same functionality in incremental regexp search.
  313. Typing @kbd{C-s} or @kbd{C-r} immediately after starting a search
  314. retrieves the last incremental search regexp used:
  315. incremental regexp and non-regexp searches have independent defaults.
  316. @findex re-search-forward
  317. @findex re-search-backward
  318. Non-incremental search for a regexp is done by the functions
  319. @code{re-search-forward} and @code{re-search-backward}. You can invoke
  320. them with @kbd{M-x} or bind them to keys. You can also call
  321. @code{re-search-forward} by way of incremental regexp search with
  322. @kbd{M-C-s @key{RET}}; similarly for @code{re-search-backward} with
  323. @kbd{M-C-r @key{RET}}.
  324. @node Regexps, Search Case, Regexp Search, Search
  325. @section Syntax of Regular Expressions
  326. Regular expressions have a syntax in which a few characters are
  327. special constructs and the rest are @dfn{ordinary}. An ordinary
  328. character is a simple regular expression that matches that character and
  329. nothing else. The special characters are @samp{.}, @samp{*}, @samp{+},
  330. @samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
  331. special characters will be defined in the future. Any other character
  332. appearing in a regular expression is ordinary, unless a @samp{\}
  333. precedes it.
  334. For example, @samp{f} is not a special character, so it is ordinary, and
  335. therefore @samp{f} is a regular expression that matches the string
  336. @samp{f} and no other string. (It does @emph{not} match the string
  337. @samp{ff}.) Likewise, @samp{o} is a regular expression that matches
  338. only @samp{o}.@refill
  339. Any two regular expressions @var{a} and @var{b} can be concatenated. The
  340. result is a regular expression that matches a string if @var{a} matches
  341. some amount of the beginning of that string and @var{b} matches the rest of
  342. the string.@refill
  343. As a simple example, we can concatenate the regular expressions @samp{f}
  344. and @samp{o} to get the regular expression @samp{fo}, which matches only
  345. the string @samp{fo}. Still trivial. To do something more powerful, you
  346. need to use one of the special characters. Here is a list of them:
  347. @need 1200
  348. @table @kbd
  349. @item .@: @r{(Period)}
  350. @cindex @samp{.} in regexp
  351. is a special character that matches any single character except a newline.
  352. Using concatenation, we can make regular expressions like @samp{a.b}, which
  353. matches any three-character string that begins with @samp{a} and ends with
  354. @samp{b}.@refill
  355. @item *
  356. @cindex @samp{*} in regexp
  357. is not a construct by itself; it is a quantifying suffix operator that
  358. means to repeat the preceding regular expression as many times as
  359. possible. In @samp{fo*}, the @samp{*} applies to the @samp{o}, so
  360. @samp{fo*} matches one @samp{f} followed by any number of @samp{o}s.
  361. The case of zero @samp{o}s is allowed: @samp{fo*} does match
  362. @samp{f}.@refill
  363. @samp{*} always applies to the @emph{smallest} possible preceding
  364. expression. Thus, @samp{fo*} has a repeating @samp{o}, not a
  365. repeating @samp{fo}.@refill
  366. The matcher processes a @samp{*} construct by matching, immediately, as
  367. many repetitions as can be found; it is "greedy". Then it continues
  368. with the rest of the pattern. If that fails, backtracking occurs,
  369. discarding some of the matches of the @samp{*}-modified construct in
  370. case that makes it possible to match the rest of the pattern. For
  371. example, in matching @samp{ca*ar} against the string @samp{caaar}, the
  372. @samp{a*} first tries to match all three @samp{a}s; but the rest of the
  373. pattern is @samp{ar} and there is only @samp{r} left to match, so this
  374. try fails. The next alternative is for @samp{a*} to match only two
  375. @samp{a}s. With this choice, the rest of the regexp matches
  376. successfully.@refill
  377. Nested repetition operators can be extremely slow if they specify
  378. backtracking loops. For example, it could take hours for the regular
  379. expression @samp{\(x+y*\)*a} to match the sequence
  380. @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}. The slowness is because
  381. Emacs must try each imaginable way of grouping the 35 @samp{x}'s before
  382. concluding that none of them can work. To make sure your regular
  383. expressions run fast, check nested repetitions carefully.
  384. @item +
  385. @cindex @samp{+} in regexp
  386. is a quantifying suffix operator similar to @samp{*} except that the
  387. preceding expression must match at least once. It is also "greedy".
  388. So, for example, @samp{ca+r} matches the strings @samp{car} and
  389. @samp{caaaar} but not the string @samp{cr}, whereas @samp{ca*r} matches
  390. all three strings.
  391. @item ?
  392. @cindex @samp{?} in regexp
  393. is a quantifying suffix operator similar to @samp{*}, except that the
  394. preceding expression can match either once or not at all. For example,
  395. @samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anything
  396. else.
  397. @item *?
  398. @cindex @samp{*?} in regexp
  399. works just like @samp{*}, except that rather than matching the longest
  400. match, it matches the shortest match. @samp{*?} is known as a
  401. @dfn{non-greedy} quantifier, a regexp construct borrowed from Perl.
  402. @c Did perl get this from somewhere? What's the real history of *? ?
  403. This construct is very useful for when you want to match the text inside
  404. a pair of delimiters. For instance, @samp{/\*.*?\*/} will match C
  405. comments in a string. This could not easily be achieved without the use
  406. of a non-greedy quantifier.
  407. This construct has not been available prior to XEmacs 20.4. It is not
  408. available in FSF Emacs.
  409. @item +?
  410. @cindex @samp{+?} in regexp
  411. is the non-greedy version of @samp{+}.
  412. @item ??
  413. @cindex @samp{??} in regexp
  414. is the non-greedy version of @samp{?}.
  415. @item \@{n,m\@}
  416. @c Note the spacing after the close brace is deliberate.
  417. @cindex @samp{\@{n,m\@} }in regexp
  418. serves as an interval quantifier, analogous to @samp{*} or @samp{+}, but
  419. specifies that the expression must match at least @var{n} times, but no
  420. more than @var{m} times. This syntax is supported by most Unix regexp
  421. utilities, and has been introduced to XEmacs for the version 20.3.
  422. Unfortunately, the non-greedy version of this quantifier does not exist
  423. currently, although it does in Perl.
  424. @item [ @dots{} ]
  425. @cindex character set (in regexp)
  426. @cindex @samp{[} in regexp
  427. @cindex @samp{]} in regexp
  428. @samp{[} begins a @dfn{character set}, which is terminated by a
  429. @samp{]}. In the simplest case, the characters between the two brackets
  430. form the set. Thus, @samp{[ad]} matches either one @samp{a} or one
  431. @samp{d}, and @samp{[ad]*} matches any string composed of just @samp{a}s
  432. and @samp{d}s (including the empty string), from which it follows that
  433. @samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
  434. @samp{caddaar}, etc.@refill
  435. The usual regular expression special characters are not special inside a
  436. character set. A completely different set of special characters exists
  437. inside character sets: @samp{]}, @samp{-} and @samp{^}.@refill
  438. @samp{-} is used for ranges of characters. To write a range, write two
  439. characters with a @samp{-} between them. Thus, @samp{[a-z]} matches any
  440. lower case letter. Ranges may be intermixed freely with individual
  441. characters, as in @samp{[a-z$%.]}, which matches any lower case letter
  442. or @samp{$}, @samp{%}, or a period.@refill
  443. To include a @samp{]} in a character set, make it the first character.
  444. For example, @samp{[]a]} matches @samp{]} or @samp{a}. To include a
  445. @samp{-}, write @samp{-} as the first character in the set, or put it
  446. immediately after a range. (You can replace one individual character
  447. @var{c} with the range @samp{@var{c}-@var{c}} to make a place to put the
  448. @samp{-}.) There is no way to write a set containing just @samp{-} and
  449. @samp{]}.
  450. To include @samp{^} in a set, put it anywhere but at the beginning of
  451. the set.
  452. @item [^ @dots{} ]
  453. @cindex @samp{^} in regexp
  454. @samp{[^} begins a @dfn{complement character set}, which matches any
  455. character except the ones specified. Thus, @samp{[^a-z0-9A-Z]}
  456. matches all characters @emph{except} letters and digits.@refill
  457. @samp{^} is not special in a character set unless it is the first
  458. character. The character following the @samp{^} is treated as if it
  459. were first (thus, @samp{-} and @samp{]} are not special there).
  460. Note that a complement character set can match a newline, unless
  461. newline is mentioned as one of the characters not to match.
  462. @item ^
  463. @cindex @samp{^} in regexp
  464. @cindex beginning of line in regexp
  465. is a special character that matches the empty string, but only at the
  466. beginning of a line in the text being matched. Otherwise it fails to
  467. match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at
  468. the beginning of a line.
  469. When matching a string instead of a buffer, @samp{^} matches at the
  470. beginning of the string or after a newline character @samp{\n}.
  471. @item $
  472. @cindex @samp{$} in regexp
  473. is similar to @samp{^} but matches only at the end of a line. Thus,
  474. @samp{x+$} matches a string of one @samp{x} or more at the end of a line.
  475. When matching a string instead of a buffer, @samp{$} matches at the end
  476. of the string or before a newline character @samp{\n}.
  477. @item \
  478. @cindex @samp{\} in regexp
  479. has two functions: it quotes the special characters (including
  480. @samp{\}), and it introduces additional special constructs.
  481. Because @samp{\} quotes special characters, @samp{\$} is a regular
  482. expression that matches only @samp{$}, and @samp{\[} is a regular
  483. expression that matches only @samp{[}, and so on.
  484. @c Removed a paragraph here in lispref about doubling backslashes inside
  485. @c of Lisp strings.
  486. @end table
  487. @strong{Please note:} For historical compatibility, special characters
  488. are treated as ordinary ones if they are in contexts where their special
  489. meanings make no sense. For example, @samp{*foo} treats @samp{*} as
  490. ordinary since there is no preceding expression on which the @samp{*}
  491. can act. It is poor practice to depend on this behavior; quote the
  492. special character anyway, regardless of where it appears.@refill
  493. For the most part, @samp{\} followed by any character matches only
  494. that character. However, there are several exceptions: characters
  495. that, when preceded by @samp{\}, are special constructs. Such
  496. characters are always ordinary when encountered on their own. Here
  497. is a table of @samp{\} constructs:
  498. @table @kbd
  499. @item \|
  500. @cindex @samp{|} in regexp
  501. @cindex regexp alternative
  502. specifies an alternative.
  503. Two regular expressions @var{a} and @var{b} with @samp{\|} in
  504. between form an expression that matches anything that either @var{a} or
  505. @var{b} matches.@refill
  506. Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
  507. but no other string.@refill
  508. @samp{\|} applies to the largest possible surrounding expressions. Only a
  509. surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
  510. @samp{\|}.@refill
  511. Full backtracking capability exists to handle multiple uses of @samp{\|}.
  512. @item \( @dots{} \)
  513. @cindex @samp{(} in regexp
  514. @cindex @samp{)} in regexp
  515. @cindex regexp grouping
  516. is a grouping construct that serves three purposes:
  517. @enumerate
  518. @item
  519. To enclose a set of @samp{\|} alternatives for other operations.
  520. Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
  521. @item
  522. To enclose an expression for a suffix operator such as @samp{*} to act
  523. on. Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any
  524. (zero or more) number of @samp{na} strings.@refill
  525. @item
  526. To record a matched substring for future reference.
  527. @end enumerate
  528. This last application is not a consequence of the idea of a
  529. parenthetical grouping; it is a separate feature that happens to be
  530. assigned as a second meaning to the same @samp{\( @dots{} \)} construct
  531. because there is no conflict in practice between the two meanings.
  532. Here is an explanation of this feature:
  533. @item \@var{digit}
  534. matches the same text that matched the @var{digit}th occurrence of a
  535. @samp{\( @dots{} \)} construct.
  536. In other words, after the end of a @samp{\( @dots{} \)} construct. the
  537. matcher remembers the beginning and end of the text matched by that
  538. construct. Then, later on in the regular expression, you can use
  539. @samp{\} followed by @var{digit} to match that same text, whatever it
  540. may have been.
  541. The strings matching the first nine @samp{\( @dots{} \)} constructs
  542. appearing in a regular expression are assigned numbers 1 through 9 in
  543. the order that the open parentheses appear in the regular expression.
  544. So you can use @samp{\1} through @samp{\9} to refer to the text matched
  545. by the corresponding @samp{\( @dots{} \)} constructs.
  546. For example, @samp{\(.*\)\1} matches any newline-free string that is
  547. composed of two identical halves. The @samp{\(.*\)} matches the first
  548. half, which may be anything, but the @samp{\1} that follows must match
  549. the same exact text.
  550. @item \(?: @dots{} \)
  551. @cindex @samp{\(?:} in regexp
  552. @cindex regexp grouping
  553. is called a @dfn{shy} grouping operator, and it is used just like
  554. @samp{\( @dots{} \)}, except that it does not cause the matched
  555. substring to be recorded for future reference.
  556. This is useful when you need a lot of grouping @samp{\( @dots{} \)}
  557. constructs, but only want to remember one or two -- or if you have
  558. more than nine groupings and need to use backreferences to refer to
  559. the groupings at the end.
  560. Using @samp{\(?: @dots{} \)} rather than @samp{\( @dots{} \)} when you
  561. don't need the captured substrings ought to speed up your programs some,
  562. since it shortens the code path followed by the regular expression
  563. engine, as well as the amount of memory allocation and string copying it
  564. must do. The actual performance gain to be observed has not been
  565. measured or quantified as of this writing.
  566. @c This is used to good advantage by the font-locking code, and by
  567. @c `regexp-opt.el'.
  568. The shy grouping operator has been borrowed from Perl, and has not been
  569. available prior to XEmacs 20.3, nor is it available in FSF Emacs.
  570. @item \w
  571. @cindex @samp{\w} in regexp
  572. matches any word-constituent character. The editor syntax table
  573. determines which characters these are. @xref{Syntax}.
  574. @item \W
  575. @cindex @samp{\W} in regexp
  576. matches any character that is not a word constituent.
  577. @item \s@var{code}
  578. @cindex @samp{\s} in regexp
  579. matches any character whose syntax is @var{code}. Here @var{code} is a
  580. character that represents a syntax code: thus, @samp{w} for word
  581. constituent, @samp{-} for whitespace, @samp{(} for open parenthesis,
  582. etc. @xref{Syntax}, for a list of syntax codes and the characters that
  583. stand for them.
  584. @item \S@var{code}
  585. @cindex @samp{\S} in regexp
  586. matches any character whose syntax is not @var{code}.
  587. @end table
  588. The following regular expression constructs match the empty string---that is,
  589. they don't use up any characters---but whether they match depends on the
  590. context.
  591. @table @kbd
  592. @item \`
  593. @cindex @samp{\`} in regexp
  594. matches the empty string, but only at the beginning
  595. of the buffer or string being matched against.
  596. @item \'
  597. @cindex @samp{\'} in regexp
  598. matches the empty string, but only at the end of
  599. the buffer or string being matched against.
  600. @item \=
  601. @cindex @samp{\=} in regexp
  602. matches the empty string, but only at point.
  603. (This construct is not defined when matching against a string.)
  604. @item \b
  605. @cindex @samp{\b} in regexp
  606. matches the empty string, but only at the beginning or
  607. end of a word. Thus, @samp{\bfoo\b} matches any occurrence of
  608. @samp{foo} as a separate word. @samp{\bballs?\b} matches
  609. @samp{ball} or @samp{balls} as a separate word.@refill
  610. @item \B
  611. @cindex @samp{\B} in regexp
  612. matches the empty string, but @emph{not} at the beginning or
  613. end of a word.
  614. @item \<
  615. @cindex @samp{\<} in regexp
  616. matches the empty string, but only at the beginning of a word.
  617. @item \>
  618. @cindex @samp{\>} in regexp
  619. matches the empty string, but only at the end of a word.
  620. @end table
  621. Here is a complicated regexp used by Emacs to recognize the end of a
  622. sentence together with any whitespace that follows. It is given in Lisp
  623. syntax to enable you to distinguish the spaces from the tab characters. In
  624. Lisp syntax, the string constant begins and ends with a double-quote.
  625. @samp{\"} stands for a double-quote as part of the regexp, @samp{\\} for a
  626. backslash as part of the regexp, @samp{\t} for a tab and @samp{\n} for a
  627. newline.
  628. @example
  629. "[.?!][]\"')]*\\($\\|\t\\| \\)[ \t\n]*"
  630. @end example
  631. @noindent
  632. This regexp contains four parts: a character set matching
  633. period, @samp{?} or @samp{!}; a character set matching close-brackets,
  634. quotes or parentheses, repeated any number of times; an alternative in
  635. backslash-parentheses that matches end-of-line, a tab or two spaces; and
  636. a character set matching whitespace characters, repeated any number of
  637. times.
  638. @node Search Case, Replace, Regexps, Search
  639. @section Searching and Case
  640. @vindex case-fold-search
  641. All searches in Emacs normally ignore the case of the text they
  642. are searching through; if you specify searching for @samp{FOO},
  643. @samp{Foo} and @samp{foo} are also considered a match. Regexps, and in
  644. particular character sets, are included: @samp{[aB]} matches @samp{a}
  645. or @samp{A} or @samp{b} or @samp{B}.@refill
  646. If you want a case-sensitive search, set the variable
  647. @code{case-fold-search} to @code{nil}. Then all letters must match
  648. exactly, including case. @code{case-fold-search} is a per-buffer
  649. variable; altering it affects only the current buffer, but
  650. there is a default value which you can change as well. @xref{Locals}.
  651. You can also use @b{Case Sensitive Search} from the @b{Options} menu
  652. on your screen.
  653. @node Replace, Other Repeating Search, Search Case, Search
  654. @section Replacement Commands
  655. @cindex replacement
  656. @cindex string substitution
  657. @cindex global substitution
  658. Global search-and-replace operations are not needed as often in Emacs as
  659. they are in other editors, but they are available. In addition to the
  660. simple @code{replace-string} command which is like that found in most
  661. editors, there is a @code{query-replace} command which asks you, for each
  662. occurrence of a pattern, whether to replace it.
  663. The replace commands all replace one string (or regexp) with one
  664. replacement string. It is possible to perform several replacements in
  665. parallel using the command @code{expand-region-abbrevs}. @xref{Expanding
  666. Abbrevs}.
  667. @menu
  668. * Unconditional Replace:: Replacing all matches for a string.
  669. * Regexp Replace:: Replacing all matches for a regexp.
  670. * Replacement and Case:: How replacements preserve case of letters.
  671. * Query Replace:: How to use querying.
  672. @end menu
  673. @node Unconditional Replace, Regexp Replace, Replace, Replace
  674. @subsection Unconditional Replacement
  675. @findex replace-string
  676. @findex replace-regexp
  677. @table @kbd
  678. @item M-x replace-string @key{RET} @var{string} @key{RET} @var{newstring} @key{RET}
  679. Replace every occurrence of @var{string} with @var{newstring}.
  680. @item M-x replace-regexp @key{RET} @var{regexp} @key{RET} @var{newstring} @key{RET}
  681. Replace every match for @var{regexp} with @var{newstring}.
  682. @end table
  683. To replace every instance of @samp{foo} after point with @samp{bar},
  684. use the command @kbd{M-x replace-string} with the two arguments
  685. @samp{foo} and @samp{bar}. Replacement occurs only after point: if you
  686. want to cover the whole buffer you must go to the beginning first. By
  687. default, all occurrences up to the end of the buffer are replaced. To
  688. limit replacement to part of the buffer, narrow to that part of the
  689. buffer before doing the replacement (@pxref{Narrowing}).
  690. When @code{replace-string} exits, point is left at the last occurrence
  691. replaced. The value of point when the @code{replace-string} command was
  692. issued is remembered on the mark ring; @kbd{C-u C-@key{SPC}} moves back
  693. there.
  694. A numeric argument restricts replacement to matches that are surrounded
  695. by word boundaries.
  696. @node Regexp Replace, Replacement and Case, Unconditional Replace, Replace
  697. @subsection Regexp Replacement
  698. @code{replace-string} replaces exact matches for a single string. The
  699. similar command @code{replace-regexp} replaces any match for a specified
  700. pattern.
  701. In @code{replace-regexp}, the @var{newstring} need not be constant. It
  702. can refer to all or part of what is matched by the @var{regexp}. @samp{\&}
  703. in @var{newstring} stands for the entire text being replaced.
  704. @samp{\@var{d}} in @var{newstring}, where @var{d} is a digit, stands for
  705. whatever matched the @var{d}'th parenthesized grouping in @var{regexp}.
  706. For example,@refill
  707. @example
  708. M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET}
  709. @end example
  710. @noindent
  711. would replace (for example) @samp{cadr} with @samp{cadr-safe} and @samp{cddr}
  712. with @samp{cddr-safe}.
  713. @example
  714. M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET}
  715. @end example
  716. @noindent
  717. would perform exactly the opposite replacements. To include a @samp{\}
  718. in the text to replace with, you must give @samp{\\}.
  719. @node Replacement and Case, Query Replace, Regexp Replace, Replace
  720. @subsection Replace Commands and Case
  721. @vindex case-replace
  722. @vindex case-fold-search
  723. If the arguments to a replace command are in lower case, the command
  724. preserves case when it makes a replacement. Thus, the following command:
  725. @example
  726. M-x replace-string @key{RET} foo @key{RET} bar @key{RET}
  727. @end example
  728. @noindent
  729. replaces a lower-case @samp{foo} with a lower case @samp{bar}, @samp{FOO}
  730. with @samp{BAR}, and @samp{Foo} with @samp{Bar}. If upper-case letters are
  731. used in the second argument, they remain upper-case every time that
  732. argument is inserted. If upper-case letters are used in the first
  733. argument, the second argument is always substituted exactly as given, with
  734. no case conversion. Likewise, if the variable @code{case-replace} is set
  735. to @code{nil}, replacement is done without case conversion. If
  736. @code{case-fold-search} is set to @code{nil}, case is significant in
  737. matching occurrences of @samp{foo} to replace; also, case conversion of the
  738. replacement string is not done.
  739. @node Query Replace,, Replacement and Case, Replace
  740. @subsection Query Replace
  741. @cindex query replace
  742. @table @kbd
  743. @item M-% @var{string} @key{RET} @var{newstring} @key{RET}
  744. @itemx M-x query-replace @key{RET} @var{string} @key{RET} @var{newstring} @key{RET}
  745. Replace some occurrences of @var{string} with @var{newstring}.
  746. @item M-x query-replace-regexp @key{RET} @var{regexp} @key{RET} @var{newstring} @key{RET}
  747. Replace some matches for @var{regexp} with @var{newstring}.
  748. @end table
  749. @kindex M-%
  750. @findex query-replace
  751. If you want to change only some of the occurrences of @samp{foo} to
  752. @samp{bar}, not all of them, you can use @code{query-replace} instead of
  753. @kbd{M-%}. This command finds occurrences of @samp{foo} one by one,
  754. displays each occurrence, and asks you whether to replace it. A numeric
  755. argument to @code{query-replace} tells it to consider only occurrences
  756. that are bounded by word-delimiter characters.@refill
  757. @findex query-replace-regexp
  758. Aside from querying, @code{query-replace} works just like
  759. @code{replace-string}, and @code{query-replace-regexp} works
  760. just like @code{replace-regexp}.@refill
  761. The things you can type when you are shown an occurrence of @var{string}
  762. or a match for @var{regexp} are:
  763. @kindex SPC (query-replace)
  764. @kindex DEL (query-replace)
  765. @kindex , (query-replace)
  766. @kindex ESC (query-replace)
  767. @kindex . (query-replace)
  768. @kindex ! (query-replace)
  769. @kindex ^ (query-replace)
  770. @kindex C-r (query-replace)
  771. @kindex C-w (query-replace)
  772. @kindex C-l (query-replace)
  773. @c WideCommands
  774. @table @kbd
  775. @item @key{SPC}
  776. to replace the occurrence with @var{newstring}. This preserves case, just
  777. like @code{replace-string}, provided @code{case-replace} is non-@code{nil},
  778. as it normally is.@refill
  779. @item @key{DEL}
  780. to skip to the next occurrence without replacing this one.
  781. @item , @r{(Comma)}
  782. to replace this occurrence and display the result. You are then
  783. prompted for another input character. However, since the replacement has
  784. already been made, @key{DEL} and @key{SPC} are equivalent. At this
  785. point, you can type @kbd{C-r} (see below) to alter the replaced text. To
  786. undo the replacement, you can type @kbd{C-x u}.
  787. This exits the @code{query-replace}. If you want to do further
  788. replacement you must use @kbd{C-x @key{ESC} @key{ESC}} to restart (@pxref{Repetition}).
  789. @item @key{ESC}
  790. to exit without doing any more replacements.
  791. @item .@: @r{(Period)}
  792. to replace this occurrence and then exit.
  793. @item !
  794. to replace all remaining occurrences without asking again.
  795. @item ^
  796. to go back to the location of the previous occurrence (or what used to
  797. be an occurrence), in case you changed it by mistake. This works by
  798. popping the mark ring. Only one @kbd{^} in a row is allowed, because
  799. only one previous replacement location is kept during @code{query-replace}.
  800. @item C-r
  801. to enter a recursive editing level, in case the occurrence needs to be
  802. edited rather than just replaced with @var{newstring}. When you are
  803. done, exit the recursive editing level with @kbd{C-M-c} and the next
  804. occurrence will be displayed. @xref{Recursive Edit}.
  805. @item C-w
  806. to delete the occurrence, and then enter a recursive editing level as
  807. in @kbd{C-r}. Use the recursive edit to insert text to replace the
  808. deleted occurrence of @var{string}. When done, exit the recursive
  809. editing level with @kbd{C-M-c} and the next occurrence will be
  810. displayed.
  811. @item C-l
  812. to redisplay the screen and then give another answer.
  813. @item C-h
  814. to display a message summarizing these options, then give another
  815. answer.
  816. @end table
  817. If you type any other character, Emacs exits the @code{query-replace}, and
  818. executes the character as a command. To restart the @code{query-replace},
  819. use @kbd{C-x @key{ESC} @key{ESC}}, which repeats the @code{query-replace} because it
  820. used the minibuffer to read its arguments. @xref{Repetition, C-x ESC ESC}.
  821. @node Other Repeating Search,, Replace, Search
  822. @section Other Search-and-Loop Commands
  823. Here are some other commands that find matches for a regular expression.
  824. They all operate from point to the end of the buffer.
  825. @findex list-matching-lines
  826. @findex occur
  827. @findex count-matches
  828. @findex delete-non-matching-lines
  829. @findex delete-matching-lines
  830. @c grosscommands
  831. @table @kbd
  832. @item M-x occur
  833. Print each line that follows point and contains a match for the
  834. specified regexp. A numeric argument specifies the number of context
  835. lines to print before and after each matching line; the default is
  836. none.
  837. @kindex C-c C-c (Occur mode)
  838. The buffer @samp{*Occur*} containing the output serves as a menu for
  839. finding occurrences in their original context. Find an occurrence
  840. as listed in @samp{*Occur*}, position point there, and type @kbd{C-c
  841. C-c}; this switches to the buffer that was searched and moves point to
  842. the original of the same occurrence.
  843. @item M-x list-matching-lines
  844. Synonym for @kbd{M-x occur}.
  845. @item M-x count-matches
  846. Print the number of matches following point for the specified regexp.
  847. @item M-x delete-non-matching-lines
  848. Delete each line that follows point and does not contain a match for
  849. the specified regexp.
  850. @item M-x delete-matching-lines
  851. Delete each line that follows point and contains a match for the
  852. specified regexp.
  853. @end table