I'm trying to use auto-complete to match in the middle of a string (after the characters ::). I'd like to typing a then hit tab and have the list of candidates appear 'in full' e.g. fruit::apples, others::apples, veg::asparagus.

So far, I'm approaching this using an alist:

(setq l1 '(("apples"  "fruit::apples")
       ("apples"  "other::apples")
       ("bananas"  "fruit::bananas")
       ("asparagus"  "veg::asparagus")))

The completions are matched to the car of each element in l1 but return the cdr. The 'prefix' part always ends with :: and I would prefer to avoid using alternatives such as fruit-apples...

Here is a function which prompts in the minibuffer and can display a list of candidates in another window:

(defun f1 () "
Replaces text at point with completion, including the appropriate prefix"
       ;; ac1 = all completions
       ;; lc1 = list of completions
       (let ((tap1 (thing-at-point 'symbol))
         (lc1 ())
         (resize-mini-windows t)
         (p1 (point))
         elem1 r1)
     (when (not tap1) (set 'tap1 ""))
     (set 'ac1 (all-completions tap1 l1))
     (mapc (lambda (elem1)
         (when (member (car elem1) ac1)
           (push (cdr elem1) lc1)))
     (set  'lc1 (mapcar (lambda (x) (print (car x))) lc1))
     (set 'elem1 (concat "Complete " tap1 " (TAB for selection) : "))
     (set 'r1 (completing-read elem1 lc1))
     (delete-region (- p1 (length tap1)) p1)
     (insert r1)))

so that if I call f1 when at text that says a I can replace it as desired.

Now, is there any way to adapt auto-complete to behave like this? I've been going through the source files and can't figure out where I should replace the usual list of (all-completions STRING COLLECTION) with the list which includes the prefixes.

Is there a simpler way to do this? Should I be looking at company-mode instead?

1 Answer 1


Even with vanilla Emacs you can do this, just by using completing-read and looking up the car in I1. If the cars are unique then assoc is all you need.

(cadr (assoc (completing-read "Choose: " I1) I1))
  • Sorry, the cars are not unique here; I have edited the question to reflect this. Good suggestion though.
    – dardisco
    Commented Aug 8, 2015 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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