ved  →  an introduction

ved: an introduction

ved is a line editor. You work with text one command at a time, not by moving a cursor. If you've never used one, that idea might sound strange. It is also the idea behind nearly every Unix text tool: grep, sed, awk, and the modal commands in vim are all direct descendants of this single editor's design.

ved is a clone of ed, the original Unix line editor, but with one crucial difference: where ed speaks in cryptic single letters and responds to errors with a single question mark, ved uses full English words, gives you friendly error messages, and has a built-in help system. When you are comfortable with ved, switching to ed is a matter of using shorter commands. This page will get you there.

Starting a session

Launch ved from the command line. If you give it a filename, it loads the file into its buffer (its in-memory workspace) and shows you the file size in bytes.

$ ved -p '* ' notes.txt
47
* 

The -p '* ' flag sets a prompt, which ved shows whenever it is ready for a command. Without it, ved is silent between commands — a deliberate ed compatibility choice that tends to confuse newcomers. Until you are comfortable, use the prompt.

Without a filename, ved starts with an empty buffer:

$ ved -p '* '
* 

Adding text with append

The append command enters a special input mode. Every line you type goes straight into the buffer until you end input mode by typing a single period on its own line.

* append
The quick brown fox
jumps over the lazy dog
.
* 

After the period, you're back in command mode and the prompt returns. The buffer now contains two lines.

Seeing what you have

The print command shows a line from the buffer. By itself, it prints the current line — the last line you touched.

* print
jumps over the lazy dog
* 

To see more than one line, give print an address range. A comma by itself is shorthand for "all lines":

* ,print
The quick brown fox
jumps over the lazy dog
* 

If you want line numbers alongside the text, use number instead of print:

* ,number
1	The quick brown fox
2	jumps over the lazy dog
* 

Addresses

Most ved commands can take an address or a range of addresses, telling the command which lines to operate on. The defaults vary by command, but the address syntax is the same everywhere.

AddressMeaning
5Line 5
.The current line
$The last line
+3Three lines after the current line
-1One line before the current line
2,7Lines 2 through 7
,Every line (shorthand for 1,$)
;From current to last (shorthand for .,$)

So 3print prints line 3, 2,5print prints lines 2 through 5, and $print prints the last line. A bare address with no command letter jumps to that line and prints it:

* 1
The quick brown fox
* $
jumps over the lazy dog
* 

Just pressing Enter on an empty line advances one line and prints it, which is how you walk through a file.

Editing: insert, append, delete

You already know append, which adds lines after an address. Its sibling is insert, which adds lines before an address. Both enter input mode, and both end with a period.

* 1insert
ONCE UPON A TIME:
.
* ,number
1	ONCE UPON A TIME:
2	The quick brown fox
3	jumps over the lazy dog
* 

The delete command removes the addressed lines and confirms what happened:

* 1delete
deleted line 1
* ,number
1	The quick brown fox
2	jumps over the lazy dog
* 

A range works the same way. 2,4delete removes three lines, reports deleted 3 lines (2-4), and leaves the buffer one step smaller.

Search and replace: substitute

The s command replaces text matching a pattern. Its syntax has three slashes: the pattern, the replacement, and an optional g flag for "replace every occurrence on the line, not just the first."

* 1s/quick/slow/
The slow brown fox
* 1s/o/O/g
The slOw brOwn fOx
* 

The pattern is a regular expression. In ved's flavor (POSIX Basic Regular Expressions), the useful building blocks are:

SymbolMatches
.Any single character
*Zero or more of the preceding element
^Start of line
$End of line
[abc]Any one of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Any lowercase letter
\(...\)Capturing group (remembered for later)
\1 to \9Refer back to a captured group

In the replacement, & stands for the whole match and \1 through \9 stand for captured groups. So you can rearrange pieces of matched text:

* 1s/\(brown\) \(fox\)/\2 the \1 one/
The slOw fox the brOwn one
* 

An address range applies the substitution to each line in turn. 1,$s/old/new/g replaces every occurrence of "old" with "new" throughout the entire buffer.

Global: run a command on every matching line

The g command takes a pattern and another command, and runs that command on every line in the buffer matching the pattern. Its most famous form is g/pattern/print — literally "global regex print," which is where the standalone grep command got its name.

* ,number
1	apple pie
2	banana bread
3	cherry cake
4	blueberry muffin
5	avocado toast
* g/b/print
banana bread
blueberry muffin
* g/b/number
2	banana bread
4	blueberry muffin
* 

The inner command can be anything. g/pattern/delete removes every matching line. g/pattern/s/old/new/g substitutes only on matching lines.

The v command is the inverse: it operates on every line that does not match the pattern. Useful for filtering:

* v/b/print
apple pie
cherry cake
avocado toast
* 

Working with files

Three commands move text between the buffer and the disk:

write (or write filename)
Writes the buffer to a file. Without a filename, it writes back to whatever file you opened or most recently wrote. Confirms with the byte count.
edit filename
Replaces the buffer with the contents of a file. You lose whatever was in the buffer, so save first if you care about it.
read filename (or Nread filename)
Inserts the contents of a file into the buffer after the addressed line (default: the end). Useful for concatenating files.
* write notes.txt
wrote 37 bytes to notes.txt
* 

Any time you need a reminder of what's available, type help and ved will print a summary of every command, address form, and regex symbol it understands.

Going faster: the short commands

Every command you have learned also has a short form — a single letter. When you use the short forms, you are writing ed. ved will still be running, but the commands you type will work without change on real ed on any Unix system.

Verbose commands and their short equivalents
VerboseShortWhat it does
appendaAdd lines after an address
insertiAdd lines before an address
deletedDelete addressed lines
printpPrint addressed lines
numbernPrint with line numbers
quitqQuit (warns on unsaved changes)
helpHPrint the command reference

The commands that already use delimiters — s/old/new/, g/pattern/cmd, v/pattern/cmd, w filename, e filename, r filename — have no long form. They are single letters in both ved and ed.

That is the whole deal. Every skill you just picked up transfers directly to the editor that's been on every Unix system since 1969. If you ever find yourself on a stripped-down server with no vim or nano, ed will be there. And now you know how to use it.

Ready to try it?

ved is a single binary for Linux, macOS, and Windows, MIT-licensed and dependency-free. The ved page has the install steps, and the full source lives at github.com/excelano/ved.

Install ved