Quickly jumping to symbols

This is brilliant.

For a while, I have been using ido to quickly jump between buffers and files by just typing any part of the file/buffer name I want. In fact, typing any sequence of letters narrows the interactive search to candidates with names that contain those letters in that order:


fb -> Foobar, FrancisGailBrown

But for finding method definitions in a big source file, I have always just used isearch. It turns out that there is a better way.

By combining imenu with ido, I can quickly find methods as well:



All it takes is this method in my init.el:



(defun ido-goto-symbol ()
"Will update the imenu index and then use ido to select a
symbol to navigate to"
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))

((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))

((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))

(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))



I have bound it to C-t, because I realized that I don't use transpose-chars (or whichever transpose method was there..I don't use any of those, really).

I found this trick on the imenu wiki page.

4 comments:

  John Tells All

July 13, 2010 at 9:26 PM

very cool!

On Emacs 23 it whines about "flet" not being defined. Adding this gets it to work:

(require 'cl)

  Chopmo

July 13, 2010 at 9:57 PM

Cool, thanks for the tip. I must have that line in my config somewhere, because upgrading to Emacs 23 didn't break this for me.

BTW this is my old blog. All content, plus more Emacs tips, can be found on my new blog: http://chopmode.wordpress.com/

  Chad

June 23, 2011 at 5:10 PM

HI, Chopmo, Thanks for the blog.

You mentioned you can type fb -> foobar, how do you do that ? I can only use foob -> foobar,

Thanks!

  Chopmo

June 24, 2011 at 9:04 AM

Hi Chad,

Thanks for you comment :-)

Actually this is my old blog, so I took the liberty of copying your comment and answering it on my new blog:
http://chopmode.wordpress.com/2008/09/03/quickly-jumping-to-symbols/#comment-209

Hope that's OK :)