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:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

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