如何指定一个编辑器来打开 crontab 文件? “ export EDITOR = vi”不工作吗

我使用的是 Red Hat Enterprise Linux 5,我想设置 vim 编辑器来编辑 crontab 文件。

如果我运行 echo $EDITOR,我得到 vim。但是当我运行 crontab -e,我得到不同的编辑器。

135741 次浏览

I think you might need to use the full path:

export EDITOR=/usr/bin/vim

To quote the man:

The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables

Most often if you run crontab -e from X, you have VISUAL set; that's what is used. Try this:

VISUAL=vi crontab -e

It just worked for me :)

Very probable that your VISUAL environment variable is set to something else. Try:

export VISUAL=vi

If the above methods don't work (as they didn't work on my Ubuntu 13.04 installation) try:

There are a number of alternative ways:

1) Run select-editor

select-editor

2) Manually edit the file: ~/.selected_editor specifying your preferred editor. With this option you can specify editor parameters.

# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/emacs -nw"

3) You can specify on the fly on the commandline with:

env VISUAL="emacs -nw" crontab -e

It wasn't working for me. I run crontab with sudo, so I switched to root, did the above suggestions, and crontab would open in vim, but it still wouldn't from my user account. Finally I ran sudo select-editor from the user account and that did the trick.

export EDITOR=vim worked for me

You can use below command to open it in VIM editor.

export VISUAL=vim; crontab -e

Note: Please make sure VIM editor is installed on your server.

This worked for me :

EDITOR="/usr/bin/vim"
export EDITOR

Add this to ~/.bash_profile or ~/.bashrc to enable this for current user.