Elisp: Cursor Position Functions

By Xah Lee. Date: . Last updated: .

Here's the most useful cursor related functions.

Get Cursor Position

point
(point)
Return cursor's current position.
;; returns cursor's current position
(point)
;; beginning of buffer is 1
region-beginning
(region-beginning)
Return start position of text selection.
region-end
(region-end)
Return end position of text selection.
;; return the position of the beginning of region
(region-beginning)
point-min
(point-min)
Return the start position of visible buffer. (respect narrow-to-region)
point-max
(point-max)
Return the end position of visible buffer. (respect narrow-to-region)

(info "(elisp) Positions")

Move Cursor

goto-char
(goto-char POSITION)
Move cursor to a given position.
;; move cursor to position 392
(goto-char 392)
forward-char
(forward-char &optional N)
Move cursor forward by N characters.
backward-char
(backward-char &optional N)
Move cursor backward by N characters.
;; move cursor by 9 chars
(forward-char 9)
(backward-char 9)
beginning-of-line
(beginning-of-line &optional N)
Move cursor to beginning of physcal line. If N is given, move forward N-1 lines first.
end-of-line
(end-of-line &optional N)
Move cursor to end of physcal line.
;; move to beginning of line
(beginning-of-line)

[see Elisp: Functions on Line]

Search Text and Move Cursor

search-forward
(search-forward STRING &optional BOUND NOERROR COUNT)
Move cursor forward by searching for given string. Cursor stops at end of matched string.
Also search-backward. Cursor stops at beginning of matched string.
;; move cursor to the location of string "cat"
;; returns the new position
(search-forward "cat")
(search-backward "cat")
re-search-forward
(re-search-forward REGEXP &optional BOUND NOERROR COUNT)
Move cursor forward by searching for regex pattern. Cursor stops at end of matched pattern.
Also re-search-backward. Cursor stops at beginning of matched string.
;; move cursor forward to the location matched by regex. returns the new position
(re-search-forward myRegex)
skip-chars-forward
(skip-chars-forward STRING &optional LIM)
Move cursor forward by skip a given set of characters. Also skip-chars-backward.
;; move cursor to the first char that's not a newline or tab. Returns the distance traveled
(skip-chars-forward "\n\t")

(info "(elisp) Motion")

[see Emacs: Regex Tutorial]

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 finished.

save-excursion
(save-excursion &rest BODY)
Run BODY, and restore cursor position and buffer to user's original. See also: Elisp: Save narrow-to-region
;; preserve {point, mark, current buffer}
(save-excursion
  ;; lisp code here
)

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp

Basics

Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc