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.
Download the PDF cheatsheet
- 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.
- 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.
- 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.
- 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.
- 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.