Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

ed cheatsheet

Published: 06-10-2012 | Author: Remy van Elst | Text only version of this article


❗ This post is over twelve years old. It may no longer be up to date. Opinions may have changed.


ed is a line editor for the Unix operating system. It was one of the first end-user programs hosted on the system and has been standard in Unix-based systems ever since. ed was originally written in PDP-11/20 assembler by Ken Thompson in 1971.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below. It means the world to me if you show your appreciation and you'll help pay the server costs:

GitHub Sponsorship

PCBWay referral link (You get $5, I get $20 after you've placed an order)

Digital Ocea referral link ($200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!)

Download the PDF cheatsheet

Navigating

  • Line number: go to that line and print it.
  • pn - Print current line and line number.
  • 3kxe - mark line 3 as "xe".
  • 'xep - print the line marked as xe.

Editing

  • a - start editing after the current line.
  • i - start editing before the current line.
  • c - replace the current line.
  • end the editing with only a period on a line (.).
  • 1d - delete line 1.
  • 1,2j - join line one and two.

Printing

  • 1,$p - print entire buffer.
    • ,p - also prints the entire buffer.
  • 2,3p - print lines 2 and 3.
  • .,4p - print from the current line (.) to line 4.
  • -1,+1p - print 1 line above and 1 line below the current line.

Buffer operations

  • 2t4 - Copy line 2 to after line 4.
  • 2t$ - Copy line 2 to the end of the file.
  • 3m6 - Move line 3 to after line 6.
  • 2,4m$ - Move lines 2 to 4 to the end of the file.

Searching / replace

  • /like - Move to the next line after the current one matching regular expression /like.
  • ?Like - Search backwards.
  • 1s/re/jo - Substitute only the first occurence of re with jo on line 1.
  • 1s/re/jo/g - Substitute all occurences of re with jo on line 1.
  • 1,$s/re/jo/g - Substitute all occurences of re with jo in the whole buffer.

Regular expresions

  • g/re/p - print all the lines matching regular expression /re/.
  • v/re/p - print all the lines NOT matching regular expression /re/.
  • g/re/m$ - move all the lines matching regular expression /re/ to the end of the file.

Reading and writing

  • w file.txt - Save the current buffer as filename file.txt.
  • w - Save the current buffer if the file is already saved.
  • w1,4 - Save lines 1 to 4 to the current buffer.
  • W - append to a file (not replace).
  • r /etc/hosts - Insert the contents of /etc/hosts after the current line.
  • r !ps -e - Insert the output of the command "ps -e" after the current line.
  • e file.txt - Open the file file.txt and replace the current buffer.
Tags: cheatsheet , ed , pdf , tutorials , unix-editor