How do you quickly navigate inside a source file using emacs ?
I am a C++ dev. I am now able to follow up symbols or files using LSP,/projectile/ivy/transient aso... but inside one file, I have difficulties going quickly up function by function, or by if/for statement. Do you know a convenient way to do this ? I am interested in any navigation tips as well ! Thanks for your help !
You can go to the next/previous function with C-M-a and C-M-e. With evil mode it's ] m and [ m. You can use imenu. You can make keybindings to scroll forwards/backwards several lines at a time.
I think the "hide-show" package is invaluable in navigating large files.
You can see all functions collapsed in a file.
Or, within a function, you can see all blocks of code {} collapsed.
They jump to the beginning or end of a function and can make scrolling much less painful. I know Evil mode has something similar.
I usually do screenwriting, and fountain-mode uses M-n and M-p to jump between dialogue. I love that and feel like C-M-a and C-M-e are really the closest comparisons to that I know of for code.
Here are some ways to move around. The first two are in vanilla Emacs. (There are previous functions corresponding to the next functions mentioned.)
C-M-e and C-M-a: Move to next "defun" (function definition).
Imenu, if you know the name of the thing (e.g. a definition) you`re looking for. (Various libraries let you complete/filter and cycle among candidates.)
next-visible-thing. Moves to end of next THING. First nonconsecutive use prompts for THING type. Or use next-visible-thing to define such a command for a specific kind of THING (so no prompt needed for the kind).
Library find-where.el lets you get something at a position where an arbitrary predicate is true (not just a position at the start of a text THING), or move to such a position.
E.g., function fw-next-thingreturns the next THING and its position, and command fw-to-next-thinggoes there.
E.g., this defines a command to move to the beginning of the next sexp:
(defun to-next-sexp (n)
"Go to next start of a sexp."
(interactive "p")
(fw-to-next-thing 'sexp nil n))
Likewise, for fw-next-where and fw-to-next-where, which look for the next place and some data where some predicate is satisfied.
See the Commentary in find-where.el.
Commands in library isearch-prop.el to search within the text of certain things.
E.g., isearchp-imenu-non-interactive-function searches only within (or only outside of) definitions of functions that are not commands. isearch-property-forward searches only within text that has (or doesn't have) a given text or overlay property. isearchp-zones-forward searches only within (or only outside) the text of a given set of zones (i.e., within a noncontiguous region).
The old library hideif.el lets you hide text that's within ifdefs.
Awesome compilation!! I already start using C-M e/a already mentioned in this post. It's simple and effective. I need to finish reading everything to get a way to move inside one function.
inside one file, I have difficulties going quickly up function by function
helm-occur
Just start typing and it will show you occurrences in a file; you can use C-n/C-p (or whatever you bind it to), and it will move the point in the buffer and show you the occurrence so you can see the surrounding text. If you C-g the point is left where you were, and if you just press enter your point will be placed at the occurence.
inside one file, I have difficulties going quickly up function by function
helm-occur
Just start typing and it will show you occurrences in a file; you can use C-n/C-p (or whatever you bind it to), and it will move the point in the buffer and show you the occurrence so you can see the surrounding text. If you C-g the point is left where you were, and if you just press enter your point will be placed at the occurence.