Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit crontabs in VS Code?

If I try to use Visual Studio Code (on macOS 10.15) to edit my crontab, it opens an empty file without the contents of my crontab.

$ VISUAL='code' crontab -e
crontab: no changes made to crontab

I didn't actually expect this to work (without -w) but include it for completeness. But when I add the -w it still fails.

$ VISUAL="code -w" crontab -e
crontab: code -w: No such file or directory
crontab: "code -w" exited with status 1

It occurred to me that there may be some weirdness with quoting, but neither single quotes nor the following fixed anything:

$ function codew() {
function> code -w "$1"
function> }
$ export VISUAL='codew'
$ crontab -e

The problem seems to be that the crontab's tempfile is not actually present. But how do I solve this? How can I use VS Code to edit crontabs?

like image 305
iconoclast Avatar asked Jun 28 '20 03:06

iconoclast


People also ask

How do I open crontab in editor?

The very first time you issue the crontab command with the -e (edit) option in a Bash terminal, you're asked to pick the editor you'd like to use. Type crontab , a space, -e and press Enter. The editor you select is then used to open your cron table.

How do I enable crontabs?

Use the crontab -e command to open your user account's crontab file. Commands in this file run with your user account's permissions. If you want a command to run with system permissions, use the sudo crontab -e command to open the root account's crontab file.


1 Answers

  1. Create a file touch ~/code-wait.sh:
#!/bin/bash
OPTS=""
if [[ "$1" == /tmp/* ]]; then
    OPTS="-w"
fi

/usr/local/bin/code ${OPTS:-} -a "$@"
  1. Make this file executable:
chmod 755 ~/code-wait.sh 
  1. Add to your .bashrc or .bash_profile or .zshrc:
export VISUAL=~/code-wait.sh
export EDITOR=~/code-wait.sh
  1. Run command:
EDITOR='code' crontab -e
like image 50
Dmitry Grinko Avatar answered Oct 27 '22 00:10

Dmitry Grinko