How to TAB to auto-complete with org-mode?
Asked Answered
P

3

8

I've installed auto-complete-mode, and I'm using org-mode. When I hit TAB to auto-complete words, it instead tries to indent the line. Sometimes it indents the whole paragraph, sometimes it does nothing.

I tried customizing the variable (setq tab-always-indent 'complete) and changing it to nil, but now when I hit TAB, it just inserts tab spaces across the screen.

I know it's possible to successfully use org-mode with yasnippet and auto-complete both working correctly, because with Emacs Live it works out of the box.

Prieto answered 26/2, 2014 at 14:18 Comment(1)
Did you try this: (ac-flyspell-workaround)Messene
P
11

Auto-complete uses the variable ac-modes to decide whether to enable auto-completion in a particular mode, by default org-mode is not present in this list. So to enable auto-completion in org-mode simply add it to the ac-modes something like this should work

(add-to-list 'ac-modes 'org-mode)

Also to complete using tab you will need to set ac-trigger-key to tab like so (link to the manual)

(ac-set-trigger-key "TAB")
Prohibitionist answered 26/2, 2014 at 14:27 Comment(13)
Thanks. Unfortunately that doesn't work. Auto-complete-mode is on, displaying the AC in the mode line. I can trigger completion by doing M-x auto-complete, but TAB still has no effect.Prieto
What does C-h k tab give? Does it give something like TAB (translated from <tab>) runs the command ac-trigger-key-command,Prohibitionist
<tab> runs the command org-cycle, which is an interactive autoloaded Lisp function in `org.el'.Prieto
Oh right! How can I forget that org-mode takes the tab key, you can get auto-completion in org-mode by overriding tab key but I would advice NOT to do so since tab is very useful key in org-mode. You can bind auto-complete to some other key if you want.Prohibitionist
Hmm, in Emacs Live (another curated Emacs package), TAB to auto-complete does work in org-mode. (And TAB to org-cycle also works, depending on whether you're on a headline or not.)Prieto
@PeterSalazar, just checked emacs-live it does prompt me for completion with tab rather it pops up completion immediately (i.e. without pressing tab). Maybe you have a function declared somewhere that is making tab work for you in org-mode. Just do C-h k tab (in your emacs-live), you will come to know the command run by tab, this will also provide a link to the function's source you can copy that to your init file. I will also try to post a function that will achieve what you want but I am not sure that is a good a idea.Prohibitionist
Emacs Live: <tab> runs the command yas-expand, which is an alias for yas-expand-from-trigger-key' in yasnippet.el'. It is bound to <tab>, <menu-bar> <YASnippet> <Expand trigger>. (yas-expand &optional FIELD) Expand a snippet before point. If no snippet expansion is possible, call command org-cycle'. Optional argument FIELD is for non-interactive use and is an object satisfying yas--field-p' to restrict the expansion to.Prieto
Hi @PeterSalazar, I guess you are looking for the yasnippet package available, at github.com/capitaomorte/yasnippet. Emacs live is running the command from yasnippet it is not autocompletingProhibitionist
It is autocompleting. There are only a handful of snippets defined in the snippets package, but in Emacs Live org-mode, TAB autocompletes any word that already appears in the buffer.Prieto
TAB is not auto-completing for me (in emacs-live). I looked at emacs-live's auto-complete configuration they do not seem to be doing anything that will enable tab completion. Just try adding (setq ac-auto-start 2) and (add-to-list 'ac-sources ac-source-words-in-buffer) in your init file.Prohibitionist
I just did a clean install of Emacs Live, and for me it works out of the box. Launch Emacs, then in that initial default scratch buffer, M-x org-mode. Then type the first two letters of one of the words in the welcome phrase, then TAB. For me it autocompletes.Prieto
@PeterSalazar after the menu appears, do C-g and then press tab again does it auto-complete? What you are seeing is a side-effect of (setq ac-auto-start 2) which start auto-completion as soon as you type 2 chars.Prohibitionist
Well, on my emacs (add-to-list 'ac-modes 'org-mode) causes an error on startup, because the definition of 'ac-modes is void, so no adding to that.Chian
C
1

For me the solution was to install org-ac from melpa-stable. This caused Emacs to show the completed word after the cursor, as if I had already typed it, but in a different color. Using Tab accepts that completion.

(Emacs: GNU Emacs 24.5.1)

Chian answered 6/1, 2018 at 21:40 Comment(0)
P
0

The only robust solution I found was to disable flyspell mode M-xflyspell-mode when I want to use auto-completion in org-mode: Emacs 26.1, GTK+ 3.22.30, Org mode version 9.1.14, Spacemacs 0.200.13. The suggestion (add-to-list 'ac-modes 'org-mode) did not work and I was unable to find ac-flyspell-workaround. Because completion in org-mode is more valuable to me than is flyspell mode, I turn off flyspell much of the time.

Pellerin answered 10/2, 2019 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.