229

I believe I can do something like export EDITOR=vi, but I'm not sure what exactly to enter, and where.

How can I set "vi" as my default editor?

3
  • 12
    just export EDITOR=vim in your bashrc or zshrc or ..rc
    – Kent
    Apr 23, 2013 at 23:31
  • often must be set up for programs in addition to this such as git
    – timpone
    Apr 24, 2013 at 0:39
  • 13
    In Ubuntu (as said), try run: select-editor.
    – Pablo A
    Feb 21, 2017 at 17:50

4 Answers 4

273

You should add it to your shell’s configuration file. For Bash, this is ~/.bashrc or ~/.bash_profile (see detailed comparison). You should also set $VISUAL, as some programs (correctly) use that instead of $EDITOR (see VISUAL vs. EDITOR). Additionally, unless you know why, you should set it to vim instead of vi.

TL;DR, add the following to your shell configuration (probably ~/.bashrc):

export VISUAL=vim
export EDITOR="$VISUAL"
6
  • Not working for me! I still see a number after: "sudo crontab -e" instead of editing crontab! Jun 26, 2018 at 9:34
  • @MohsenAbasi What do you mean “I still see a number?”. Check that EDITOR is in both your environment (env | grep EDITOR) and is passed to sudo (sudo env | grep EDITOR), as your system’s sudo security policy may prohibit it (see man sudo for more details). Jun 26, 2018 at 17:18
  • I mean that I still see just a number (not opening 'vim' editor) after executing: 'sudo crontab -e'. Since there is no default editor for editing cron jobs in my Ubuntu. To have a default editor, your solution does nothing for me. Only solution of 'DobesVandermeer' works. Jun 27, 2018 at 6:08
  • 2
    Once it is done, reload the config with . ~/.bashrc
    – Jona
    Feb 18, 2021 at 20:47
  • I had to use /usr/bin/vi rather than vim otherwise crontab -e failed with the error crontab /bin/sh: 1: vim: not found.
    – SharpC
    Jan 28 at 13:24
175

On Ubuntu and other Ubuntu/Debian-based Linux systems, you can explicitly set the default text editor at the system level by providing its path to update-alternatives:

Automatic, Scripted

sudo update-alternatives --set editor /usr/bin/vim.basic
sudo update-alternatives --set vi /usr/bin/vim.basic

Note

If your distro doesn't call it /usr/bin/vim.basic, you can find out which path to use with the --list argument:

sudo update-alternatives --list editor
/bin/ed
/bin/nano
/usr/bin/vim.basic
/usr/bin/vim.tiny

Manual, Interactive

Or, to see all options and choose interactively:

sudo update-alternatives --config editor
5
  • 6
    This set the default for git too, which was exactly what I needed.
    – Kzqai
    Mar 9, 2016 at 18:26
  • 2
    This set the default for ranger too, which was exactly what I needed. PS: just for helping index for people who is trying to do the same.
    – wviana
    Dec 6, 2016 at 16:42
  • 1
    Only this worked for me on Ubuntu server 18.04 Sep 17, 2018 at 17:55
  • Not within FreeBSD freebsd.org/cgi/… but within the port of dkpg freshports.org/archivers/dpkg Dec 25, 2020 at 9:05
  • This works with my version of WSL2/Ubuntu on Windows 10.
    – staylorx
    Apr 11, 2022 at 21:27
44

In recent versions of Ubuntu you use the alternatives system to manage the default, editor, e.g.:

update-alternatives --set editor /usr/bin/vim.basic

To see which editors are available for use:

update-alternatives --list editor

Some UNIX distributions might provide a select-editor command:

select-editor

And it will ask you which editor to use.

Make sure you actually have vim installed before trying to set it as your default editor.

4
3

If bash is your shell, then insert it into .bash_profile in your home directory; if zsh is your shell, then insert it into .zprofile; for other shells see the according documentation.

5
  • On zsh I recommend putting it into .zshenv instead.
    – pepoluan
    Oct 20, 2020 at 9:23
  • 1
    @pepoluan If you see this, could you please elaborate? The .zprofile file will be read for each and every shell invocation. It seems odd to want to set the editor for shell sessions that aren't even interactive.
    – Kusalananda
    Dec 28, 2020 at 19:02
  • @Kusalananda it's just setting an env var anyways, so not harmful. If you need to, say su into your account, .zshenv will be sourced.
    – pepoluan
    Dec 29, 2020 at 4:55
  • 1
    Why not put it in .zshrc for zsh as the top-vote opts for .bashrc?
    – Timo
    May 3 at 11:57
  • You shouldn't go around putting it into things like that.
    – NeilG
    Jun 21 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .