Is it possible to configure company to do completion when cursor is inside a word?

For example, if: ('|' marks the cursor)


and there is a getStream completion, then after pressing my completion hot-key, I'd like to have:


Currently, completion fails, nothing happens. I always have to put a space after the word, then press left, and then initiate the completion.

3 Answers 3


You can advice company-clang--prefix temporarily since you mentioned in another answer's comment that you actually are using clang backend:

(defvar my-override-company-clang--prefix t)
(defadvice company-clang--prefix (around company-clang--prefix-hack activate)
  (if my-override-company-clang--prefix (setq ad-return-value (buffer-substring-no-properties (save-excursion (backward-word) (point)) (point)))

Insert above code after your company setup in ~/.emacs.

Tested on Emacs 25.3 company-0.9.4

  • Thanks! I don't have any experience with company's internals, can you tell me please where to put this code?
    – geza
    Commented Oct 4, 2017 at 17:57
  • in ~/.emacs, after you usual company setup.
    – chen bin
    Commented Oct 5, 2017 at 0:03
  • I fixed some minor issue. Use my latest code.
    – chen bin
    Commented Oct 5, 2017 at 0:18
  • Unfortunately, this doesn't work for me. If I add this in my .emacs file, clang completion stops working entirely. I've tried this with company 0.9.2 and 0.9.4. Do you have a suggestion how can I debug this?
    – geza
    Commented Oct 5, 2017 at 14:02
  • I updated code.
    – chen bin
    Commented Oct 6, 2017 at 6:17

I don't have enough reputation to leave a comment, so not a complete solution, but you should look at writing a backed for company: http://sixty-north.com/blog/writing-the-simplest-emacs-company-mode-backend.html http://sixty-north.com/blog/a-more-full-featured-company-mode-backend.html. You can get most of the code from this page. You are given a prefix, which is the string up to the point. I haven't found more complete documentation than this.

You could also check the Elisp manual's basic completion functions and advising functions. I can't link to them because I don't have enough reputation.

  • Thanks for the answer. Unfortunately, it would be too much for me to fix this issue. I'd like to use current backends (specifically, the clang backend), but with fixed behavior, so writing a new one is not an option.
    – geza
    Commented Sep 29, 2017 at 19:49
  • I upvoted @chen bin's answer though I haven't been able to test it and it doesn't show up because I don't have enough reputation, but that is what I was talking about when I mentioned advising functions.
    – bikemule
    Commented Oct 4, 2017 at 17:20
  • Thanks! I'm not experienced at all with company's internals though, so I still kinda lost what to do (look at my comment under Chen's answer). I've upvoted his answer and yours too, to gain some reputation :)
    – geza
    Commented Oct 4, 2017 at 17:59

There's no good solution for this currently, see https://github.com/company-mode/company-mode/issues/340.

  • Too bad, this is a huge shortcoming of company (for me, at least. I used this feature in my previous editor a lot). Do you know any other completion plugin which can do this?
    – geza
    Commented Sep 29, 2017 at 15:38
  • Yes, auto-complete can do this
    – InHarmsWay
    Commented Sep 29, 2017 at 18:02
  • @Dmitry, would it be possible to revert this behavior somehow: github.com/auto-complete/auto-complete/pull/50. It seems, that before this patch, company worked as I expected. To be honest, I don't really see, why this behavior (#50) is useful. Completing a half-written word using the whole word which is the cursor in (i.e., not ignoring the right part of the word) doesn't make any sense to me. In what editing process is this useful? My example happens all the time with me: when inserting a word before another one.
    – geza
    Commented Sep 29, 2017 at 20:08
  • @geza You're linking to an issue belonging to a different package. It never worked in company; it does when using auto-complete, but with certain drawbacks.
    – Dmitry
    Commented Oct 1, 2017 at 0:02
  • Oops, sorry, this was cross linked from the page you've linked, I didn't noticed that it was about AC.
    – geza
    Commented Oct 4, 2017 at 17:54

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.