Elisp: Cursor Position Functions

By Xah Lee. Date: . Last updated: .

Here's the most useful functions for cursor position.

You should memorize these by heart.

Get Cursor Position

point → return cursor's current position.

;; returns cursor's current position
;; beginning of buffer is 1

region-beginning → return start position of text selection.

region-end → return end position of text selection.

;; returns the position of the beginning/end 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)

;; return the beginning position of buffer

;; returns the position for the end of buffer, respect narrow-to-region

(info "(elisp) Positions")

Move Cursor

goto-char → move cursor to a given position.

;; move cursor to position 392
(goto-char 392)

forward-char → move cursor by given number of characters.

;; move cursor by 9 chars
(forward-char 9)
(backward-char 9)

beginning-of-line → move cursor to beginning of physcal line.

end-of-line → move cursor to end of physcal line.

;; move to beginning/end of line

[see Elisp: Functions on Line]

Search Text and Move Cursor

search-forward → move cursor forward by searching for given string. Cursor stops at end of matched string.

;; move cursor to the location of string "cat"
;; returns the new position
(search-forward "cat")
(search-backward "cat")

re-search-forward → move cursor forward by searching for regex pattern. Cursor stops at end of matched pattern.

;; move cursor to the location matched by regex
;; returns the new position
(re-search-forward myRegex)
(re-search-backward myRegex)

skip-chars-forward → move cursor forward by skip a given set of characters.

;; move cursor to the first char that's not a newline or tab
;; Returns the distance traveled
(skip-chars-forward "\n\t")
(skip-chars-backward "\n\t")

Note: the search functions are more generally useful than skip-chars-forward.

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

Use save-excursion to save {point, mark, current buffer}:

;; preserve {point, mark, current buffer}
  ;; lisp code here involving moving cursor, mark, changing buffer.

Use save-restriction to save user's narrow-to-region or narrowing by your code:

;; preserve user's narrow-to-region
;; useful when you want to narrow-to-region in your code to work in just that region
  (narrow-to-region pos1 pos2)
  ;; lisp code here

(info "(elisp) Positions")

Elisp Basic Functions

  1. Elisp: Cursor Position Functions
  2. Elisp: Text Editing Functions
  3. Elisp: String Functions
  4. Elisp: Buffer and File Functions
  5. Elisp: File and Directory Functions
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.