I am trying make a filetype dependent gvim alias in bashrc as:

if [[ $file == *.tex ]]; then
  alias vi="/home/rudra/bin/vims.py"
  alias vi="gvim"

And I am expecting to run ~/bin/vims.py foo.tex for vi foo.tex file, and gvim bar.txt when I type vi bar.txt. But, its not working, and only running gvim foo.{tex/txt} for all extensions.

It is also showing:

which vi
alias vi='gvim'

So I guess, if condition for *.tex is not working at all. Any idea why?

I am using:

bash --version 
GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu)

\vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jan 26 2017 07:53:42)


vi --version
bash: syntax error near unexpected token `--version'

Here's a shell function, edit, that will open a file with an editor. It defaults to gvim, but if the file has a .tex suffix it chooses another editor:

function edit
    typeset argc="$#"
    typeset file="${!argc}"

    typeset cmd="gvim"

    case "$file" in
        *.tex)  cmd="/home/rudra/bin/vims.py" ;;

    command "$cmd" "$@"

This makes it easy to add other editors for specific filename suffixes.

This function may be put in your ~/.bashrc file.

The bash manual contains the statement

For almost every purpose, aliases are superseded by shell functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.