Emacs Tip of the Day # Are you a fan of Tab Completion? #

by

Tab Completion is a great, great feature. It saves me a lot of time and effort, in bash as well as in the Emacs minibuffer.
I don’t know if you are aware of this, but Emacs also comes with a built-in feature called dabbrev-expand (M-/ in the minibuffer). Here is what it does. If you type out the first few letters of your word and hit the key-combo, it auto-completes the word after searching the current buffer and the other open buffers for completion. You can cycle through all possible completions by repeatedly reinvoking the function. This is very, very cool. The only problem is that M-/ is not conducive to speed.
So can we do something super cool like re-bind the function to the TAB key? Without destroying other tab related functions like indentation? Turns out we can!

(global-set-key [(tab)] ‘smart-tab)
(defun smart-tab ()
“This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line.”
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(dabbrev-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at “\\_>”)
(dabbrev-expand nil)
(indent-for-tab-command)))))

Add this to your .emacs file, and start using TAB in everything you type. You will find that your typing speed triples!

[hat-tip]THE EMACS WIKI

Advertisements

Tags: , , ,

4 Responses to “Emacs Tip of the Day # Are you a fan of Tab Completion? #”

  1. Jitesh Says:

    uber-cool !!!!! … super-duper 😀
    Am going to try this out today itself …

  2. Kedar Says:

    Good to know….
    But didn’t work out for me… 😦

  3. Vedang Says:

    @kedar
    why? worked perfectly well for me.. :S
    I have copied the code from the emacs wiki page. Maybe some extra characters have been introduced, i don’t know.. why don’t you try typing it out in your .emacs file? or taking the code from the emacs wiki page itself?

  4. A quick-tips Emacs post « Tech Rants Says:

    […] Auto-completion with Smart Tab ;function to implement a smarter TAB (global-set-key [(tab)] 'smart-tab) (defun smart-tab () "This […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: