How to turn on company-files?

I would like to turn on company-files in order to complete file paths in code buffers and code blocks inside org-mode buffers. I have tried many examples I found, but none have had any effect. Please help!

Have you tried something like (set-company-backend! 'prog-mode '(company-capf company-files))?

SPC h f set-company-backend! RET will show you some more examples of its usage.

Thanks, I did look at SPC h f set... but I did not understand it.
I tried (set-company-backend! 'prog-mode '(company-capf company-files)) but I see no difference. The only completion I ever get is possible commands, no paths. I think I must be missing something.

Try these and see if one of these works

(set-company-backend! 'prog-mode '(company-capf :with company-files))
(set-company-backend! 'prog-mode 'company-capf 'company-files)
(set-company-backend! 'prog-mode 'company-files 'company-capf)

If none of them work what is the value of company-backends in the buffer, using SPC h v company-backends RET? Read its documentation too, especially the part under “Grouped backends” heading.

Unfortunately none of those worked. They when I look at SPC h v company-backends I get:

 (:separate company-dabbrev company-yasnippet company-ispell))

which is the default or, when trying some of the code you proposed, I get only:


company-files never apperars. I looked and I do have company-files.el installed. The other thing it says is: Original Value

(company-bbdb company-semantic company-cmake company-capf company-clang company-files
              (company-dabbrev-code company-gtags company-etags company-keywords)
              company-oddmuse company-dabbrev)

My bad! It somehow didn’t register for me that you were working in the org mode. Try replacing 'prog-mode with 'org-mode and see if one of the lines above work. They should at least change the value of company-backends.

Actually I would like it to work in any mode.

I tried with all of these:

(set-company-backend! 'org-mode '(company-capf :with company-files))
(set-company-backend! 'org-mode '(company-capf company-files))
(set-company-backend! 'org-mode 'company-capf 'company-files)
(set-company-backend! 'org-mode 'company-files 'company-capf)

and only the last one had any effect on the company-backends variable; it changed it to (company-capf) only, no company-files.

One think to be aware of and which I should have said but slipped out of my mind: none of these are going to work for already opened buffers unless you revert them i.e do SPC b r and then check if the value of company-backends has changed.

If it doesn’t, the let’s try to directly set the company-backends variable. Do SPC : (setq company-backends '((company-capf :with company-files))). This will set the value of company-backends for the current buffer. See if this gets you what you want.

If not try (setq company-backends '(company-capf company-files)) and so on until you find what you like. Once you do we can try to figure out correct incantation of set-company-backends! so that you don’t have to eval it every time you open a file.

I’m an idiot. I investigated further and it seems, that it is necessary to use C-x C-f to activate the completion. This works. I hoped it would be automatic when it sees a file path. Like the completion for commands and words.
Found it under SPC h p company RET. Once there open the readme under Modules.

Thanks for your help.

This is the default configuration and by adding company-files to the company-backends you should be able to get filename completions automatically. In fact if I evaluate any of

(set-company-backend! 'org-mode 'company-capf 'company-files)
(set-company-backend! 'org-mode 'company-files 'company-capf)

And afterwards open an org buffer, I see 'company-files in the list of company backends and if I type ‘~/’ I get the files in my home directory as completions.

Ok, that worked. I wrote (set-company-backend! 'org-mode 'company-files 'company-capf) into config.el, evaluated the buffer (SPC m e b ), opened a new org buffer and it worked automatically.

I then closed emacs, did a doom sync, opened a new org buffer and it did not work any more. How do I make this permanent?

This seems like a load order issue. My guess would be,

(after! org (set-company-backend! 'org-mode 'company-files 'company-capf))

But I am not a heavy org user and a little bit of tinkering might be needed.

Thank you very, very much! That worked.

Now I need to figure out how to get it to work in prog-mode. Mainly Bash scripts in my case.
In a Bash buffer calling the completion with C-x C-f works, running SPC h v company-backends in that buffer shows:

((company-shell company-files)
 company-capf company-yasnippet)

Just the automatic part is missing. Adding (set-company-backend! 'prog-mode 'company-files 'company-capf) doesn’t work. It actually only duplicates the company-files entry in the variable:

((company-shell company-files)
 company-files company-capf)

From what I understand getting company-backends to work as you want can be a bit fiddly. The elements in that list are tried in order until one activates and the rest are then ignored, so ordering matters. For

((company-shell company-files)
 company-files company-capf)

I would expect company-files to be already active. If it is not then that is probably due to mismatch between the prefix between company-shell and company-files. I can’t be certain as I don’t write shell scripts at all.

My general advice would be following. Use SPC : to test out various combinations of backends till you get one you like. Then check the major-mode using SPC h v major-mode RET and find the file where that major mode is defined. It will be something like FILE.el. Then add the appropraite

(after! FILE (set-company-backend! ...))

to your config.el.

You can try this for one of the parent modes of the major mode too but if there is more specific configurations for the major mode somewhere in doom modules, your company backends will be after the more specific ones.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.