26.2.2 Moving by Defuns
These commands move point or set up the region based on top-level major definitions, also called defuns.
C-M-a
Move to beginning of current or preceding defun
( beginning-of-defun
).
C-M-e
Move to end of current or following defun ( end-of-defun
).
C-M-h
Put region around whole current or following defun ( mark-defun
).
The commands to move to the beginning and end of the current defun
are C-M-a
( beginning-of-defun
) and C-M-e
( end-of-defun
). If you repeat one of these commands, or use a
positive numeric argument, each repetition moves to the next defun in
the direction of motion.
C-M-a
with a negative argument -n moves forward
n times to the next beginning of a defun. This is not exactly
the same place that C-M-e
with argument n would move to;
the end of this defun is not usually exactly the same place as the
beginning of the following defun. (Whitespace, comments, and perhaps
declarations can separate them.) Likewise, C-M-e
with a
negative argument moves back to an end of a defun, which is not quite
the same as C-M-a
with a positive argument.
To operate on the current defun, use C-M-h
( mark-defun
), which sets the mark at the end of the current
defun and puts point at its beginning. See Commands to Mark Textual Objects. This
is the easiest way to get ready to kill the defun in order to move it
to a different place in the file. If the defun is directly preceded
by comments (with no intervening blank lines), they are marked, too.
If you use the command while point is between defuns, it uses the
following defun. If you use the command while the mark is already
active, it extends the end of the region to include one more defun.
With a prefix argument, it marks that many defuns or extends the
region by the appropriate number of defuns. With negative prefix
argument it marks defuns in the opposite direction and also changes
the direction of selecting for subsequent uses of mark-defun
.
In C mode, C-M-h
runs the function c-mark-function
,
which is almost the same as mark-defun
; the difference is that
it backs up over the argument declarations, function name and returned
data type so that the entire C function is inside the region. This is
an example of how major modes adjust the standard key bindings so that
they do their standard jobs in a way better fitting a particular
language. Other major modes may replace any or all of these key
bindings for that purpose.