Home > python > Insert the path of the Python interpreter easily

Insert the path of the Python interpreter easily

Problem
How many times have you written “#!/usr/bin/env python” in your life? A few hundred times? :) How to insert this line easily?

Solution #1
I mainly use vim. So far I have done it the following way.

In vim, type this on the first line in normal mode. This is the mode that vim will usually start in, which you can usually get back to with ESC. At the end press ENTER.

!!which env<ENTER>

!!” brings you to command mode and the current line (which was empty) is replaced with the output of “which env“. Thus the first line becomes “/usr/bin/env“. All you need to do is add “#!” and “python” manually.

Fine, but I got fed up with this. It’s still too much typing. How to do it easier?

Solution #2
Write a bash script called “py” with the following content:

echo "#!`which env` python"

Put it somewhere in your PATH and make it executable.

Then, in vim type this on the first line:

!!py<ENTER>

Phew. I should have thought of it years ago.

About these ads
Categories: python Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 63 other followers

%d bloggers like this: