1

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

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

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'
    /usr/bin/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)

also:

vi --version
bash: syntax error near unexpected token `--version'
1
  • In zsh there is suffix alias alias -s tex=$editor, seen here. Switch to zsh however is for another day and not always recommended. Commented Jun 29, 2021 at 15:56

1 Answer 1

3

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" ;;
    esac

    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.

2
  • I doubt For almost every purpose, aliases are superseded by shell functions. as aliases are quick and dirty which is super these days. With zsh you have even more alias options and flags like -s and -g. Commented Jun 29, 2021 at 15:58
  • 1
    @Timo This question is about the bash shell and I was quoting the shell's manual. If you want to question the quote, please do so on the help-bash mailing list. Commented Jun 29, 2021 at 17:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.