Emacs: Move Cursor to Beginning of Line/Paragraph

By Xah Lee. Date: . Last updated: .

Here's a command that moves cursor to beginning of line or beginning of paragrpah.

  1. When called first time, move cursor to beginning of line.
  2. When called again, move cursor to beginning of paragraph.
  3. When called again, move cursor to beginning of previous paragraph.
(defun xah-beginning-of-line-or-block ()
  "Move cursor to beginning of line, or beginning of current or previous text block.

• When called first time, move cursor to beginning of line.
• When called again, move cursor to beginning of paragraph.
• When called again, move cursor to beginning of previous paragraph.

URL `http://ergoemacs.org/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Version 2017-01-17"
  (interactive)
  (if (or (equal (point) (line-beginning-position))
          (equal last-command this-command ))
      (if (re-search-backward "\n[\t\n ]*\n+" nil "NOERROR")
          (skip-chars-backward "\n\t ")
        (goto-char (point-min)))
    (beginning-of-line)))
(defun xah-end-of-line-or-block ()
  "Move cursor to end of line, or end of current or next text block.

• When called first time, move cursor to end of line.
• When called again, move cursor to end of paragraph.
• When called again, move cursor to end of next paragraph.

URL `http://ergoemacs.org/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Version 2017-01-17"
  (interactive)
  (if (or (equal (point) (line-end-position))
          (equal last-command this-command ))
      (re-search-forward "\n[\t\n ]*\n+" nil "NOERROR" )
    (end-of-line)))

For these commands to be useful, you need to give it a key. For example:

(global-set-key (kbd "C-a") 'xah-beginning-of-line-or-block)
(global-set-key (kbd "C-e") 'xah-end-of-line-or-block)

〔►see Emacs: How to Define Keys

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

Enhanced Editing Commands Topic

  1. Emacs: Select Line, between Quotes, Extend Selection
  2. Emacs: Copy/Cut Current Line If No Selection
  3. Emacs: Copy/Cut All or Selection
  4. Emacs: Paste or Paste Previous
  5. Emacs: Delete Whitespace around Cursor
  6. Emacs: Move Cursor to Beginning of Line/Paragraph
  7. Emacs: Move Cursor by Text Block
  8. Emacs: Delete Paragraph, Text Block
  9. Emacs: Toggle Comment Current Line
  10. Emacs: Insert Date Time

Working with Brackets Topic

  1. Emacs: Select Line, between Quotes, Extend Selection
  2. Emacs: Insert Brackets by Pair
  3. Emacs: Move Cursor to Bracket/Quote
  4. Emacs: Delete Backward Char or Bracket Text
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs