Elisp: Get Line Begin/End Position, or Move To
Get Position of Beginning of Line
pos-bol-
(pos-bol &optional N)return beginning of line position.
new in Emacs 29 (date 2023)
pos-eol-
(pos-eol &optional N)return end of line position.
new in Emacs 29 (date 2023)
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-linebut 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 Line Number
line-number-at-pos-
(line-number-at-pos &optional POSITION ABSOLUTE)- Return the line number of cursor position.
- Respect Emacs: Narrow to Region.
- if
ABSOLUTEis true, return the disregard narrow to region.
(line-number-at-pos )
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: Get Text at Cursor (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.
next-lineprevious-lineline-move-visual- line-move-visual → A variable that controls whether
next-lineandprevious-linemove by newline char or screen.
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
- 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: Get Line Begin/End Position, or Move To
- Elisp: Get Text at Cursor (thing-at-point)
- Elisp: Get Text Block 📜
- Elisp: Save narrow-to-region