To my personal surprise, yes, there's a way in zsh
. This question led me to this answer talking about "suffix aliases" in zsh
. Not that yours is a duplicate -- The first question was about a bash
way to do it; the other answer was just about "favorite zsh
features".
To do it with a suffix alias:
alias -s py=vim
Add that to your ~/.zshrc
to make it permanent.
Personally, I'd recommend against it for at least two reasons. First and foremost, as @pizzapants184 pointed out in a comment, this overrides even the ability to execute a Python file using its path. For example, ./run_me.py
will not execute, even if it is set as executable, and even if it has a shebang line (e.g. #!/usr/bin/env python3
or #!/usr/bin/python3
). It's a neat feature, but it would be nice, IMHO, if it were "smarter."
Also, the extra 3 characters ("vi
") become such muscle memory that will serve you well on other systems that you haven't configured this way or that don't have zsh
installed.
Alternative method
And while I still don't necessarily recommend it, you can set the shebang line for an individual file to force it to load in vim
when executed:
- As the first line of the file (we'll call it
edit_me.py
):
#!/usr/bin/env vi
- Set the file executable:
chmod +x edit_me.py
- Running it (e.g.
./edit_me.py
) will open it in vim
(or fallback to vi
if not available).
python filename.py
which I have no problem doing. But keyboard shortcut would work for me. – Sayandip Dutta Jun 27 '21 at 15:48