How to quickly navigate/jump between functions on emacs?

6,681

Solution 1

M-x imenu lets you jump to functions in the same file. I have bound it to Super-i for easy access.

Solution 2

As already suggested by others I also use Imenu exclusively to navigate in the buffer. I find it very useful to do that by having ido interface for Imenu. here is my config for ido-imenu. (this is slightly modified version from the function you find it emacswiki page)

(defun ido-imenu ()
  "Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
  (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))
    ;; If there are matching symbols at point, put them at the beginning
    ;; of `symbol-names'.
    (let ((symbol-at-point (thing-at-point 'symbol)))
      (when symbol-at-point
        (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
               (matching-symbols
                (delq nil (mapcar
                           (lambda (symbol)
                             (if (string-match regexp symbol) symbol))
                           symbol-names))))
          (when matching-symbols
            (sort matching-symbols (lambda (a b) (> (length a) (length b))))
            (mapc
             (lambda (symbol)
               (setq symbol-names (cons symbol (delete symbol symbol-names))))
             matching-symbols)))))
    (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
           (position (cdr (assoc selected-symbol name-and-pos))))
      (push-mark)
      (if (overlayp position)
          (goto-char (overlay-start position))
        (goto-char position)))))

(global-set-key (kbd "C-x C-i") 'ido-imenu)

And I can use C-xC-i in many number of languages modes that support imenu

Solution 3

I have been using cscope integrated into Emacs, and it works really well for searching for functions, variables etc and jumping around between them.

Edit: in .emacs (or .xemacs/init.el) I have:

(require 'xcscope)
(setq cscope-do-not-update-database t)

Then I just manually run cscope on the source files as needed (for the Linux kernel, make cscope, which also works on many other large projects).

Share:
6,681

Related videos on Youtube

Jack
Author by

Jack

Updated on September 18, 2022

Comments

  • Jack
    Jack over 1 year

    How to quickly navigate/jump between functions on emacs? I'm looking for a way to jump quickly to functions on emacs. I'm using the emacs search to do it,but it's too slow and fail. For example,I need to make sure to type a string that will not match to function prototype or function call. I need to include the type of function and begging of parameters type to it.. and depending to coding syntax style used in the program,it's hard/inviable to do. What exactly I'm looking for: something that I type just the function name and jump to begging of it. My current language is C on linux. But if there's such feature to another programming languages and platforms,please show me too. Will be very appreciated.

    Please: don't suggest "use an IDE" I'm fine with emacs.

  • Vera zhin
    Vera zhin almost 11 years
    Could you post your cscope config? I'm still trying to figure out how to configure it for efficient and streamlined workflow...
  • Admin
    Admin almost 11 years
    Yes, edit with config posted...
  • Vera zhin
    Vera zhin almost 11 years
    Wait... that's all? Now I feel kinda stupid... :-)
  • Admin
    Admin almost 11 years
    This is a cool solution!
  • cevaris
    cevaris about 9 years
    Hmm. Seems imenu only searches the current open buffers. Any info on configuring it to search the whole project?
  • rkachach
    rkachach over 8 years
    I'll advice to combine imenu with ido-goto-symbol or the helm mode. This way you have just to type few letters from the name of the function to jump to it in the current buffer.
  • rkachach
    rkachach over 8 years
    This is a very good solution. Another alternative is to enable the helm mode this way you get the same functionality by default without the need of extra lisp code.