I want to edit crontab. It is supposed to be Vi, but it's not, as you can see:
root@euve252628:~# crontab -e
0
0 * * * * /var/www/vhosts/nevemind-what-webs/httpdocs/megacronstunde.php
?
:wq
?
Run Code Online (Sandbox Code Playgroud)
Yes, it gave me a ?. And why does it give me a zero?
When I type a q and press Enter - it quits. I need to know how to save, because it's not classic Vi.
JoL*_*JoL 65
ed默认情况下,它的提示是空字符串。如果您想退出,只需输入q。不要前缀:. 如果您有未保存的更改,它会回复?。您可以将其解释为“您确定吗?”,并通过q再次命令确认。顺便说一句,它不理解的任何命令也会导致它回复?。这是它知道的唯一错误消息。
它的命令是什么VIM / VI / EX是基于/ SED上,所以命令一样g/re/p,%s/vi/&m/g,1,3d,/pattern/,$d,w,q,wq工作就像VIM。
命令一样i,a和c进入插入模式。要离开插入模式并返回命令模式,只需输入一行只有.. 要“移动”到另一行,只需输入行号、与当前行的偏移量(如+2或-1)或正则表达式作为转到该行的命令。.表示命令模式下的当前行。你可以用它来知道你在哪里。$表示最后一行。
顺便说一句,如果你想了解更多关于它的信息,这是一个 linux 中的 GNU 程序,它的大部分文档都在info ed而不是man ed.
这是一个示例会话,添加了评论(不被 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
Run Code Online (Sandbox Code Playgroud)
编辑:与 grawity 提到的一样,可以使用h或激活更有用的错误消息H。此外,,而不是%在命令的范围部分是在ed. 在 GNU 中ed,%支持使用此功能的可能性,但info手册中未提及。%对所有行的使用显然是由 发明的ex,似乎是因为在那里,,手段.,.而不是1,$像 in ed。
与提到的其他答案一样,如果要指定不同的编辑器,可以通过设置EDITOR或VISUAL环境变量来实现。两者之间的区别在这个答案中有解释。
如果您想为单个命令设置它,您可以这样做:
EDITOR=vi crontab -e
Run Code Online (Sandbox Code Playgroud)
或者像这样,如果您希望在 shell 会话中启动的所有程序都使用它:
export EDITOR=vi
crontab -e
Run Code Online (Sandbox Code Playgroud)
您可以分别保存exportin~/.profile或/etc/profile,具体取决于您希望它是 bash 的用户设置还是系统设置。这是设置编辑器的 Unix 可移植方式;您可以在任何发行版中执行此操作。
在 Ubuntu 中,也有该update-alternatives命令。当前默认编辑器可以用 看到update-alternatives --display editor,你可以用update-alternatives --config editor它来设置:
$ 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.
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 39
按Ctrl+ Z。这会暂停编辑器并为您提供 shell 提示。在提示符下,运行ps以查看此终端中正在运行的进程。
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
Run Code Online (Sandbox Code Playgroud)
bash是原始外壳程序,crontab是预期的,sh是另一个必须由 调用的外壳程序crontab,并且ps是正在运行的ps命令。剩下的ed(由同名包提供)。
如果您不知道如何退出编辑器,此时可以使用kill 26302或杀死它kill %1。
Ed 是一位古老的编辑器,可以追溯到显示器成为(司空见惯的)事物之前。它是为交互式输出外围设备是电传打印机的计算机设计的。
将0它显示在开始是文件中的行数。显然这是重要的信息。
?意味着 ed 不明白您输入的内容。行式打印机速度较慢,因此 ed 不会浪费时间和墨水来显示无意义的信息,例如错误消息。一个老笑话(我不知道出处)是这样的:
肯汤普森有一辆他帮助设计的汽车。与大多数汽车不同,它既没有速度表,也没有油表,也没有任何其他困扰现代驾驶员的白痴灯。相反,如果司机犯了一个错误,一个巨大的“?” 仪表板中央亮起。“有经验的司机,”汤普森说,“通常会知道哪里出了问题。”
如果你觉得没有经验,你可以发出命令H(除了一个命令之外的所有命令都是一个字母(不包括数据参数),因为谁喜欢打字),你会得到错误消息。例如,如果您只是按Enter...
?
H
Invalid address
Run Code Online (Sandbox Code Playgroud)
清楚,不是吗?(空行是空命令 - 不是单个字母的命令。空命令移动到下一行并打印它。如果您在文件末尾,则在空命令中总是这种情况文件,没有下一行,因此您请求移动到的地址无效。)
crontab与大多数运行文本编辑器的命令一样,该命令检查环境变量VISUAL并EDITOR(按该顺序)决定运行哪个程序,并返回到系统默认值。如果这些变量中的任何一个设置为ed,请更改或删除它。
在 Ubuntu(以及 Debian 和其他)上,系统默认编辑器是/etc/alternatives/editor,它是通过替代机制管理的。默认情况下,它选择安装的“最佳”编辑器,其中“最佳”由包维护者设置的优先级定义。使用update-alternatives配置的替代方案。您可以显示可用的编辑器及其优先级
update-alternatives --display editor
Run Code Online (Sandbox Code Playgroud)
ed具有优先级 -100,而所有其他“明智的”编辑器具有正优先级,因此默认情况下使用它的唯一方法是未安装任何编辑器。Ubuntu 的默认安装使用 nano 作为默认编辑器,优先级为 400。如果已将替代设置为ed,则可以更改它
sudo update-alternatives --config editor
Run Code Online (Sandbox Code Playgroud)
Yar*_*ron 12
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
Run Code Online (Sandbox Code Playgroud)
After setting the default editor to be vim, running crontab -e will use vim as your editor.
Notes:
vim permanently, you'll have to add the above command to your ~/.bashrc or ~/.profile or similar file.