Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Quickly search for occurrences of the symbol at point

Isearch is one of Emacs’ most useful (and probably most used) features. Getting in the habit of quickly hitting C-s followed by 2–4 letters will forever change the way you navigate buffers, and adding it to your repertoire is a tremendous productivity improvement. What, then, could we possibly improve on such a phenomenal command?

One of the search actions I find myself doing a lot is to search forward for other occurrences of the symbol at point. You can do that in Isearch by typing C-w a few times (which copies the next word at point into the prompt) until you’ve got the entire symbol, and then you type M-s _ to toggle symbol-search.

However, this is a lot of keys, and it only works if the cursor is at the start of the symbol. Fortunately, it’s very easy to improve this because the action we want is already implemented as the isearch-forward-symbol-at-point command.

(defun endless/isearch-symbol-with-prefix (p)
  "Like isearch, unless prefix argument is provided.
With a prefix argument P, isearch for the symbol at point."
  (interactive "P")
  (let ((current-prefix-arg nil))
    (call-interactively
     (if p #'isearch-forward-symbol-at-point
       #'isearch-forward))))

(global-set-key [remap isearch-forward]
                #'endless/isearch-symbol-with-prefix)

The result here is that C-s starts Isearch as usual, but if I type C-u C-s instead it’s going to search for other occurrences of the symbol at point—a huge improvement over the 6+ keys of our previous option.

Normally, calling Isearch with a prefix would start it in regexp-mode. That’s not something I’ve ever used, but you should be aware of it before you override it.

comments powered by Disqus