Whether you’re editing crontab files, writing systemd service overrides, or simply tired of being greeted by nano, setting Vim as your default editor can make life in Ubuntu far more pleasant.
Ubuntu offers several ways to set Vim as the default editor — for your user account, for all users system-wide, and specifically for commands like systemctl edit. In this guide, we’ll go through each method in detail, so you can choose the one that fits your workflow best.
1. Setting Vim as the Default Editor for Your User
This is the simplest and most common approach. It affects your account only and applies to commands that respect the $EDITOR or $VISUAL environment variables — such as crontab -e, git commit, or sudoedit.
Step 1: Run select-editor
Ubuntu includes a handy utility named select-editor. It allows you to choose your preferred text editor interactively.
Open a terminal and type:
select-editor
You’ll see a list of installed editors similar to this:
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
Choose 1-3 [1]:
Type the number corresponding to your preferred version of Vim (for example, 2 for /usr/bin/vim.basic) and press Enter.
That’s it! Your selection is saved to ~/.selected_editor, a hidden file in your home directory.
You can verify the setting by displaying the file:
cat ~/.selected_editor
You should see something like:
SELECTED_EDITOR="/usr/bin/vim.basic"
Now, any command that respects the system’s default editor setting will automatically open files in Vim.
2. Setting Vim as the System-Wide Default Editor
If you’re managing a multi-user system or want every user (including root) to default to Vim, use the update-alternatives mechanism.
Step 1: Run the update-alternatives Command
This tool manages symbolic links for common system commands — including the generic name editor.
To configure it, run:
sudo update-alternatives --config editor
You’ll get a list of all editors registered in the system alternatives system:
There are 3 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
1 /bin/nano 40 manual mode
2 /usr/bin/vim.basic 30 manual mode
3 /usr/bin/vim.tiny 10 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter the number for Vim (usually 2) and press Enter.
From now on, any command or script that calls the generic editor binary — such as visudo, crontab, or system maintenance tools — will open Vim instead of nano.
You can test this with:
sudo update-alternatives --display editor
This will show the currently configured default editor and where the symbolic link points.
3. Setting Vim for systemctl edit
When you edit or override systemd services with systemctl edit, Ubuntu doesn’t use the general $EDITOR or $VISUAL variable directly.
Instead, systemd uses its own dedicated environment variable: SYSTEMD_EDITOR.
This means you can control which editor systemd uses independently of your normal system editor.
Method A: Temporary Change
If you want to use Vim for a single systemctl edit session, simply prefix the command with the environment variable assignment:
sudo SYSTEMD_EDITOR=vim systemctl edit ollama.service
This change is temporary — it applies only to this single command execution.
Method B: Permanent Change (for Your User)
To make Vim the permanent editor for all systemctl edit commands in your user environment, add the following line to your shell configuration file (~/.bashrc or ~/.zshrc):
export SYSTEMD_EDITOR=vim
Then, reload your shell configuration:
source ~/.bashrc
Now any future systemctl edit command will open Vim automatically.
Method C: Permanent Change for Root
If you use sudo systemctl edit, the editor is determined by root’s environment, not your user’s. To set it permanently for the root user:
- Start an interactive root shell:
sudo -i
- Open root’s
.bashrcfile:sudo vim /root/.bashrc
- Add this line at the end of the file:
export SYSTEMD_EDITOR=vim
- Save and exit.
- Start a new shell session for root (or reboot) to apply the change.
From now on, even when editing services with elevated privileges, systemd will always use Vim.
4. Alternative: Setting Vim Using Environment Variables
If you prefer a more manual approach (or you want maximum control over which editor different tools use), you can define your preferred editors via environment variables.
Add the following lines to your ~/.bashrc, ~/.zshrc, or system-wide /etc/environment file:
export EDITOR=vim
export VISUAL=vim
Then reload your shell configuration:
source ~/.bashrc
Explanation of Variables
EDITOR— the default editor used by most CLI programs.VISUAL— used by some graphical terminal-based programs that need a more “visual” editor.
Defining both ensures consistent behavior across all applications.
5. Troubleshooting Common Issues
5.1 Vim Isn’t Listed in select-editor
If Vim doesn’t appear in the list of editors, make sure it’s installed:
sudo apt update && sudo apt install vim -y
After installation, run select-editor again.
5.2 systemctl edit Still Opens Nano
If systemctl edit keeps opening nano, check which editor variables are active:
echo $SYSTEMD_EDITOR
echo $EDITOR
If both are empty, export the correct one again and reload your configuration.
5.3 The Change Doesn’t Persist After Reboot
Ensure your environment variable export lines are placed in your shell’s startup file (~/.bashrc or /root/.bashrc for root).
You can verify this with:
grep EDITOR ~/.bashrc
6. System-Wide Configuration via /etc/environment
If you want every user and every shell to default to Vim, you can add the environment variables globally:
sudo vim /etc/environment
Add these lines:
EDITOR=vim
VISUAL=vim
SYSTEMD_EDITOR=vim
Save and exit.
This ensures that all future sessions — for all users — will recognize Vim as the default editor.
7. Quick Reference Summary
| Scope | Method | Command / File |
|---|---|---|
| User only | Interactive | select-editor |
| System-wide | Alternatives | sudo update-alternatives --config editor |
| Temporary (systemctl) | Environment variable | sudo SYSTEMD_EDITOR=vim systemctl edit foo.service |
| Permanent (systemctl, root) | Add variable | /root/.bashrc |
| All users | Global environment | /etc/environment |
Conclusion
Switching from nano to Vim might seem like a small change, but for anyone who spends time in the terminal, it’s a massive quality-of-life upgrade.
Whether you prefer a simple per-user tweak, a global system setting, or a fine-tuned configuration for systemd services, Ubuntu provides flexible tools to make Vim your editor of choice — everywhere.
So go ahead, make the switch — and never fumble for Ctrl+X again 😄.