Isearch, that is, incremental search, is the standard way to search in vanilla Emacs. Type ‘C-s’ and start typing a search string – Emacs finds a match to what you’ve typed so far. Change your input, and Emacs adjusts to find the new target. This dynamic, on-the-fly behavior is what is meant by “incremental” search.
Searching with regular expressions (regexps) is searching with wildcards (and more). If you aren’t already searching using regexps, then that’s the next thing you should learn – NOW!
Try ‘C-M-s’ (forward) or ‘C-M-r’ (backward) – or you can use ‘M-r’ after ‘C-s’ or ‘C-r’ (i.e., during a literal search).
With regexp searching you will need to think a little more, but will do fewer searches for a task, and will be able to more precisely get what you’re searching for.
To learn about regexps, see RegularExpression.
You can define your own keymap to extend Isearch with some other key bindings (see MoreIsearchKeys, below), but Isearch already comes with these handy keybindings (and more):
‘RET’. You cannot use ‘RET’ to search for the end of a line – use ‘C-q C-j’ for that. The cursor is left on the last match.‘C-g’ – Abort the search, putting back the cursor to its initial position.‘C-s’ – Repeat the search as many times as you want throughout the buffer.‘C-w’ – Select the (rest of the) word the TextCursor is on as the search string; repeating ‘C-w’ appends more words to the search string.‘M-s C-e’ – Select the text up to the end of the line as the search string (this was bound to ‘C-y’ up until Emacs 24.1).‘M-s h r’ – Highlight regular expression (‘highlight-regexp’)‘M-s h u’ – Unhighlight regular expression‘M-s o’ – call ‘occur’ with the current search term‘C-y’ – Yank (paste) the text last copied to the kill-buffer (clipboard) to the end of the search string (this was bound to ‘M-y’ up until Emacs 24.1).‘M-n’, ‘M-p’ – Re-use a previous search string. Repeat to choose older strings. ‘M-n’ moves forward in the search history; ‘M-p’ moves backward.‘M-TAB’ – Complete the current search string against all previous search strings.‘M-c’ – Toggle search case-sensitivity.‘M-r’ – Toggle between regular-expression searching and literal-string searching.‘M-e’ – Pause to edit the search string. Searching is disabled until you explicitly resume it with ‘C-j’ (or ‘C-s’ or ‘C-r’).‘M-%’ – Start query replace using the current string.‘C-M-%’ – Start a regular-expression query replace using the current search string.‘S-TAB’ to complete the current search string against all previous search strings. This is thus similar to ‘M-TAB’ (see StdIsearchKeys, above), but it gives you all of the Icicles completion features: regexp matching, candidate cycling, … In particular, you can see all matching previous searches in buffer ‘*Completions*’ and either click one (‘mouse-2’) or cycle to it with Page Up/Down. See Icicles - Isearch Enhancements.‘C-h’ provides help on Isearch while isearching. It also redefines ‘isearch-mode-help’ so that it lists all Isearch bindings and ends Isearch properlyC-+’ toggles searching through invisible text.‘C-SPC’ toggles setting the region around the last found occurrence. It adds a function to ‘isearch-mode-end-hook’ that respects the state of this toggle.C-`’ toggles quoting (escaping) of regexp special characters.C-M-;’ toggles option ‘isearchp-ignore-comments-flag’, which controls ignoring text in comments.C-M-~’ toggles searching the complements of the normal search contexts.C-M-`’ toggles escaping of regexp special chars in replacement text.‘C-M-D’ (aka ‘C-M-S-d’) toggles option ‘isearchp-dim-outside-search-area-flag’, which controls dimming of the text that is outside the search area.‘C-M-l’ removes failed part of search string, if any.‘C-M-t’ regexp-searches within propertized (or overlay) zones.‘C-M-y’ yanks from secondary selection ring.C-_’ yanks successive symbols into the search string.C-(’ yanks successive sexps into the search string.‘C-j’ lets you edit the search string. It also provides other options, like changing to word search (‘C-w’ as first char) or non-incremental search (RET). (Standard binding ‘M-e’.)‘C-x r g’ yanks contents of a register.‘C-y C-2’ yanks from secondary selection ring.‘C-y C-c’ yanks next char. Repeatable with ‘C-c’.‘C-y C-e’ yanks rest of line. Repeatable with ‘C-e’.‘C-y C-w’ yanks next char or word. Repeatable with ‘C-w’.‘C-y C-y’ yanks from kill-ring.C-y C-_’ yanks yanks char, subword, word, or symbol. Repeatable with `C-_’.C-y C-(’ yanks sexp, char, subword, word, or symbol. Repeatable with `C-(’.‘C-y M-g’ (same as ‘M-g’) inserts the last successful search string from when you hit ‘C-g’ during Isearch.‘C-y M-y’ (same as ‘M-y’)C-z !’ sets the value of ‘isearch-filter-predicate’.C-z &’ adds a given predicate to ‘isearch-filter-predicate’.C-z %’ adds a filter predicate that matches a given regexp against the current search hit.C-z -’ removes a given predicate.C-z 0’ resets ‘isearch-filter-predicate’ to saved/default value.C-z |’ combines the current filter with a given filter, using ‘or’.C-z ~’ complements the current filter (‘not’).C-z @’ adds a predicate to match a given pattern within a given distance of the search hit.C-z <’ adds a predicate to match a given pattern within a given distance before the search hit.C-z >’ adds a predicate to match a given pattern within a given distance after the search hit.C-z ?’ echoes the current filter predicates.C-z n’ names the current filter predicate, defuning it.C-z p’ toggles option ‘isearchp-show-filter-prompt-prefixes-flag’, which controls whether to prompt for prompt prefixes when adding filters.C-z s’ saves the current filter predicate for subsequent searches.C-z S’ toggles automatically saving current filters for subsequent searchs (option ‘isearchp-auto-save-filter-predicate-flag’).M-:’ reads a Lisp sexp, evaluates it, and appends the value to the search string.M-;’ toggles hiding/showing of comments.‘M-g’ inserts the last successful search string from when you hit ‘C-g’ during Isearch.‘M-k’ cycles option ‘isearchp-drop-mismatch’, which controls how a search-string mismatch is handled.‘M-s h l’ toggles ‘lazy-highlight-cleanup’.‘M-s h L’ toggles option ‘isearch-lazy-highlight’.‘M-s i’ toggles searching invisible text, either temporarily or not, depending on option ‘isearchp-toggle-option-flag’ (a prefix arg flips the option behavior).‘M-s v’ toggles option ‘isearchp-toggle-option-flag’, which controls whether toggling behaviors applies beyond the current search.‘M-s M-k’ toggles option ‘isearchp-repeat-search-if-fail-flag’, which controls whether to restart search from search limit.‘M-s M-SPC’ toggles option ‘isearchp-set-region-flag’, which controls setting the region around the search target.‘M-w’ copies the current search string to the kill ring.‘C-h’ in Isearch provide help on Isearch. See IncrementalSearchHelp. This is also done by Isearch+ (see above).‘C-y’ in Isearch act like ‘M-y’ does in the default map: (define-key isearch-mode-map (kbd "C-y") 'isearch-yank-kill)‘C-y’ in Isearch didn’t do what you might expect: Instead of yanking from the ‘kill-ring’ to the search string, it acts on the current line.‘isearch-occur’ → OccurFromIsearch: Invoke ‘occur’ on Isearch hits.‘isearch-highlight-phrase’ → HighlightFromIsearch: Highlight Isearch hits even after searching.(defun isearch-yank-sexp () "*Pull next expression from buffer into search string." (interactive) (isearch-yank-internal (lambda () (forward-sexp 1) (point))))
isearch-mode-map.‘C-s’ and ‘C-r’ to the <next> and <prior> keys (page down and up): (define-key isearch-mode-map [next]
'isearch-repeat-forward) (define-key isearch-mode-map [prior]
'isearch-repeat-backward)‘C-q C-j’. To use another key for this, e.g. ‘C-o’, do this in your init file:(define-key isearch-mode-map "\C-o" '(lambda ()(interactive)(isearch-process-search-char ?\n)))
Note that when you type ‘C-r’, isearch goes backwards. When you search backwards, find something, and then end the search, for example by hitting ‘RET’, point is at the beginning of the string you searched. When you search forwards, point is not necessarily at the end of the string you were searching for, but only after the last character that you typed – bad usability! That means that you cannot predict point based on the string you are searching for (and thinking about). You must predict point based on the numbers of characters you typed.
Here is how to change that in your InitFile, courtesy of AdrianKubala:
(add-hook 'isearch-mode-end-hook 'my-goto-match-beginning)
(defun my-goto-match-beginning ()
(when (and isearch-forward isearch-other-end)
(goto-char isearch-other-end)))(defadvice isearch-exit (after my-goto-match-beginning activate) "Go to beginning of match." (when (and isearch-forward isearch-other-end) (goto-char isearch-other-end)))
With this hook, both ‘C-g’ and ‘RET’ exit the search at the begining of the search string. To recover the old behaviour of ‘C-g’ (going back to where you where before starting the search), you can change the hook to (only tested on Emacs 22):
(defun my-goto-match-beginning () (when (and isearch-forward isearch-other-end (not isearch-mode-end-hook-quit)) (goto-char isearch-other-end)))
Tip:
To get back to where you started the search, just use‘C-x C-x’. This works because isearch sets the mark at the search start (if point ends up elsewhere after searching).Note: Emacs 21 currently has a bug in that ‘isearch-mode-end-hook’ is called after ‘C-g’ exits an isearch. That means that if you end a search with ‘C-g’ you cannot use ‘C-x C-x’ to get back to where you were before searching.
Since I submitted that bug, I’ve heard back that it will be corrected in the next GNU Emacs release. – DrewAdams
Another way to get the functionality of jumping to the beginning of the search-term is simply adding a little wrapper function and binding it to a different key. I find C-v a good candidate, you don’t usually want to jump to next page when searching:
(define-key isearch-mode-map (kbd "C-v") 'my-isearch-forward-to-beginning) (defun my-isearch-forward-to-beginning () "Do a forward search and jump to the beginning of the search-term." (interactive) (isearch-repeat 'forward) (goto-char isearch-other-end))
Now you can use C-s to search normally (point at end) and C-v to make point jump to beginning. Alternatively, you can swap these two keys if you prefer the new behaviour more often.
(define-key isearch-mode-map (kbd "C-s") 'my-isearch-forward-to-beginning) (define-key isearch-mode-map (kbd "C-v") 'isearch-repeat-forward)
– Fuco
Another approach is hit C-r Enter to end the incremental search forward instead of just Enter
– Jimmity
See also: IsearchOtherEnd.
Library oneonone.el changes the background color of the (separate) minibuffer frame when you search. Screenshots of the minibuffer frame (normal, followed by isearch):
Besides enhancing Isearch completion (see MoreIsearchKeys, above), Icicles provides an additional, unusual way of searching incrementally that is akin to both isearch and ‘occur’ – but weirder! The explanation is here: Icicles - Search Commands, Overview. Highlights:
“Incremental” has a double meaning here:
(add-hook 'isearch-mode-hook (lambda () (interactive) (setq isearch-message (concat isearch-message "[ " (car search-ring) " ] ")) (isearch-search-and-update)))
Here’s a script which does the trick: isearch-dabbrev
Here is some more info about Isearch:
I often search for a string which I want to grab and paste elsewhere: So I want to find it, mark it, and kill it. I can use C-w/C-z to extend the search string (either by word or by sexp), and then I just want to grab is somehow: I think it’s natural to bind it to M-w.
Here’s how:
(defun isearch-save-and-exit () "Exit search normally. and save the `search-string' on kill-ring." (interactive) (isearch-done) (isearch-clean-overlays) (kill-new isearch-string)) (define-key isearch-mode-map "\M-w" 'isearch-save-and-exit)