An introduction to the UNIX ed line editor

From Computer History Wiki
Jump to: navigation, search

ed was originally the only text editor on UNIX; it is still the minimal editor there. It was based on QED, an editor Ken Thompson wrote for CTSS.

Starting and Ending an Editor Session

  • ed name1 = Edit file "name1" if existing, if not, creates it
  • w = Write file to disk
  • 2,5w name2 = Write lines 2 to 5 to disk into file "name2"
  • q = Leave editor; asks with "?" for confirmation if changes not written to disk; another "q" = leave unconditional
  • Q = Leave editor unconditional, discarding edits

The editor starts in command mode, the commands "a", "c", and "i" switch the editor to editing mode.

You switch back to command mode by entering a dot (.) as the first character on a new line followed by a [RETURN].

Specifying Line Ranges

  • . = Current line
  • $ = Last line
  • 5 = Line 5
  • 2,5 = Lines 2 to 5 (inclusive)
  • 1,$ = All lines

Editing Commands

  • 3i = Insert BEFORE line 3
  • 4a = Insert AFTER line 4
  • $a = Insert at file end
  • 5c = Replace line 5 with new text
  • 3,8c = Replace lines 3 to 8 (inclusive) with new text

Commands to List, Delete, and Change Text

  • .p = List current line
  • 2p = List line 2
  • 3,7p = List lines 3 to 7 (inclusive)
  • .n = List current line with line number
  • 2n = List line 2 with line number
  • 3,7n = List lines 3 to 7 (inclusive) with line numbers
  • 1,$n = List whole file with line numbers


  • 10d = Delete line 10
  • 2,5d = Delete lines 2 to 5 (inclusive)


  • 5t10 = Copy line 5 before line 10 (line 5 remains)
  • 2,4t9 = Copy lines 2 to 4 (inclusive) before line 9 (lines 2 to 4 remain)


  • 3m12 = Copy line 3 before line 12 (line 3 is deleted)


  • 3r name3 = Copy content of file "name3" after line 3
  • $r text3 = Copy content of file "name3" at end of text


  • .s/text1/text2 = Substitute "text1" with "text2" in the current line
  • 3,8s/thing/item = Substitute word "thing" with word "item" in lines 3 to 8 (inclusive)
  • 1,$s/this/that = Substitute word "this" with word "that" in whole text


  • ! command = Execute arbitrary UNIX command without leaving the editor
  • u = Cancel last command

External links