[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Isearch - Incremental search with predefined initial character?

From: Drew Adams
Subject: RE: Isearch - Incremental search with predefined initial character?
Date: Sat, 4 Oct 2014 14:58:45 -0700 (PDT)

> I wish to do incremental searches with the first character pre-
> defined, i.e. have emacs prepend a character to the characters I
> enter.
> E.g., if I start an i-search, and then enter "abc", emacs will
> pretend I entered "Xabc". If I continue the i-search, emacs will
> seek "Xabc".

Just how do you want to tell Emacs to do this?

1. Do you want to hit a key during Isearch to prepend this prefix?
   IOW, on-demand insertion of your string?

2. Do you want a substitute for the normal `C-s', so this prefix is
   prepended each time you start Isearch, i.e., for every search?

Is the prefix to prepend always the same (a constant string)?

Your request is not specific.  Try to specify it better.


1. Did you know that you can yank the last kill into the current
search string, using `C-y'?  Based on the code that does that,
here is some code that you can use to yank a constant string
at the end of the search string, when you hit `C-o' - #1, above.

If you use it before typing anything then your prefix will be
inserted before whatever you type.

(defvar my-prefix "XYZ" "My Isearch prefix.")

(defun my-yank-prefix (&optional prefix)
  "Yank PREFIX into current search string.
Default is the value of `my-prefix'."
  (unless prefix (setq prefix  my-prefix))
  (isearch-yank-string my-prefix))

(define-key isearch-mode-map "\C-o" 'my-yank-prefix)


2. If you want to automatically prepend your prefix each time
you start Isearch - #2, above, then you can do this:

Copy the code for function `isearch-mode' (from isearch.el),
and change the "" binding of `isearch-string' in your copy, so
that that variable is bound instead to your prefix. For example:

(setq isearch-forward  forward ; Initialize global vars.
      isearch-string   my-prefix ; <======= Value changed here

It is not usually a great idea to redefine a standard function
this way, but (a) I don't see how advising the function would
get this job done, and (b) I don't think there is a hook that
you can use to accomplish the same thing.

With luck, someone else will suggest a simpler, cleaner solution.

(I would anyway question why you would want to do this.)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]