1

I used to have a configuration for TAB which was doing something like this :

This is the code before hitting TAB.

template<typename T>
class A
{

    }; // This is where I hit TAB

The code auto-indented as below after hitting TAB:

template<typename T>
class A
{

};

The TAB key used to work as above but now it does only auto-complete.

Can I fix this ? I can use TAB with C-qTAB but it is hard to do always.

Here is my .emacs file

(add-to-list 'load-path "~/.emacs.d/jdee-2.4.1/lisp")
(load "jde")

(add-to-list 'load-path "~/.emacs.d")
(require 'linum)
(global-linum-mode 1)

(add-to-list 'load-path "~/.emacs.d/elpa/yasnippet-0.8.0")
(require 'yasnippet)
(yas-global-mode 1)
; care about this place
(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "<backtab>") 'yas-expand)
; care about this place
(setq ac-source-yasnippet nil)


(add-to-list 'load-path "~/.emacs.d/auto-complete-1.3.1")
(require 'auto-complete-config)
(ac-config-default)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(global-auto-complete-mode t) ; section1
(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")

(defun my:ac-c-headers-init ()
  (require 'auto-complete-c-headers)
  (add-to-list 'ac-sources 'ac-source-c-headers))

(add-hook 'c++-mode-hook 'my:ac-c-headers-init)
(add-hook 'c-mode-hook 'my:ac-c-headers-init)

(add-to-list 'load-path "~/.emacs.d/iedit")
(require 'iedit)
(define-key global-map (kbd "C-c ;") 'iedit-mode)

(semantic-mode 1)
(defun my:add-semantic-to-autocomplete() 
  (add-to-list 'ac-sources 'ac-source-semantic)
)
(add-hook 'c-mode-common-hook 'my:add-semantic-to-autocomplete)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ansi-color-names-vector ["#242424" "#e5786d" "#95e454" "#cae682" "#8ac6f2" "#333366" "#ccaa8f" "#f6f3e8"])
 '(cua-mode t nil (cua-base))
 '(custom-enabled-themes (quote (tango-dark)))
 '(font-use-system-font t)
 '(show-paren-mode t)
 '(uniquify-buffer-name-style (quote forward) nil (uniquify)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:family "DejaVu Sans Mono" :foundry "unknown" :slant normal :weight bold :height 113 :width normal)))))
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(setq indent-line-function 'insert-tab)

Edit When I add JDE it fixed but now I cannot use auto-complete ? It doesn't complete anything.

7
  • Are you talking about using TAB to move to next field when inserting a yasnippet template? Commented Oct 31, 2014 at 12:12
  • 1
    Or are you asking how to use TAB for template insertion instead of auto completion? Commented Oct 31, 2014 at 12:15
  • second one, my TAB button is doing just complete things, I do not have any problem with yasnippet or auto-complete. Commented Oct 31, 2014 at 12:21
  • 1
    Bind TAB to one thing only, and should be used for auto-complete. It would be easier. You can bind RET to newline-and-indent for automatic indentation: (global-set-key (kbd "RET") 'newline-and-indent). You can indent whole buffer or region (if selected) using the code snippet here.
    – Tu Do
    Commented Oct 31, 2014 at 12:56
  • @TuDo Could you look at my edit, how can i enable y auto-complete for jde,it doesn't work when I coding java. Commented Oct 31, 2014 at 13:00

1 Answer 1

3

If you didn't have other packages bound to TAB, I think the behavior you want is provided by:

 (setq tab-always-indent 'complete)

With this setting the TAB key will first try to re-indent the current line. If the line is already indented properly it will call completion-at-point instead. You can customize the variable completion-styles to tune how the built-in completion behaves.

If you want to use the auto-complete package instead, you can hook ac in to completion-at-point so that it works with the setting above. This example configuration might be helpful: https://github.com/purcell/emacs.d/blob/master/lisp/init-auto-complete.el

That example basically adds auto-complete-at-point to completion-at-point-functions, with a wrapper to handle some edge cases and a hook to wire things up when auto-complete is enabled.

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.