More ways to edit text

Table of Contents

Search and replace

The following settings are useful (alread implemented in aprl-config).

(setq search-highlight t)		;; Highlight search object
(setq query-replace-highlight t)	;; Highlight query object
(setq mouse-sel-retain-highlight t)	;; Keep mouse highlighting

search

Another way to move around the buffer is through incremental searching (Yegge effective Emacs).

type pattern backward forward
incremental string C-r (isearch-backward) C-s (isearch-forward)
incremental regex C-M-r (isearch-backward-regexp) C-M-s (isearch-forward-regexp)
by region or buffer string (search-backward) (search-forward)
by region or buffer regex (search-backward-regexp) (search-forward-regexp)

Selected options (see C-h f isearch-forward for more):

task keybinding
cancel last input item from end of search string DEL
exit; leave point at location found RET
toggle search case-sensitivity M-c
toggle regular-expression mode M-r
search again forward C-s
search again backward C-r

Non-incremental search options are seldom used, but see high-lock-mode for highlighting all regex matches in text.

replace

See help for respective functions for more information.

type string regex
incremental M-% (query-replace) C-M-% (query-replace-regex)
by region or buffer (replace-string) (replace-regex)

Special characters:

character key strokes
tab C-q <tab>
newline C-q C-j

hi-lock-mode

Invoke with M-x hi-lock-mode. hi-lock-mode is an extension of font-lock-mode and can be used to highlight certain patterns (or lines containing those patterns) in the buffer:

task keybinding function
highlight text matching REGEXP using FACE color (default is provided) M-s h r REGEXP <RET> FACE <RET> (highlight-regexp)
Unhighlight REGEXP M-s h u REGEXP <RET> (unhighlight-regexp)

More interactive functions

keyboard macros

description keybinding
start C-x (
end C-x )
apply macro NUM times C-u NUM C-x e

rectangle commands

Delete/add "columns"/character strings in text.

keybinding function
C-x r o (open-rectangle)
C-x r d (delete-rectangle)
C-x r k (kill-rectangle)
C-x r y (yank-rectangle)
C-x r t (string-rectangle)

CUA-mode has a more advanced version of this–especially in visual representation of the selected rectangle area, but I only enable it to enumerate columns because it otherwise hijacks my keybindings.

  1. Enable CUA mode (M-x cua-mode)
  2. highlight a single(?) column.
  3. C-<return>
  4. M-n (for seq, not M-i for incr); enter arguments/parameters
  5. Disable CUA mode (M-x cua-mode)

align

Align text around some symbol. align-regexp is generally more flexible. Can use align with customized align-rules-list for different minor-modes but requires more investment up-front.

registers

Registers can be used to store

  • specific points in the buffer,
  • a block of text (for repeated insert), and
  • window/frame configuration (see also winner-mode and elscreen)
keybinding function
C-x r SPC r (point-to-register)
C-x r j r (jump-to-register)
C-x r s r (copy-to-registr)
C-x r i r (insert-register)
C-x r w r (window-configuration-to-register)
C-x r f r (frame-configuration-to-register)

(Also use C-x r j r to restore window or frame configuration).

Registers can also be used to save rectangles and file names.

Note that the C-x r prefix is also used for the rectangle commands.

Sometimes when using beamer, I will load the following text into the first 5 registers:

(mapc 'eval
      (list
       (set-register 49 "\\begin{frame}\n  \\frametitle{}")                     ;; register 1
       (set-register 50 "\\begin{columns}[t]\n\\column{.5\\textwidth}")         ;; register 2
       (set-register 51 "\\column{.5\\textwidth}")                              ;; register 3
       (set-register 52 "\\centerline{\\includegraphics[width=\\textwidth]{}}") ;; register 4
       (set-register 53 "\\flushright\n\\vspace{-5ex}\n{\\footnotesize{}}")))   ;; register 5

word expansions

See dabbrev, autocomplete, hippie-expand.

Type M-/ to let emacs autocomplete word based on other words present in open buffers.

In aprl-config, I have mapped M-/ to (hippie-expand).

visual selection

Toggle selection of units (word, sentence, paragraph, etc.) with expand-region.

(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)

Editing with an external script

pass region to external script

Use (shell-command-on-region) interactively (M-|) with universal prefix (C-u) argument (C-u M-|). Without the prefix (shell-command-on-region) will print output to minibuffer.

I create a file called processtext.py as an example:

import sys

txt = sys.stdin.read()
sys.stdout.write(txt.replace('\n','\\\\\n'))

Then I highlight a region of text in my emacs buffer and type C-u M-|; in the minibuffer I provide the shell command

Shell command on region: python processtext.py

And the output replaces the text in the region in the current buffer.

More regularly used scripts can be wrapped in an emacs lisp function.

text processing using emacs lisp

The major advantages for using emacs lisp (e.g., by writing a function in the *scratch* buffer) rather than applying (shell-command-on-region) as above would be

  • no need to commit to a script file on the hard disk (somehow seems to pose a disproportionately significant deterrent, if only mentally for on-the-fly tasks)
  • process text between point and mark rather than by line-by-line (or on full buffer, possibly after (narrow-to-region))
  • transform text 'under cursor' or by word, line, or sentence in addition to operating on marked regions

One disadvantage may be that some buffer-management (especially creation, moving, and destruction of temporary ones) may be required.

Some helpful tutorials on text-processing with emacs lisp:



Emacs pages: main aprl-config


Generated by Org-mode 9.2 with Emacs 26