We frequently need to edit the beginning or end of a line. The text may also be arranged in columns. We can then resort to keyboard macros, but there's a less laborious approach.
Suppose we're editing this excerpt:
foo = 1
bar = 'a'
foobar = foo + bar
First let's prepend const
using Emacs' rectangular (column) editing.
- put cursor before
foo
and set mark withC-<SPC>
(or use rectangular mark withC-x <SPC>
). - put cursor before
foobar
C-x r t const<space> <RET>
const foo = 1
const bar = 'a'
const foobar = foo + bar
A special case. To prepend a column of numbers, select the region and C-x r N
(followed by inserting a column of dots).
1. list item
2. list item
3. list item
- We first delete a column of text with
C-x r k
. - We then paste the column at point with
C-x r y
.
From
| Tables | Are | Cool |
|----------|:-------------:|------:|
| col 1 is | left-aligned | $1600 |
| col 2 is | centered | $12 |
| col 3 is | right-aligned | $1 |
to
| Tables | Cool | Are |
|----------|:------:|-------------:|
| col 1 is | $1600 | left-aligned |
| col 2 is | $12 | centered |
| col 3 is | $1 |right-aligned |
- Align on whitespace:
C-u M-x align
lastname firstname
Smith John 33-88
Gere Alan 24-23
Verne Bill 12-34
- Align on an arbitrary regexp:
M-x align-regexp <regexp>
x match {
case 0 => "zero"
case 11 => "eleven"
case _ => "unknown"
}
This is a more general case, since the text may not be neatly aligned in columns.
Select the region, then search/replace end of line regexp with
M-x replace-regexp <RET> $ <RET> <string>
const foo = 1;
const bar = 'a';
const foobar = foo + bar;
Use query-replace-regexp
(C-M-%
) for interactive search and replace .