Emacs: Select Line, between Quotes, Extend Selection ๐
Master these text selection commands, and you almost never need to manually mark a region.
Select Text between Quotes/Brackets
Select text inside quotes is one of the most frequently needed operation in programing languages code.
(defun xah-select-text-in-quote () "Select text between the nearest left and right delimiters. Delimiters here includes the following chars: \"`<>(){}[]โโโโโนโบยซยปใใใใใใใใใใใใใใ๏ผ๏ผ This command select between any bracket chars, does not consider nesting. For example, if text is (a(b)cโฎ) the selected char is โcโ, not โa(b)cโ. URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html' Version 2020-11-24 2021-07-11" (interactive) (let ( $skipChars $p1 ) (setq $skipChars "^\"`<>(){}[]โโโโโนโบยซยปใใใใใใใใใใใใใใ๏ผ๏ผใใ") (skip-chars-backward $skipChars) (setq $p1 (point)) (skip-chars-forward $skipChars) (set-mark $p1)))
Select Current Line
This command selects current line.
If there's already a selection, extend selection downward by line.
(defun xah-select-line () "Select current line. If region is active, extend selection downward by line. URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html' Version 2017-11-01" (interactive) (if (region-active-p) (progn (forward-line 1) (end-of-line)) (progn (end-of-line) (set-mark (line-beginning-position)))))
Select Current Block
This lets you select current block of text. (a block here is text between empty lines.)
If there's already a selection, extend selection downward by block.
(defun xah-select-block () "Select the current/next block of text between blank lines. If region is active, extend selection downward by block. URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html' Version 2019-12-26" (interactive) (if (region-active-p) (re-search-forward "\n[ \t]*\n" nil "move") (progn (skip-chars-forward " \n\t") (when (re-search-backward "\n[ \t]*\n" nil "move") (re-search-forward "\n[ \t]*\n")) (push-mark (point) t t) (re-search-forward "\n[ \t]*\n" nil "move"))))
Extend Selection
Here's a multi-purpose command that select different text depending on where cursor is, and if called repeatedly, extend selection.
This command is currently most useful in lisp code. Or, place cursor on a bracket and select the whole including bracket and text in between. (bracket here includes parenthesis and any type of quotation marks.)
(defun xah-extend-selection () "Select the current word, bracket/quote expression, or expand selection. Subsequent calls expands the selection. when there is no selection, โข if cursor is on a any type of bracket (including parenthesis, quotation mark), select whole bracketed thing including bracket โข else, select current word. when there is a selection, the selection extension behavior is still experimental. But when cursor is on a any type of bracket (parenthesis, quote), it extends selection to outer bracket. URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html' Version: 2020-02-04 2022-05-16" (interactive) (if (region-active-p) (progn (let (($rb (region-beginning)) ($re (region-end))) (goto-char $rb) (cond ((looking-at "\\s(") (if (eq (nth 0 (syntax-ppss)) 0) (progn ;; (message "left bracket, depth 0.") (end-of-line) ; select current line (set-mark (line-beginning-position))) (progn ;; (message "left bracket, depth not 0") (up-list -1 t t) (mark-sexp)))) ((eq $rb (line-beginning-position)) (progn (goto-char $rb) (let (($firstLineEndPos (line-end-position))) (cond ((eq $re $firstLineEndPos) (progn ;; (message "exactly 1 line. extend to next whole line." ) (forward-line 1) (end-of-line))) ((< $re $firstLineEndPos) (progn ;; (message "less than 1 line. complete the line." ) (end-of-line))) ((> $re $firstLineEndPos) (progn ;; (message "beginning of line, but end is greater than 1st end of line" ) (goto-char $re) (if (eq (point) (line-end-position)) (progn ;; (message "exactly multiple lines" ) (forward-line 1) (end-of-line)) (progn ;; (message "multiple lines but end is not eol. make it so" ) (goto-char $re) (end-of-line))))) (t (error "%s: logic error 42946" real-this-command )))))) ((and (> (point) (line-beginning-position)) (<= (point) (line-end-position))) (progn ;; (message "less than 1 line" ) (end-of-line) ; select current line (set-mark (line-beginning-position)))) (t ;; (message "last resort" ) nil)))) (progn (cond ((looking-at "\\s(") ;; (message "left bracket") (mark-sexp)) ; left bracket ((looking-at "\\s)") ;; (message "right bracket") (backward-up-list) (mark-sexp)) ((looking-at "\\s\"") ;; (message "string quote") (mark-sexp)) ; string quote ;; ((and (eq (point) (line-beginning-position)) (not (looking-at "\n"))) ;; (message "beginning of line and not empty") ;; (end-of-line) ;; (set-mark (line-beginning-position))) ((or (looking-back "\\s_" 1) (looking-back "\\sw" 1)) ;; (message "left is word or symbol") (skip-syntax-backward "_w" ) ;; (re-search-backward "^\\(\\sw\\|\\s_\\)" nil t) (push-mark) (skip-syntax-forward "_w") (setq mark-active t) ;; (exchange-point-and-mark) ) ((and (looking-at "\\s ") (looking-back "\\s " 1)) ;; (message "left and right both space" ) (skip-chars-backward "\\s " ) (set-mark (point)) (skip-chars-forward "\\s ")) ((and (looking-at "\n") (looking-back "\n" 1)) ;; (message "left and right both newline") (skip-chars-forward "\n") (set-mark (point)) (re-search-forward "\n[ \t]*\n")) ; between blank lines, select next block (t ;; (message "just mark sexp" ) (mark-sexp) (exchange-point-and-mark)) ;; ))))
You should give these commands keys. [see Emacs: How to Define Keys].
I suggest these keybinding:
xah-select-block
ใAlt+6ใxah-select-line
ใAlt+7ใxah-extend-selection
ใAlt+8ใxah-select-text-in-quote
ใAlt+9ใ
xah-extend-selection
is even more useful together with commands that move cursor to brackets or quotes. [see Emacs: Move Cursor to Bracket ๐]
The above commands are also in ergoemacs-mode [https://ergoemacs.github.io/ ] and xah-fly-keys. [see Emacs: Xah Fly Keys]
Emacs, Work with Brackets
- Emacs: Highlight Brackets
- Emacs: Insert Bracket Pairs, electric-pair-mode
- Emacs: Insert Brackets by Pair ๐
- Emacs: Delete Brackets by Pair ๐
- Emacs: Move Cursor to Bracket ๐
- Emacs: Jump to Matching Bracket ๐
- Emacs: Change Brackets/Quotes ๐
- Emacs: Navigate Lisp Code as Tree
- Emacs: Select Line, between Quotes, Extend Selection ๐
- Emacs: Xah Elisp Mode
Emacs Modernization
- Emacs Modernization: Simple Changes Emacs Should Adopt
- Why Emacs Keys are Painful
- Emacs: Problems of the Scratch Buffer
- Emacs M-key Notation vs Alt+key Notation
- Emacs Menu Usability Problem
- Emacs Mode Line Problem
- Emacs cua-mode Problems
- Emacs: Inconsistency of Search Features
- Problems of grep in Emacs
- Emacs: Usability Problems of Mode Documentation
- Problems of Emacs Manual
- Emacs Manual Sucks by Examples
- Emacs: kill-buffer Induces Buffer Accumulation
- Emacs Spell Checker Problems
- Emacs Form Feed ^L
- Emacs: Single Key to Delete Whole Line
- Emacs HTML Mode Sucks
- Emacs Does Not Support Viewing Images Files In Windows
- Emacs Should Adopt HTML as Texinfo Replacement
- Emacs Should Support HTML Mail
- Problems of Emacs's โmanโ Command
- Emacs Lisp Mode Syntax Coloring Problem
- Emacs AutoHotkey Mode Problems
- Emacs Lisp: Ban Syntax Table
- Emacs: Make elisp-index-search use Current Symbol
- Emacs GNU Texinfo Problems; Invalid HTML
- A Record of Frustration in IT Industry; Disappearing FSF URLs, 2006
- Emacs Manual Node Persistency Issues
- Emacs: dired-do-query-replace-regex Replace ALL (fixed)
- Problems of Emacs Supporting Obsolete Systems
- Emacs Lisp: Function to Copy/Delete a Dir Recursively (fixed)
- Thoughts on Common Lisp Scheme Lisp Based Emacs
- Text Editors Popularity and Market Research
- Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)
- Emacs: Toggle Letter Case ๐
- Emacs: Select Line, between Quotes, Extend Selection ๐
- Emacs: isearch Current Word ๐
- Emacs: Reformat Lines (Hard-Wrap lines, fill) ๐