2

Ctrl-s, isearch-forward is not appealing to me and so, i wrote a wrapper around it to search from the beginning of the buffer always.

(defun sk-isearch-forward (original)
  (interactive "sEnter search string:  ")
  (save-excursion
    (beginning-of-buffer)
    (isearch-forward original)))

Now, i am seeing two issues:::

1) M-x sk-isearch-forward goes to the beginning of the buffer but never returns to the original cursor position. save-excursion is added to take care of this but it is failing me

2) it is not highlighting any of the matching words in the current buffer.

Is isearch-forward not the right function for this job?

I always search for a word, no regexes and the search, i prefer to start from the beginning. How can i do this?

  • @Name, please make it as the answer... thanks, Name – Madhavan Aug 5 '15 at 5:07
3

Just remove the argument, i.e.

(defun sk-isearch-forward () 
  (interactive)
  (save-excursion 
    (goto-char (point-min))
    (isearch-forward)))
  • 2
    Thanks @lawlist I modified it. The OP wants to return to the original cursor position as stated in the question, so save-excursion seems to be the right choice in this situation. – Name Aug 5 '15 at 5:30
  • how is (beginning-of-buffer) different from (goto-char (point-min))? – Madhavan Aug 5 '15 at 8:33
  • @MadhavanKumar Check out the docstring of beginning-of-buffer by using C-h f to learn that. – Kaushal Modi Aug 5 '15 at 10:59
  • 1
    Reproducing the relevant section of beginning-of-buffer's docstring for the benefit of others - Don't use this (beginning-of-buffer) command in Lisp programs! (goto-char (point-min)) is faster) – Madhavan Aug 5 '15 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.