13

I enabled company using the following commands in my init.el:

(require 'company)
(add-hook 'after-init-hook 'global-company-mode)

However I'm not getting filename completion, although there is some provider in the source and also the website mentions it's supported.

I start typing /home/emmanuel... and nothing happens, although I would expect the completion to start offering options?

Also, how does company-mode autodetect completion for relative paths, I think I read it's supported, but I'm not sure how it would detect it, without the initial "/" as a tip?

3
  • 1
    You should mention in which major-mode you're trying to have auto-completion through company-mode. Or maybe you are looking for file name completion in the minibuffer? Please specify.
    – paprika
    Nov 17, 2014 at 16:03
  • 1
    I'm a total beginner. I'd like to use it in all the major modes as I understand it (lisp, plain text, any programming languages). And indeed simple keyword completion (offer a word used somewhere else in the buffer) appears to work in lisp-editing as well as other modes. Nov 17, 2014 at 16:05
  • note that I'll later attempt to get more powerful completion for Haskell also, but that'll be only for haskell-mode and for now I'm still trying to get that feature to work in all the modes. Nov 17, 2014 at 16:38

3 Answers 3

8

You have to run company-files for file completion. You can bind a different map for the files completion completion with

(define-key global-map (kbd "C-.") 'company-files)

Also make sure you have the company-files in company-backends (M-x > customize-group > company > company backends)

5
  • You don't necessarily need to bind a key for filename completion. I think the key point here is to configure company-backends. For example you could put company-files first in that list, or include it in a group backend.
    – glucas
    Nov 17, 2014 at 20:43
  • I think it's needed to write some function to trigger company-files, or to configure something. For me, at least, it's not triggered automatically even if it's in company-backends
    – Jesse
    Nov 17, 2014 at 21:32
  • I suspect that is because you have other backends ahead of it in the list? Company mode will use the first backend that indicates it can handle the current prefix. If you insert company-files at the beginning of the company-backends list it should auto-complete. Of course, this may not be what you actually want, just pointing out that the files backend is just another backend...
    – glucas
    Nov 17, 2014 at 22:17
  • True, I didn't know that. I'm a migrating vim user to emacs, and in vim I had the file completion triggered automatically in certain cases, and the code completion in the others. I think that would be the ideal.
    – Jesse
    Nov 18, 2014 at 11:07
  • You can use a mode hook to specify a different list of backends in different modes. For example in lisp mode you may want symbol completion and for shell scripts you want to add filename completion, etc.
    – glucas
    Nov 18, 2014 at 13:41
15

As @Jesse already pointed out, what you want here is the company-files backend. There are several different ways to use it:

  1. Bind a key to call company-files directly.

  2. Use command company-begin-backend. This prompts you for the backend to use, then offers completion candidates.

  3. Use company-other-backend to rotate through the list of backends (see next item). This can be used to trigger completion or it can be used after company mode has been triggered to switch to a different set of completion candidates. You may want to assign a key binding in the company map, e.g. (define-key company-active-map (kbd "C-e") #'company-other-backend)

  4. Configure the variable company-backends. Company mode traverses this list in order to find a backend that accepts the current prefix (i.e. the text before point). It is entirely possible to have a backend in the list that accepts the current prefix but does not offer any completion candidates, at which point company mode won't auto-complete anything. You can customize the list to order the backends in a way that meets your needs.

A few examples of modifying company-backends:

If you only ever wanted to complete filenames, you could make that your only backend:

(setq company-backends '(company-files))

That seems unlikely, so you're better off putting your most commonly used backend first and then using one of options mentioned earlier to switch backends or invoke one by name when you need something else.

You can also configure a 'group' backend that creates a merged set of completion candidates. Try this, for example:

(setq company-backends '((company-capf company-dabbrev-code company-files)))

This specifies a single backend that merges the candidates from three other backends. It will give you results from completion-at-point, dabbrev, and the file system.

You can use mode hooks to specify a different set of backends for different major modes. For example:

(add-hook 'org-mode-hook
          (lambda ()
            (setq-local company-backends '((company-files company-dabbrev)))))

(add-hook 'emacs-lisp-mode-hook
          (lambda ()
            (setq-local company-backends '((company-capf company-dabbrev-code)))))
0
1

You must give either an absolute or a relative path to your eshell command,

$ cat ./some-file   # yey!
$ cat /another-file # yey!
$ cat some-file     # fail :(

Otherwise, the company-files--grab-existing-name function returns nil. You can edebug the said function and check that that is the case.

Check and update the company-files--regexps to support the last case also if you feel brave enough and you are sure it doesn't break other stuff.

Your Answer

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

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