2

I was wondering whether it is possible to open python files in vim, without typing "vim" in front of it. For example: Instead of:

vim filename.py

simply

$ filename.py

Would open the file in vim.

I believe it is not upto .vimrc but .bashrc or .zshrc (in my case), which would interpret the .py files to be executable with vim or something. I am using ubuntu (wsl2), with zsh (oh-my-zsh). It is possible that this has been asked before, but I couldn't find such a question. Thank you.

  • 1
    How would you run the "filename.py" script? If you don't make the file executable then you could leverage the command not found handler https://unix.stackexchange.com/a/333259/194382. This seems a strange thing to want to do, to save typing 4 characters. Perhaps you would be better off defining a keyboard macro so say typing control-e just went to the start of the line and added v i m space return. That way you could type the filename and return to execute the file or the filename and control-e to edit the file? – icarus Jun 27 '21 at 15:45
  • Yes, the macro seems like a good idea. Thanks. By the way, for running the script I can type python filename.py which I have no problem doing. But keyboard shortcut would work for me. – Sayandip Dutta Jun 27 '21 at 15:48
  • While I know I've answered the question below, let me also recommend, since you are running on WSL, that you consider Visual Studio Code for Python editing. I love Vim, but VSCode's WSL integration (with the "Remote - WSL" extension) is pretty amazing. There's also an extension for vi keybindings, of course. – NotTheDr01ds Jun 28 '21 at 19:56
  • @NotTheDr01ds I'm still not accustomed with the feel of VSCode. But yes, I plan to gradually switch to it. The fact that you can run jupyter nb in it is pretty useful too. Thanks. – Sayandip Dutta Jun 29 '21 at 03:46

1 Answers1

5

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).
NotTheDr01ds
  • 3,547