Bash – place the aliased version of an existing command in /usr/bin/

aliasbashexecutablegitvim

I use Vim a lot, and I know how I can start vim in insert mode. So I have an alias named vii in my .bash_aliases file.

On other hand I use Git a lot too, and I have this line in my .gitconfig:

[core]
    editor = vi

To write a commit message the vi editor is opened every time and I have to go in insert mode. So I thought of replacing vi with vii, and did.

But the problem is when I do git commit, instead of opening the vim in insert mode, it gives this error:

error: cannot run vii: No such file or directory
error: There was a problem with the editor 'vii'.
Please supply the message using either -m or -F option.

This makes clear that git does not looks to .bash_aliases file, even it isn't related to bash in any way. It does directly looks if there is /usr/bin/vii or not. And executes it if it is.

The Question

Can I place the aliased version of vi as vii in /usr/bin/?

(and please don't suggest me to use git commit -m "<commit message>". There are other situation where I need vim in insert mode.)

Best Answer

Aliases are internal to each of your current shell environments - they are expanded by the currently running shell (bash in your case), so they only have effect on what you execute by typing/pasting in your terminal.

You have at least two options here:

  • create a wrapper script named vii that will execute vim -c 'startinsert' and put it preferably in /usr/local/bin/ (or $HOME/bin, if it exists and is in your search path). The script only needs to contain

    #!/bin/sh1
    exec vim -c 'startinsert' "$@" 2
    

    (Make sure to make it executable by running chmod +x /usr/local/bin/vii.) Depending on the PATH configuration of your git/other programs, you may need to specify full path to that wrapper script (i.e., editor = /usr/local/bin/vii).

  • If it is ok for you to have vim always start in insert mode, configure it to do so by adding startinsert at the end of .vimrc.


1   You can write the "she-bang" line as #!/bin/bash, but there's no need to in a script that contains no bashisms.
2   $@ must be in double quotes in case the script is ever called with argument(s) that contain space(s). startinsert does not need to be quoted (but it doesn't hurt).

Related Question