Emacs Autocomplete Bliss

You known that IntelliSense rots your brain, right?

Well if you use Emacs, now you too can rot your brain, only better (everything is always better with Emacs).

This is the most treasured snippet from my .emacs file. All it does is store every word in all open files and when you hit tab, it autocompletes based on the text before the cursor. Sure, it doesn't show you the list of possible candidates, but all you ever need is a few tabs and you'll find what you want.

; auto completion
(require 'dabbrev)
(setq dabbrev-always-check-other-buffers t)
(setq dabbrev-abbrev-char-regexp "\sw\|\s_")

(global-set-key "C-i" 'my-tab)

(defun my-tab (&optional pre-arg)
  "If preceeding character is part of a word then dabbrev-expand,
else if right of non whitespace on line then tab-to-tab-stop or
indent-relative, else if last command was a tab or return then dedent
one step, else indent 'correctly'"
  (interactive "*P")
  (cond ((= (char-syntax (preceding-char)) ?w)
         (let ((case-fold-search t)) (dabbrev-expand pre-arg)))
        ((> (current-column) (current-indentation))
        (t (indent-according-to-mode)))
  (setq this-command 'my-tab))

(add-hook 'html-mode-hook
          '(lambda () 
             (local-set-key "C-i"     'my-tab)))
(add-hook 'sgml-mode-hook
          '(lambda () 
             (local-set-key "C-i"     'my-tab)))
(add-hook 'perl-mode-hook
          '(lambda () 
             (local-set-key "C-i"     'my-tab)))
(add-hook 'text-mode-hook
          '(lambda () 
             (local-set-key "C-i"     'my-tab)))

; add more hooks here

The code isn't mine and I can't remember where I found it, but it has saved me countless hours of typing.

This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *