42

My default editor is Pico at my server. I use Bash and Linux.

I tried to change Vim to be my default editor unsuccessfully by:

echo vim > $EDITOR

How can I change Vim to be my default editor?

The following code does not work in file .bashrc:

export EDITOR='vim'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    What unix/linux are you using, and specifically what shell? Bash, csh, other? – Zoredache Mar 15 '09 at 01:26
  • 1
    do you have vim installed? and if so is it on your PATH? –  Mar 24 '09 at 16:02
  • @Neil: I have Vim installed. It is apparently also in my PATH, since I can start vim by the command vim in Terminal. – Léo Léopold Hertz 준영 Mar 24 '09 at 18:13
  • Additionally, if you only wish to temporarily change the default editor for one command (for the case of git, not wanting to use vi) you can do `EDITOR=nano git commit --amend` or whatever the command happens to be, and `EDITOR` will be set to `nano` just for that command. – adam_0 Mar 14 '11 at 00:16

9 Answers9

81

Adding

export EDITOR=vim

to your .bashrc file should really do the trick. (Quotes aren't necessary there and, depending on what quotes you used, they may be the cause for your problem.)

You must open a new shell (or enter source ~/.bashrc at the prompt) after modifying file .bashrc for the modification to take effect.

What is the program from which you want Vim to be started?

I haven't used Git, but the documentation reads:

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

So check whether one of these variables is set:

echo $GIT_EDITOR $VISUAL $EDITOR
git config --get-all core.editor

For me,

export VISUAL=vim

solved the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jochen Walter
  • 1,420
  • 11
  • 10
21

You can use the Git configuration option core.editor to set the editor of your liking, e.g., nano:

git config [--global] core.editor "nano"

You can also change this by editing the .gitconfig file in your home directory (global) or git repository (create it if it doesn't exist) if you don't have shell access:

...
[user]
  name = Your Name
  email = your@email.address
[core]
  editor = nano
...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4levels
  • 3,134
  • 1
  • 23
  • 22
2

Check this command:

sudo update-alternatives --config editor
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • Yes, this is the correct answer for the default editor. I keep the accepted answer as it is because the focus here is about Git and server side. Like setting its editor by variables `$GIT_EDITOR`, ... – Léo Léopold Hertz 준영 Apr 28 '16 at 12:59
2
vim=/usr/bin/vim # Or wherever the Vim binary is
export EDITOR=vim

should do the job.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

I don't have an EDITOR environmental variable. My .bashrc file does define this:

alias vi='vim'

And supposedly, if Vim can't find a file called .vimrc in your home directory, it runs in "compatibility mode" and you only get vi features until you say type :nocp.

If it is based on your EDITOR environmental variable, you would set it like this in Bash:

export EDITOR='vim'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philluminati
  • 2,649
  • 2
  • 25
  • 32
1

Since things have changed in Mac OS X, you will have to add the following in the .profile file in the base directory of the user:

export EDITOR='vim'

You can follow the following instructions:

  1. open the terminal

  2. Type cd (hit Return or Enter (this will take you to the base directory))

  3. Type echo "export EDITOR='vim'" >> .profile (hit Return or Enter and you are done)

  4. (Restart the terminal)

=========================

Or just type:

echo "export EDITOR='vim'" >> ~/.profile

Hit Enter and restart.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1306828
  • 715
  • 7
  • 14
0

If you want vi to be your default history editor (which is why I'm here):

Edit file ~/.bashrc and add

set -o vi

anywhere in the file. Then all the lovely vi command history is available (Esc + K, etc.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Since none of these answers are helping me:

Here is what the Git documentation are saying, git-commit(1) Manual Page:

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

Here is the Bash man page excerpt on export (brackets are optional):

export [-fn] [name[=word]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bentford
  • 33,038
  • 7
  • 61
  • 57
0

I had this same challenge when setting up my new MacBook Pro.

Here's how I solved it

To switch to your editor of choice (say nano) on a MacBook you will need to add the following lines to your ~/.zshrc file if your default shell is zsh or ~/.bash_profile if your default shell is bash:

export EDITOR=nano
export VISUAL="$EDITOR"

However, a simpler approach to do this will be to use the echo command to insert them into your ~/.zshrc file if your default shell is zsh:

echo 'export EDITOR=nano' >> ~/.zshrc
echo 'export VISUAL="$EDITOR"' >> ~/.zshrc

OR ~/.bashrc if your default shell is bash:

echo 'export EDITOR=nano' >> ~/.bash_profile
echo 'export VISUAL="$EDITOR"' >> ~/.bash_profile

Run the command below to activate the new configuration:

source ~/.zshrc

Or

source ~/.bash_profile

If you need to switch to other editors of choice you can replace nano with your preferred editor:

  • Vim - vim
  • Vi - vi

That's all.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Promise Preston
  • 24,334
  • 12
  • 145
  • 143