Elisp: Cursor Position Functions
What is Cursor Position
- Cursor position is between characters.
- The beginning of buffer has cursor position 1. (unless Narrow to Region is in effect.)
- By default, cursor is shown as black square ▮ over a character, the position is to the left of the black square.
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
pos-eol
pos-bol
line-beginning-position
line-end-position
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
Emacs Lisp, text processing functions
- Elisp: Cursor Position Functions
- Elisp: Move Cursor
- Elisp: Text Editing Functions
- Elisp: Search Text
- Elisp: Find Replace Text in Buffer
- Elisp: Mark, Region, Active Region
- Elisp: Cut Copy Paste, kill-ring
- Elisp: Get Buffer String
- Elisp: Functions on Line
- Elisp: thing-at-point
- Elisp: Get Text Block 🚀
- Elisp: Save narrow-to-region