Stop Going to the Top: isearch-allow-motion in Emacs
I have a small ritual when I need to find something in a file: jump to the top with M-<, start C-s, and type the search string. It works, but it is a little fiddly. The top of the buffer is not where I am, and most of the time I do not actually need to start there. I just want the first match in the file, or I want to search forward from wherever point happens to be.
The built-in isearch-allow-motion option is kinda useful. It lets the ordinary motion keys M-< and M-> work as match navigation while an isearch is active. Type C-s, type the search string, and then press M-< to jump to the first occurrence in the buffer or M-> to jump to the last. The search stays active, so C-s and C-r keep working from the new position. No trip to the top, no restarting the search.
With isearch-allow-motion set to non-nil, isearch intercepts four motion commands and gives them a search-specific meaning:
| Key | Command | Default during isearch | With isearch-allow-motion |
|---|---|---|---|
M-< |
beginning-of-buffer |
exits isearch, goes to buffer start | jumps to first match in buffer |
M-> |
end-of-buffer |
exits isearch, goes to buffer end | jumps to last match in buffer |
C-v |
scroll-up-command |
exits isearch, scrolls forward | jumps to first match after window |
M-v |
scroll-down-command |
exits isearch, scrolls backward | jumps to last match before window |
The search string is not changed, and you are still in isearch after the jump. That is the whole trick: the motion command has been repurposed from "move the buffer" to "move through the matches", but only while you are searching.
The option has been in Emacs since 28.1, so I would say generally there is probably a high chance you already have it.
Add one line in your init file:
(setq isearch-allow-motion t)
The top-of-buffer ritual was not a huge cost, but it was a repeated one. I do this dozens of times a day, and every time I did it I was leaving the place I wanted to search from, just to make the first match the first match. isearch-allow-motion lets me keep my starting point and still get the first or last match when I need it.
It is a small, built-in option, not a new package or a new search interface. It just teaches a few familiar keys to do the obvious thing while isearch is active. If you are on Emacs 28.1 or later, give it a try!