Linux crontab set editor

Edit crontab with an editor other than the default

I would like to edit crontab with vi once. My default editor is nano, and I want to keep it that way. I only want to edit with vi one time. I don’t want to change the default to vi then back to nano after editing. Can this be done?

2 Answers 2

crontab should respect the EDITOR environment variable, so you can just do (for the root crontab for example)

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

I don’t think you want sudo here unless you want to edit really fundamental tasks. Most people run cron jobs at user-level. Also, on Mac OS X if you use sudo it gives an error. Without it it works fine.

Specifying nano as the editor for crontab file

Specifying vim as the editor for crontab file

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

What is this editor that gets opened by crontab?

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.

Most likely ed : by default, it gives a character count (which in Debian based systems is typically 888 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 properly

While you usually want to just override EDITOR to something else, if you’re interested in actually using ed, this is a great introduction: sanctum.geek.nz/arabesque/actually-using-ed

@justme, by the way, welcome to StackExchange! If you find an answer that best answers your question, you can accept it by clicking on the checkmark next to it.

4 Answers 4

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.

Читайте также:  Astra linux установить драйвер nvidia

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:

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.

Читайте также:  Linux show file permissions

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 specify a editor to open crontab file? «export EDITOR=vi» does not work

I’m using Red Hat Enterprise Linux 5, and I want to set the vim editor to edit the crontab file. If I run echo $EDITOR , I get vim. But when I run crontab -e , I get different editor.

8 Answers 8

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

If the crontab is managed by several persons with one user, I recommend to do this in a subshell, so the default editor stays in place.

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:

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

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 

I was by mistake setting default editor to nano when opening crontab first time. Then I had to use sudo select-editor to get it to work. A tip if only select-editor doesn’t work

You shouldn’t use «sudo» for your personal configuration. It might break things forcing you to stay at elevated operation for normal computer use. The sudo (elevated command) is for working with systemwide features outside your personal area (~user area).

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.

I think you might need to use the full path:

The trap is that VISUAL is checked first. So a perfectly sound advice like yours works well for an SSH session and mysteriously fails on a desktop box.

I tried exporting while assigning as described, but it didn’t work. By assigning first and then exporting it worked for me, like so: EDITOR=/usr/bin/vi; export EDITOR . Using SunOS under SSH here.

export EDITOR=vim worked for me

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.

If you have to run sudo to configure your user environment, that signifying problems that you should address. You have run sudo on your personal space and lost permission to files that should be owned by you. You can check your space by running: find ~/ -mount ! -user $(whoami) . You can correct the problem by running: sudo chown -R $(whoami):$(whoami) ~/ .

Читайте также:  Установка ядра astra linux

@L.D.James You were right, thank you. As it turns out it was just the .select_editor file that was owned by root.

Источник

Changing default crontab editor

I am trying to change the default editor from nano to vim . I have run the following commands: sudo update-alternatives —config editor and update-alternatives —config editor Both now output:

 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 

I have restarted my ssh session and restarted sshd but crontab -e still opens in nano What else do I need to do?

10 Answers 10

Just simply run select-editor , this will let you choose any editor you want.

Better answer I think than what was picked. Don’t have to change my bash profile to set the editor. Verified by logging out and logging back in.

Try your ssh session followed by

export EDITOR=/usr/bin/vim.basic 

The format of setting of the EDITOR variable depends on which shell you are using.

In Ubuntu you can set the EDITOR environment variable on logon by adding the above to the ~/.profile

Your SSH session will read a similar file (if it exists) on the remote host called ~/.ssh/environment . This has to be specifically defined by the ssh daemon config. See man sshd_config and look for PermitUserEnvironment for more details.

If you only want to choose the editor temporarily, you can do the following

If you want do this for root user then:

sudo EDITOR=nano crontab -e 

This sets the EDITOR environment variable for the command

Thank you. This helped me to edit the sudo/root’s crontab without needing to change the root user’s editor or something (on the shared VM that others might want a different editor for).

This helped me a lot too, but how to change the crontab editor permanent? I changed it as a text editor for sudoedit , but it still uses vim for crontab (unless I use this answer. However I would prefer not having to type sudo EDITOR=nano every time). And. yes, I know, many of you disagree, but I do prefer nano, sorry.

cd /bin mv nano nano_must_die ln -s /usr/bin/vim nano 

I wouldn’t know what’s wrong with nano for admins who don’t see value in learning vi commands just to tweak some config files.

I guess do whatever you like on your own personal machine. But on a machine shared with anyone else, this suggestion essentially disables nano for all users in a way that when a user specifically requests nano, they get vim instead. What’s the point of that? If they wanted vim, they would ask for it. If the issue here is that the system is configured to automatically invoke nano in some circumstance where you personally prefer vim, then change the configuration.

Источник

Оцените статью
Adblock
detector