Jim Cheung

Ed Mastery: The Standard Unix Text Editor

reading notes of Ed Mastery

Chapter 0: Introduction

Why add encryption to ed? That's what crypt(1) is for.

Chapter 1: Ed Essentials

Enter H to toggle verbose error messages

Enter P to toggle prompt set prompt when starting ed using the -p flag:

$ ed -p "> " foo.txt

Ed supports 3 different ways to get into insert mode: append, insert before current line, and change current line.

Enter append mode with the a command

when you done editing, enter . and enter

Insert text before the current line with the i command

Change current line with c command

CTRL-C aborts command

Saving and Exiting

Write the buffer back to file with w command

exit ed with the q command

you can combine the write and quite command: wq

Chapter 10: Addresses

Many ed commands expect you to put the addresses before the command: like 4d, 3,5c

setting your address

when you first open a file, current address is the last line of the file

the special address $ always refers to the last line of a file

to set the address to a line number, just enter that number, like 6

when you change the address, ed automatically prints the line

Advance the address one line by + command, to forward multiple lines, give + and the number of lines, like: +3

Walk backwards through the buffer using - or ^ commands (GNU ed has dropped ^ command)

Finding your address

= prints an address, it defaults to the last address of the buffer

the period . represents the current address.

To see your current address, enter .=

Address Ranges

Ed can perform operations on multiple lines through two special addresses, , and ;

, (also available as %) represents the whole file

,p prints the entire file

the ; represents the current address to the end of the file

;p prints from current line to end of file

Relative Addresses

A relative address is a certain number of lines before of after the current address, indicated by + or - sign

-2,+2p: prints before/after 2 lines

-,+p: gives one line of context

Scrolling

the z command tells ed to scroll from next address to as far as the terminal allows

to scroll a reduced number of lines, enter the number after z:

11z3: shows 11 and 3 lines after it

the z command changes the current address to the last line shown

Displaying Addresses

The n command shows line numbers

,n: shows entire buffer with line numbers

Viewing Trailing Space

use l suffix after n or p to put a $ symbol at the end of each line

Bookmarks

Bookmark is named after a single lower case letter

use k to assign a bookmark single quote ' to return to a bookmark

ka: assign current line to bookmark a

'a: return to bookmark a

Chapter 11: Text Editing

Changing Lines

c to change current line, 12c to change at line number

Inserting Amidst the File

append a adds new lines after current address, command i inserts new lines before.

same as changeing line, can take line number / range: 12i, 23a

Moving Text

move command m requires two addresses, before and after the command

6m2: move line 6 to after line 2

Deleting Lines

use d command to delete addresses

3d: delete line 3

Undo

u command to undo

Ed has only one level of undo. to redo, undo the undo

Regular expression operations what affect multiple lines are a single command

Inserting Text from Files

The r command read from another file

0r another_file: read another_file and put it after address 0

Joining Lines

j command join lines, uses syntax similar to n command

1,2j: join line 1 and 2

splitting lines requires using a substitution (substitution with \n does not work, use backslash and a enter)

Copying Lines

The t (transfer) command, put address/range you want to copy before and destination after

3t$: copy line 3 to end of file

duplicate current line using .t, useful for perform transformations while not touching the original line.

Appending Lines to Another File

use W (upper case W) to append addresses in your buffer to another file.

5,9W another_file: append line 5-9 to another_file

Chapter 100: File Management and Shell Escapes

The Default Filename

use the f command to set filename

single f show current filename

The w command can also set the default filename:

w foo: write to foo and set filename to foo

single w save to default filename

Switching Files

use e (edit) command to switch file

if you have unsaved changes, ed will warn you, either use w to save changes or use a second e to throw away buffer

or use E to skip the confirmation step

Shell Escapes

A shell escape runs a single command outside of the ed shell, displays the output, then returns to editing.

! to trigger a shell escape.

! cal

!! to repeat last shell escape you ran

r command to read output of a shell escape into your file

r !cal

or append after line number:

21r !cal

Send to a Program

Write the buffer to a shell escape:

w !wc

Starting with an Escape

the e command lets you create a new buffer pre-populated with the output of a shell escape.

e !ifconfig

this does not change the default filename, use f or w to give a new filename

Chapter 101: Regular Expressions and Searches

Searching in Ed

Entering a regex as a command to search:

/the/

use // to search forward for the same regex

use g command in front to search for a regex globally:

g/re/p: search all /re/ and print them

search a subset of the buffer by putting addresses before g

1,10g/re/n

to search backward from current address, use ? instead of /:

?re?n

to repeat backward search, use ??

Interactive Searching

use G command to tell ed to pause after each match:

G/re/n

here, hit Enter to continue, or a command to start editing

Inverted Matches

use v command:

v/re/n

the interactively edit each non-matching lines, use V (upper V) command

Character Classes

perform case-insensitive searches:

g/[Tt][Hh][Ee]/n

[a-zA-Z0-9]

if you need a class to include -, put it first: [-/[]]

Anchors

^ anchors the regex to the front of the line $ anchors it to the end

g/^t/n

Inverted Classes

the first character in an inverted class must be a caret ^

g/e[^t]/n

Multiple Matches and Wildcards

Use {} to match a character a certain number of times, put the count directly after the character:

e{2}: matches ee

Normally ed treats {} in a regex as characters to match, escaping the braces tell ed to treat them special:

g/e{2}/n

If you want to match a range of characters, express it as e{2,5}

or without upper limit: e{2,}

To indicate zero or more, use *:

g/.*/n

for one or more, use regex like rr* (there is no + in ed's regex)

Chapter 110: Substitution

use the s command for a substitution, followed by the regular expression and the new text.

s/regex/new/

to repeat last substitution, hit s again

for applying to all matches, use g modifier:

s/XX//gp

You can have multiple substitutions in one command, but each substitution must appear on separate lines, separated by a backslash

use % address to perform the substitution on all available lines

%s/there/Then/

Subexpressions and Backreferences

A subexpression is a part of a regular expression. Use subexpressions to split a line into chunks.

Subexpressions are marked in (), such as ([a-zA-Z0-9])

like {}, you must backslash-escape () to tell ed that you're using them for subexpression.

For ed to remember the whole line, the subexpression must match the whole line that contains the string you want.

A backreference lets you refer to the literal text matched by a subexpression.

Indicate a backreference with a backslash and its number, such as \1, \2

g/(['"]).*\1/: matches pairing " or '

g/the/\
s/\(.*\)/HIT-\1/p

Multiple Substitutions

ed lets you stack multiple substitutions after a search:

g/e{3,}/ \
s/.../.../p\
s/.../.../p\
s/.../.../p

Shortcuts and Alternates

in modern versions of ed

g/regex/ can be simplified with /regex

if you don't have any trailing commands, you can ditch the final /

for s/regex// you can just write s/regex

ed supports alternate delimiters

when you use the s or g command, whatever character appears next take the place of the /

s@nano@heresy

Chapter 111: Scripting

create a ed commands file, and run it like:

$ ed textfile < ed-commands.ed

If you want to hide the bytes read and written, use the -s flag

note, for gnu ed, there're some extension features didn't mentioned in this book, like x (cut), y (yanks) commands

read The GNU ed line editor manual for more information