Elisp: Cursor Position Functions

By Xah Lee. Date: . Last updated: .

What is Cursor Position

Get Cursor Position

point

Return cursor's current position (a positive integer). (beginning of buffer is 1. Position is best thought of as between characters.)

region-beginning

Return start position of region. [see Elisp: Mark, Region, Active Region]

region-end

Return end position of region.

point-min

Return the start position of visible buffer. (respect Narrow to Region)

point-max

Return the end position of visible buffer. (respect Narrow to Region )

Get Position of Line Beginning or End

Save Cursor Position

When moving cursor, you often want to preserve user's original cursor position, so the cursor won't end up somewhere unexpected when your command is finished.

save-excursion
(save-excursion BODY)

Run BODY, and restore cursor position and buffer. See also: Elisp: Save narrow-to-region

(save-excursion
  ;; code here that moved cursor
)

Reference

Elisp, text processing functions