Imenu, with a workaround

imenu is a very handy command i learnt not long ago. Just type M-x imenu in a source code buffer (an Elisp one, for instance) and start typing the name of a variable or function (or simply TAB for name completion). There you go: instant access to definitions in the buffer.

You can also get the list of completions in a menu item calling imenu-add-menubar-index. Or, if you want the menu to be automatically created for, say, all your C files, you can put something like

(add-hook 'c-mode-hook 'imenu-add-menubar-index)

in your initialisation files.

You can read more about imenu (which has obsoleted speedbar in my workflow) in the Emacs Wiki, including how to adapt it to your favorite language (if it’s not yet supported).

Oh, and the workaround: if you’re using Emacs 22 on Mac OS X, you may need to add this dummy definition to your .emacs to make imenu work:

(defun imenu-progress-message (a &optional c d))

(otherwise, you’ll get an error about imenu-progress-message being an invalid function).

About these ads

10 Responses to “Imenu, with a workaround”

  1. metapundit Says:

    First let me say that this blog is awesome! As an aspiring emacs beginner this is just the sort of thing I need – my emacs learning lately has consisted of trying out whatever you blog about (plus slogging through the manual)…

    My daily dev work is in PHP and I had installed php-mode (the Turadg Aleahmad version) but hadn’t ever enabled the imenu support. When I needed to see a list of functions i was using

    M-x occur ^[\ \t]*function

    I had previously looked around for some sort of more IDE’ish style environment and installed ECB (very nice: list of buffers open, list of files in current directory, list of important symbols in current file) but wasn’t able to get it to support PHP. Seeing your post made me check out imenu instead and once I got that working ECB automatically uses it for my function list as well. (ECB uses the Semantic Bovinator for file parsing, but it will fall back to ctags or imenu if necessary). Very nice – something I’d been meaning to fix for a while… Keep up the good work!

  2. Mark Aufflick Says:

    If you’re running a gui emacs (eg. X11) you can use imenu like a “modern” context menu under your right mouse button:

    (global-set-key [mouse-3] ‘imenu)

  3. Mark Aufflick Says:

    Just in case someone gets confused by the “smart” quote:

    (global-set-key [mouse-3] ‘imenu)

  4. Mark Aufflick Says:

    too smart for me…

  5. Paul Oliver Says:

    Another gem! I’ve been using it for TCL and SQL so far.

    @Mark Thanks for your tip. I hate to have to use the menubar and normally have it hidden.

  6. Vikas Gorur Says:

    I’ve been using Emacs for three years, and I thought I knew most of the tricks. To my delight, every single entry in this blog has taught me something new. Thanks!

  7. Aemon Says:

    Hey, FYI if someone leaves a reply, and they neglect to include Mail or Website (which are not declared to be required fields) then they will lose everything they typed :( which was….


    Great blog!

    However, is imenu really better than a simple isearch-forward? I like to type a fragment of what I’m looking for, not the whole thing, starting at the beginning. I’m not sure I want to retrain my fingers to use this special search in the 10% of cases where it would be more efficient.

    BTW, I refuse to enable menus ;)

  8. Phil Says:

    Cool stuff.

    I’m loving it; however, the tab completion seems pretty primitive to me. ido-style is much preferrable.

    I tried using flet to ido-ize imenu, but it seems to not be working. Perhaps this is the wrong approach. Any ideas?

    Aemon: if you can get imenu to work with ido, then you can use it by only typing the first unique fragment of what you’re looking for. Then it has all the advantages of isearch.

  9. Andrei Says:

    imenu is pretty cool, but it does not grok Javascript files where functions are defined as

    foo = function(bar) {
    alert(bar + “!”);

    It only likes the good old

    function foo(bar) {
    alert(bar + “!”);

  10. Fannie Maddin Says:

    Interesting read. There is currently quite a lot of information around this subject around and about on the net and some are most defintely better than others. You have caught the detail here just right which makes for a refreshing change – thanks.

Leave a Reply

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

You are commenting using your 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


Get every new post delivered to your Inbox.

%d bloggers like this: