Elisp: Move Cursor

By Xah Lee. Date: . Last updated: .

Move Cursor to a Position

goto-char

(goto-char POSITION)

Move cursor to a given position.

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

Move by Char Count

;; move cursor back by 3 chars
(backward-char 3)

Move by Word

Move by Line

Move Cursor by Skip Chars

skip-chars-forward

(skip-chars-forward STRING &optional maxPos)

  • Move cursor forward by skipping chars in STRING.
  • but do not go beyond maxPos
  • Returns the distance traveled, either zero or positive.

STRING is like the inside of a [chars] in a Regular Expression Syntax

;; move cursor, by skipping whitespace
(skip-chars-forward " \n\t")

;; mover cursor, by skipping alphanumeric and hyphen lowline
(skip-chars-forward "-_a-zA-Z0-9")
skip-chars-backward

(skip-chars-backward STRING &optional LIM)

similar to skip-chars-forward but backward.

;; move cursor backward, skip space tab newline
(skip-chars-backward " \t\n")
;; skip a to z and hyphen
(skip-chars-backward "-a-z")

Move Cursor by Search Text

These search functions are used for searchinng text and find replace, but they also move cursor and return cursor position.

Reference

Elisp, text processing functions