<p>You can use an execute command like</p>

<div class="highlight highlight-source-shell"><pre>sh -c <span class="pl-s"><span class="pl-pds">'</span>if [ -x "%f" ]; then exec "./%f"; else exec python "%f"; fi<span class="pl-pds">'</span></span></pre></div>

<p>Or even safer, checking it has a shebang that looks good:</p>

<div class="highlight highlight-source-shell"><pre>sh -c <span class="pl-s"><span class="pl-pds">'</span>if test -x "%f" && head -n1 "%f" | grep -q "^#!.*python"; then exec "./%f"; else exec python "%f"; fi<span class="pl-pds">'</span></span></pre></div>

<p>Or just trusting the shebang, if any:</p>

<div class="highlight highlight-source-shell"><pre>sh -c <span class="pl-s"><span class="pl-pds">'</span>exec $(head -n1 "%f" | grep -Po "(?<=^#!).*$" || echo python) "%f"<span class="pl-pds">'</span></span></pre></div>

<hr>

<p>Geany will probably not try to read the shebang by default, because a user could select another filetype and reasonably expect his choice to be followed.  We could possibly consider executing the file directly if it's executable, but it can also lead to unexpected results if that file is mistakenly marked as executable (like on some non-POSIX file systems).<br>
For now, IMO the best solution is for you to either make your own Python filetype run Python3, or use a conditional run command like I mentioned above.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/geany/geany/issues/1298#issuecomment-260024924">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABDrJ2lSqEQ40i7ixWuPUF9yr269PBp4ks5q9LZGgaJpZM4Kv_Hv">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/ABDrJ9eYsitQiQWyAOX9MrgtkM9CPLyQks5q9LZGgaJpZM4Kv_Hv.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/geany/geany/issues/1298#issuecomment-260024924"></link>
  <meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/geany/geany","title":"geany/geany","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/geany/geany"}},"updates":{"snippets":[{"icon":"PERSON","message":"@b4n in #1298: You can use an execute command like\r\n```shell\r\nsh -c 'if [ -x \"%f\" ]; then exec \"./%f\"; else exec python \"%f\"; fi'\r\n```\r\nOr even safer, checking it has a shebang that looks good:\r\n```shell\r\nsh -c 'if test -x \"%f\" \u0026\u0026 head -n1 \"%f\" | grep -q \"^#!.*python\"; then exec \"./%f\"; else exec python \"%f\"; fi'\r\n```\r\nOr just trusting the shebang, if any:\r\n```shell\r\nsh -c 'exec $(head -n1 \"%f\" | grep -Po \"(?\u003c=^#!).*$\" || echo python) \"%f\"'\r\n```\r\n---\r\nGeany will probably not try to read the shebang by default, because a user could select another filetype and reasonably expect his choice to be followed.  We could possibly consider executing the file directly if it's executable, but it can also lead to unexpected results if that file is mistakenly marked as executable (like on some non-POSIX file systems).\r\nFor now, IMO the best solution is for you to either make your own Python filetype run Python3, or use a conditional run command like I mentioned above."}],"action":{"name":"View Issue","url":"https://github.com/geany/geany/issues/1298#issuecomment-260024924"}}}</script>