How to Save and Exit Crontab -e in Linux: Nano/Vi Editor Solutions for Cron Job Editing


2 views



Many Linux users encounter frustration when trying to save and exit after running crontab -e. The core issue stems from the default editor configuration in most Linux distributions. Here's a comprehensive technical breakdown:

On CentOS/RHEL systems (including CentOS 5 mentioned in the question), the default editor is typically vi/vim. The confusion arises because:

  • Vi requires specific commands to exit (:wq to write and quit)
  • New users often press ESC then type :x or :wq
  • Incorrect attempts may leave users trapped in editor mode

The permanent solution is to change your default editor. Here are multiple methods:

# Temporary solution (current session only):
export EDITOR=nano

# Permanent solution (add to ~/.bashrc):
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

# System-wide alternative:
sudo update-alternatives --config editor

For those who prefer maintaining vi but want to understand the save process:

# Vi/Vim save and exit sequence:
1. Press ESC to ensure command mode
2. Type :wq (write and quit)
3. Press Enter

# Common alternatives:
:x - Write if changed then quit
:w - Write without quitting
:q! - Force quit without saving

After successful edit, always verify:

# List current crontab:
crontab -l

# Example cron job entry:
* * * * * /path/to/script.sh >/dev/null 2>&1

System administrators can configure the default system-wide:

# Set default editor in /etc/environment:
echo 'EDITOR=nano' | sudo tee -a /etc/environment

# Alternative for specific user:
sudo -u username crontab -e

When things go wrong:

  1. Check active editor: echo $EDITOR
  2. Verify editor packages: rpm -q nano vim-enhanced
  3. Test editor directly: nano testfile or vi testfile


Many Linux users, especially those new to system administration, struggle with saving and exiting the crontab -e interface. The default editor (usually vi) can be confusing if you're not familiar with its commands. Here's how to handle this situation effectively.

When you run crontab -e, your system uses the editor defined in these environment variables (in order of precedence):

VISUAL
EDITOR

If neither is set, it falls back to the system default (typically vi). This explains why many users find themselves stuck in an unfamiliar editor.

The simplest solution is to change your default editor to something more user-friendly like nano. Here's how:

# For current session only:
export EDITOR=nano

# To make it permanent for your user:
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

Alternatively, you can set it system-wide by editing /etc/profile or creating a file in /etc/profile.d/.

Here are the save/exit commands for common editors:

Nano

Ctrl+O to save
Enter to confirm filename
Ctrl+X to exit

Vi/Vim

Esc to enter command mode
:w to write (save)
:q to quit
:wq to save and quit
:q! to quit without saving

Emacs

Ctrl+X then Ctrl+S to save
Ctrl+X then Ctrl+C to exit

After setting your preferred editor, verify it works:

echo $EDITOR
crontab -e

You should now see your cron jobs in your chosen editor.

If you prefer, you can edit the crontab directly without using an interactive editor:

# List current crontab to a file
crontab -l > mycron

# Edit the file with any editor
nano mycron

# Install the edited crontab
crontab mycron

Here's a sample cron job you might add after successfully editing your crontab:

# Run backup script every day at 2:30 AM
30 2 * * * /usr/local/bin/backup.sh

Remember to save your changes using the appropriate commands for your editor.