That's ed
By default its prompt is the empty string. If you want to quit, just enter q
. Don't prefix with :
. If you have unsaved changes, it will reply with ?
. You can interpret that as "are you sure?", and confirm by commanding q
again. By the way any command it doesn't understand will also cause it to reply ?
. That's the only error message it knows.
Its commands are what vim/vi/ex/sed is based on, so commands like g/re/p
, %s/vi/&m/g
, 1,3d
, /pattern/,$d
, w
, q
, wq
work just like vim.
Commands like i
, a
, and c
go into insert mode. To leave insert mode and go back to command mode, just enter a line that has only a .
. To "move" to another line, just enter the line number, an offset from the current line like +2
or -1
, or a regex as a command to go to that line. .
means current line in command mode. You can use it to know where you're at. $
means last line.
By the way, if you want to learn more about it, this being a GNU program in linux, most of its documentation is in info ed
instead of man ed
.
Here is an example session, with comments added (not accepted by ed):
$ ed
i # insert (on current line)
vi
. # end insert
%s/vi/&m/g # substitute vi for vim globally in all lines
i # insert (on current line)
first line
. # end insert
$a # append on last line
last line
. # end insert
%p # print all lines
first line
vim
last line
2 # move to line 2 and print it
vim
/line # move forward to line matching /line/ and print it
last line
-1 # move 1 line backwards and print it
vim
?line # move backward to line matching /line/ and print it
first line
+1 # move 1 line forward and print it
vim
g/line/p # print lines matching /line/ (grep)
first line
last line
p # print (current line)
last line
. # move to current line and print it
last line
c # change (current line)
final line
. # end insert
%p # print all lines
first line
vim
final line
/vim/,$c # change from line matching /vim/ to last line
that's all
. # end insert
%p # print all lines
first line
that's all
wq # write and quit
? # write what?
h # help with last error message
No current filename
wq # write and quit to check error message
?
H # help with all error messages
No current filename
wq # write and quit to check error message
?
No current filename
wq file.txt # write file.txt and quit
22 # wrote 22 bytes
EDIT: Like grawity mentions, more helpful error messages can be activated with h
or H
. Also, ,
rather than %
in the range part of a command is the official way to refer to "all lines" in ed
. In GNU ed
, the possibility to use %
for this is supported but not mentioned in the info
manual. Use of %
for all lines was apparently invented by ex
, seemingly because, there, ,
means .,.
rather than 1,$
like in ed
.
EDIT2: Setting a different editor
Like other answers mentioned, if you want to specify a different editor, you can do so by setting the EDITOR
or VISUAL
environment variables. The difference between the two is explained in this answer.
You can do so like this if you want to set it for a single command:
EDITOR=vi crontab -e
or like this if you want all programs launched in the shell session to use it:
export EDITOR=vi
crontab -e
You can save the export
in ~/.profile
or /etc/profile
, depending if you want it to be a user or system setting for bash, respectively. That's the unix portable way to set the editor; you can do this in any distribution.
In Ubuntu, there is also the update-alternatives
command. The current default editor can be seen with update-alternatives --display editor
, and you can use update-alternatives --config editor
to set it:
$ sudo update-alternatives --config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
3 /usr/bin/vim.basic 30 manual mode
4 /usr/bin/vim.tiny 10 manual mode
Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode.
ed
: by default, it gives a character count (which in Debian based systems is typically888
the first time, being the number of characters in the template "empty" crontab). If you're seeing a count of 0, you must have modified that. See Crontab -e command not working properlyEDITOR
to something else, if you're interested in actually using ed, this is a great introduction: sanctum.geek.nz/arabesque/actually-using-ed