Emacs: isearch Current Word

By Xah Lee. Date: . Last updated: .

Here's a command to isearch the word under cursor.

(defun xah-search-current-word ()
  "Call `isearch' on current word or text selection.
“word” here is A to Z, a to z, and hyphen 「-」 and underline 「_」, independent of syntax table.
URL `http://ergoemacs.org/emacs/modernization_isearch.html'
Version 2015-04-09"
  (interactive)
  (let ( $p1 $p2 )
    (if (use-region-p)
        (progn
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
      (save-excursion
        (skip-chars-backward "-_A-Za-z0-9")
        (setq $p1 (point))
        (right-char)
        (skip-chars-forward "-_A-Za-z0-9")
        (setq $p2 (point))))
    (setq mark-active nil)
    (when (< $p1 (point))
      (goto-char $p1))
    (isearch-mode t)
    (isearch-yank-string (buffer-substring-no-properties $p1 $p2))))

You need to give it a easy key. Such as F8. [see Emacs: How to Define Keys]

Also, you should set your isearch repeat to arrow keys. Emacs: isearch by Arrow Keys.

Problem with Emacs's isearch

Emacs's “isearch” has some problems. Suppose you have this line:

… aa-bb-cc xx yy …

and your cursor is on the second dash. You want to search the next occurrence of the word “aa-bb-cc”. You have to press 【Alt+b Alt+b Ctrl+s Ctrl+w Ctrl+w Ctrl+w Ctrl+s】. That's about 8 keys. In vim, it's just a single key press 【*】.

addendum: Emacs 24.4 has the new command isearch-forward-symbol-at-point that improved the situation but still pretty bad. Now to search current word under cursor, you need to press 【Alt+s . Alt+s _ Ctrl+s】. [see Emacs: Search Text in Buffer] Besides the too-many-keys problem, this command has more problems:

  1. isearch-forward-symbol-at-point is on “symbols” only. What chars are part of “symbol” is unpredictable, mode-dependent.
  2. The search is with boundary check. That is, if current symbol is “xyz”, it'll will not find occurrences of “xyz2”.

Emacs Find Replace

  1. Search Text in Buffer
  2. isearch Current Word
  3. Isearch Whitespace Regex
  4. Highlight Text in Buffer
  5. List Delete Highlight Sort Lines
  6. Search Text in Directory
  7. Find Replace in Buffer
  8. Find Replace in Directory
  9. xah-find.el, Find Replace in Pure Elisp
  10. Regex Tutorial

Emacs Modernization

  1. Simple Changes Emacs Should Adopt
  2. Why Emacs Keys are Painful
  3. Problems of the Scratch Buffer
  4. M-key Notation vs Alt+key Notation
  5. Menu Problem
  6. Mode Line Problem
  7. cua-mode Problems
  8. kill-buffer Induces Buffer Accumulation
  9. Emacs Form Feed ^L
  10. Inconsistency of Search Features
  11. Single Key to Delete Whole Line
  12. Emacs HTML Mode Sucks
  13. Emacs Does Not Support Viewing Images Files In Windows
  14. Emacs Spell Checker Problems
  15. Adopt HTML as Texinfo Replacement
  16. Support HTML Mail
  17. Problems of “man” Command
  18. Emacs Lisp Mode Syntax Coloring Problem
  19. Emacs Ahk Mode Problems
  20. Problems of Emacs's Manual
  21. Problems of Emacs's Manual; Examples
  22. Emacs: Have You Read Emacs Manual?
  23. Elisp: Ban Syntax Table
  24. Emacs: Make elisp-index-search use Current Symbol
  25. Emacs: Usability Problems of Mode Documentation
  26. Emacs GNU Texinfo Problems; Invalid HTML
  27. A Record of Frustration in IT Industry; Disappearing FSF URLs, 2006
  28. Emacs Manual Node Persistency Issues
  29. Emacs: dired-do-query-replace-regex Replace ALL (fixed)
  30. Problems of Emacs Supporting Obsolete Systems
  31. Elisp: Function to Copy/Delete a Dir Recursively (fixed)
  32. Thoughts on Common Lisp Scheme Lisp Based Emacs
  33. Text Editors Popularity and Market Research
  34. Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)
  35. GNU Emacs Development Inefficiency
  36. Emacs Dev Inefficiency and Emacs Web 2.0?
  37. Letter-Case Commands Usability Problems
  38. Emacs: Select Line, between Quotes, Extend Selection
  39. Emacs: isearch Current Word
  40. Suggestions on Line Wrap Commands
  41. Emacs: Single Key to Delete Whole Line
  42. Emacs Undo and Emacs Cult Problem

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc