Elisp: Functions on Line

By Xah Lee. Date: . Last updated: .

Get Position of Beginning of Line

pos-bol
(pos-bol &optional N)

return beginning of line position.

new in Emacs 29 (Released 2023-07)

pos-eol
(pos-eol &optional N)

return end of line position.

new in Emacs 29 (Released 2023-07)

line-beginning-position
(line-beginning-position &optional N)

return beginning of line position, but stop at fields.

(fields are text regions that have Text Property of field. Used for things such as minibuffer prompt text.)

If N is 1, it means current line. 2 means next line. -1 means last line, etc.

If the scan reaches the end of the buffer, return that position.

;; return line beginning position
(line-beginning-position)
line-end-position
(line-end-position &optional N)

return end of line position, but stop at fields.

Move Cursor to Beginning of Line

beginning-of-line
(beginning-of-line &optional N)

Move point to beginning of current line, but stop at fields. To ignore field, use (forward-line 0)

N means move forward by N - 1 lines first. N can be negative. If point reaches the beginning or end of buffer, it stops there.

;; move cursor to beginning of current line
(beginning-of-line)
;; better than (goto-char (line-beginning-position))
end-of-line
(end-of-line &optional N)

like beginning-of-line but end of line.

💡 TIP: Do not use (search-forward "\n") for moving cursor to end of line. Because you'll have special cases if the line is at the end of buffer and doesn't have a newline char. It is also slower.

💡 TIP: Do not use move-beginning-of-line or move-end-of-line. Because these are designed for interactive use.

Move Cursor to Next Line, Previous Line

forward-line
(forward-line &optional N)
  • Move cursor to the next line, or N lines.
  • N default to 1.
  • If N is negative, move in the other direction.
  • Cursor ends at the beginning of line.
;; move cursor to the beginning of previous line
(forward-line -1)

💡 TIP: Do not use next-line or previous-line. Because these are for interactive use. Their behavior changes depending on the variable line-move-visual.

Check If Cursor is at Beginning of Line, End of Line

bolp
Return t if point is at the beginning of a line.
eolp
Return t if point is at the end of a line.

Get Current Line as String

To grab current line, use:

(setq myLine
      (buffer-substring-no-properties
       (line-beginning-position)
       (line-end-position)
       ))

💡 TIP: Do not use (thing-at-point 'line). thing-at-point normally include the newline char, but if the line is at the end of buffer, then it won't. Also, thing-at-point is complex elisp code and is slower. [see Elisp: thing-at-point]

Screen Lines

If you want to move cursor across lines as defined by the screen (wrapped at edge of screen), you can use these.

What Character Does Emacs Use for Newline

In emacs buffer, newline char is "\n", in any operating system (Mac, Linux, Microsoft Windows ).

~2010 Thanks to Uday S Reddy [http://www.cs.bham.ac.uk/~udr/] and Alan Mackenzie [http://www.emacswiki.org/emacs/AlanMackenzie] for tips.

Elisp, Process Lines

Elisp, text processing functions