What is this editor that gets opened by crontab?
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.
How to find out what this editor is
Press Ctrl+Z. This suspends the editor and gives you a shell prompt. At the prompt, run ps
to see what processes are running in this terminal.
bash-4.3$ crontab -e
1077
^Z
[1]+ Stopped crontab -e
bash-4.3$ ps
PID TTY TIME CMD
26295 pts/10 00:00:00 bash
26297 pts/10 00:00:00 crontab
26298 pts/10 00:00:00 sh
26299 pts/10 00:00:00 ed
26302 pts/10 00:00:00 ps
bash
is the original shell, crontab
is expected, sh
is another shell which must have been invoked by crontab
, and ps
is the running ps
command. That leaves ed
(provided by the package of the same name).
If you can't figure out how to quit the editor, you can kill it at this point, with kill 26302
or kill %1
.
What is ed?
Ed is an ancient editor, dating back from before monitors were a (commonplace) thing. It was designed for computers whose interactive output peripheral was a teleprinter.
The 0
it displays at the beginning is the number of lines in the file. Evidently that's important information.
?
means that ed didn't understand what you typed. Line printers are slow, so ed doesn't waste time and ink to display pointless information such as error messages. An old joke (I don't know the origin) goes:
Ken Thompson has an automobile which he helped design. Unlike most automobiles, it has neither speedometer, nor gas gauge, nor any of the other numerous idiot lights which plague the modern driver. Rather, if the driver makes a mistake, a giant “?” lights up in the center of the dashboard. “The experienced driver,” says Thompson, “will usually know what’s wrong.”
If you feel inexperienced, you can issue the command H
(all commands but one are a single letter (not counting data arguments), because who likes typing), and you'll get error messages. For example, if you just press Enter…
?
H
Invalid address
Clear, isn't it? (An empty line is the empty command — the one that isn't a single letter. The empty command moves to the next line and prints it. If you're at the end of the file, which is always the case in an empty file, there is no next line, so the address to which you requested to move is invalid.)
How to get another editor
The crontab
command, like most commands that run a text editor, check the environment variables VISUAL
and EDITOR
(in that order) to decide which program to run, falling back to a system default. If either of these variables is set to ed
, change it or remove it.
On Ubuntu (and Debian and others), the system default editor is /etc/alternatives/editor
, which is managed through the alternatives mechanism. By default it picks the “best” editor that's installed, where “best” is defined by priorities set by package maintainers. Use update-alternatives
to configure an alternative. You can display the available editors and their priorities with
update-alternatives --display editor
ed
has the priority -100, whereas all the other “sensible” editors have a positive priority, so the only way it would be used by default is if no editor has been installed. A default installation of Ubuntu uses nano as the default editor, with priority 400. If the alternative has been set to ed
, you can change it with
sudo update-alternatives --config editor
From your question it seems like your default EDITOR is not vim
, and you have problems with the current default editor.
The below command will set the default EDITOR to be vim
export EDITOR=vim
After setting the default editor to be vim
, running crontab -e
will use vim
as your editor.
Notes:
- If you prefer, you can choose different EDITOR as your default editor
- If you want to set the default editor to
vim
permanently, you'll have to add the above command to your~/.bashrc
or~/.profile
or similar file.
To use VI editor to edit crontab, use the below command.
EDITOR="vi" crontab -e
or, if you want to edit with another editor like gedit, nano, etc., just replace vi with the editor name.