It is often handy to search for the word at the current cursor position. By default, you can do this by starting a normal
C-s and then hitting
C-w to search for the current word. Keep hitting
C-w to add subsequent words to the search.
If, like me, you use swiper for your searches, you can obtain the same effect using
M-j after you start
This is all very nice, but both of those solutions above search for the string from the cursor position to the end of the word, so if “|” marks the cursor position in the word
prag|matic, then either method above would search for
matic. I made a small tweak to the relevant function in the ivy library that powers
swiper so that the whole of the word is used, so in the example above
M-j would search for the full
Here is the code:
;; version of ivy-yank-word to yank from start of word (defun bjm/ivy-yank-whole-word () "Pull next word from buffer into search string." (interactive) (let (amend) (with-ivy-window ;;move to last word boundary (re-search-backward "\\b") (let ((pt (point)) (le (line-end-position))) (forward-word 1) (if (> (point) le) (goto-char pt) (setq amend (buffer-substring-no-properties pt (point)))))) (when amend (insert (replace-regexp-in-string " +" " " amend))))) ;; bind it to M-j (define-key ivy-minibuffer-map (kbd "M-j") 'bjm/ivy-yank-whole-word)
An offline commenter pointed out that Xah Lee has a nice alternative implementation of this functionality using isearch.