Emacs: Move Cursor to Beginning of Line or Paragraph 🚀

By Xah Lee. Date: . Last updated: .

Here's a command that move cursor to beginning of line (of visible char) or previous text block.

(defun xah-beginning-of-line-or-block ()
  "Move cursor to beginning of indent or line, end of previous block, in that order.

If `visual-line-mode' is on, beginning of line means visual line.

URL `http://xahlee.info/emacs/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Created: 2018-06-04
Version: 2023-10-04"
  (interactive)
  (let ((xp (point)))
    (if (or (eq (point) (line-beginning-position))
            (eq last-command this-command))
        (when (re-search-backward "\n[\t\n ]*\n+" nil :move)
          (skip-chars-backward "\n\t ")
          ;; (forward-char)
          )
      (if visual-line-mode
          (beginning-of-visual-line)
        (if (eq major-mode 'eshell-mode)
            (progn
              (declare-function eshell-bol "esh-mode.el" ())
              (eshell-bol))
          (back-to-indentation)
          (when (eq xp (point))
            (beginning-of-line)))))))

(defun xah-end-of-line-or-block ()
  "Move cursor to end of line or next block.

• When called first time, move cursor to end of line.
• When called again, move cursor forward by jumping over any sequence of whitespaces containing 2 blank lines.
• if `visual-line-mode' is on, end of line means visual line.

URL `http://xahlee.info/emacs/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Created: 2018-06-04
Version: 2023-10-04"
  (interactive)
  (if (or (eq (point) (line-end-position))
          (eq last-command this-command))
      (re-search-forward "\n[\t\n ]*\n+" nil :move)
    (if visual-line-mode
        (end-of-visual-line)
      (end-of-line))))

For these commands to be useful, you need to give them easy keys. [see Emacs Keys: Define Key]

These commands are great because, in GNU Emacs's default, you press a key to move to beginning of line, it's dead there. If you hold the key, nothing happens. This is a waste of key. With these commands, you can move to beginning/end of line, or hold the key to continuously move to previous/next paragraphs. [see Keybinding Design, Fast-Repeat Commands]

2017-05-10 thanks to @stasvlasov.