7

Autocompletion is particularly useful in writing CSS, to see the available options for a given property. Company provides company-minimum-prefix-length to control how many characters I have to type before it starts offering options.

However, I want completion without any characters, so I get this:

company mode completion of css

Currently, I can only get this by either:

  1. Explicitly calling M-x company-complete
  2. Setting (setq company-minimum-prefix-length 0), typing a single character and backspace, e.g. a DEL.

Is this possible? Ideally I'd be able to do it for both properties and their values.

  • The problem, though is that completion would always be on. It would be on for properties, and selectors at least. There are so many properties, that completion without a prefix would not be very helpful, and would just take up screen space. So do you want context-sensitive completion properties? – PythonNut May 16 '15 at 19:40
  • Yes, I suppose it's contextual: I though about advising self-insert-command to trigger completion on space, but that only helps for values. Though I think completion without prefix is useful, even for properties. (I learn more CSS and the dropdown shows more than 10 with a scrollbar). – Wilfred Hughes May 16 '15 at 23:38
  • "typing a single character and backspace" Why this? With company-minimum-prefix-length set to 0, you will already see the completion popup after typing space. Not after pressing return, though. – Dmitry Jun 17 '15 at 9:25
  • @Dmitry I'm not seeing that behaviour. What version of company are you using? – Wilfred Hughes Jun 17 '15 at 10:23
  • @WilfredHughes master. – Dmitry Jun 17 '15 at 11:30
5

I use TAB to trigger company-mode on demand in cases like that -- it works fine in your CSS example. I also use TAB to indent (tab-always-indent set to complete). You can integrate company with the built-in indent-for-tab-command using the configuration described here: https://github.com/company-mode/company-mode/issues/94#issuecomment-40884387

Here is the relevant code from the linked github comment:

(define-key company-mode-map [remap indent-for-tab-command]
  'company-indent-for-tab-command)

(setq tab-always-indent 'complete)

(defvar completion-at-point-functions-saved nil)

(defun company-indent-for-tab-command (&optional arg)
  (interactive "P")
  (let ((completion-at-point-functions-saved completion-at-point-functions)
        (completion-at-point-functions '(company-complete-common-wrapper)))
    (indent-for-tab-command arg)))

(defun company-complete-common-wrapper ()
  (let ((completion-at-point-functions completion-at-point-functions-saved))
    (company-complete-common)))
3

As of recent versions of company, this is the way to go:

(define-key company-mode-map [remap indent-for-tab-command] #'company-indent-or-complete-common)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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