Emacs: Select Line, Block, in Quote, Extend Selection

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows many useful emacs commands for text selection.

Emacs Text Selection Commands

Emacs has the following commands to select text.

(info "(emacs) Marking Objects")

These are not efficient.

The following set is a possible replacement.

Select Text in Quote

Select text inside quotes is one of the most frequently needed operation in programing languages, also useful in English.

This command select just text inside quotes. (it does not deal with nested quotes)

(defun xah-select-text-in-quote ()
  "Select text between the nearest left and right delimiters.
Delimiters here includes the following chars: \"<>(){}[]“”‘’‹›«»「」『』【】〖〗《》〈〉〔〕()
This command does not properly deal with nested brackets.
URL `http://ergoemacs.org/emacs/modernization_mark-word.html'
Version 2015-05-16"
  (let (ξp1
        (ξskipChars "^\"<>(){}[]“”‘’‹›«»「」『』【】〖〗《》〈〉〔〕()"))
    (skip-chars-backward ξskipChars)
    (setq ξp1 (point))
    (skip-chars-forward ξskipChars)
    (setq ξp2 (point))
    (set-mark ξp1)))

Select Current Line

A “line” is a frequent semantic unit in source code, such as C, C++, Java, JavaScript, Python. Here's a command that select current line.

(defun xah-select-current-line ()
  "Select current line.
URL `http://ergoemacs.org/emacs/modernization_mark-word.html'
Version 2015-02-07
  (set-mark (line-beginning-position)))

Select Current Block

This lets you select current block of text by a single keystroke. (a block here is text between empty lines. It's similar to emacs's “paragraph” concept, except it's not major-mode dependent, so the behavior is predictable anywhere.)

(defun xah-select-current-block ()
  "Select the current block of text between blank lines.
URL `http://ergoemacs.org/emacs/modernization_mark-word.html'
Version 2015-02-07
  (let (p1 p2)
      (if (re-search-backward "\n[ \t]*\n" nil "move")
          (progn (re-search-forward "\n[ \t]*\n")
                 (setq p1 (point)))
        (setq p1 (point)))
      (if (re-search-forward "\n[ \t]*\n" nil "move")
          (progn (re-search-backward "\n[ \t]*\n")
                 (setq p2 (point)))
        (setq p2 (point))))
    (set-mark p1)))

You should give these commands keys. 〔➤ Emacs: How to Define Keys〕.

I suggest 【Alt+n】 or 【Ctrl+n】.

Extend Selection

Select current word, then, repeated call extends selection by semantic unit.

(defun xah-semnav-up (φarg)
"Called by `xah-extend-selection'.

URL `http://ergoemacs.org/emacs/modernization_mark-word.html'
Version 2015-11-13.
Written by Nikolaj Schumacher, 2008-10-20. Released under GPL 2"
  (interactive "p")
  (when (nth 3 (syntax-ppss))
    (if (> φarg 0)
          (skip-syntax-forward "^\"")
          (goto-char (1+ (point)))
          (setq φarg (1- φarg) ))
      (skip-syntax-backward "^\"")
      (goto-char (1- (point)))
      (setq φarg (1+ φarg) )))
  (up-list φarg))

(defun xah-extend-selection (φarg &optional φincremental-p)
  "Select the current word.
Subsequent calls expands the selection to larger semantic unit.

This command works mostly in lisp syntax.
URL `http://ergoemacs.org/emacs/modernization_mark-word.html'
Version 2015-11-13.
Written by Nikolaj Schumacher, 2008-10-20. Released under GPL 2."
   (list (prefix-numeric-value current-prefix-arg)
         (or (use-region-p)
             (eq last-command this-command))))
  (if φincremental-p
        (xah-semnav-up (- φarg))
        (mark-sexp -1))
    (if (> φarg 1)
        (xah-extend-selection (1- φarg) t)
      (if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
          (goto-char (match-end 0))
        (unless (memq (char-before) '(?\) ?\"))
      (mark-sexp -1))))

I suggest 【Alt+8】 for the command. Pressing it once will select the current whole word. Press it again will extend the selection to the next outer parens.

The “extend-selection” can be used to select quoted text. If the quoted text has more than one word, you need to call it twice. “extend-selection” will select including the quote chars.

The above commands are also in ergoemacs-mode 〔https://ergoemacs.github.io/〕 and xah-fly-keys. 〔➤ Emacs: Xah Fly Keys: More Efficient Than Vim

This command is most useful together with commands that move cursor to brackets or quotes. 〔➤ Emacs: Move Cursor to Brackets, Quotes

Replacing Emacs Text Selection Scheme

Here is suggestion of a scheme that may replace or supplement the above system.

We create a command that select the whole word. When repeated, it should select the next larger syntactic unit. In human languages, that would be sentence, then paragraph, then whole buffer. In computer languages, the sequence would be: current identifier or token, current expression, current construct (or line), current block or defun. If the language is lisp, this simply means extending the selection to the next outer parens. For a illustrated example of this, see: A Text Editor Feature: Extend Selection By Semantic Unit.

This extend-selection scheme with transient-mark-mode on, should simplify and replace the functionality of {mark-word, mark-sexp, mark-paragraph, mark-defun}. With just one command to remember, and more efficient to operate.

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus