Open PHP files in the right editor, automatically.

This is one of those cases when, viewing a file in your window manager, you see a .php file you want to quickly inspect. You double click on it, and…

  • It quickly opens Geany, or your default quick file editor, but PHPStorm is already running right there
  • Or the opposite: you just want a quick peak for a single little file, but it slowly cold starts your 4 GB IDE :x

What if your file manager just knew? That's the idea of the following bash script that acts as the default handler for .php files:

  • PhpStorm is running? Open the file there, in the existing instance.
  • PhpStorm is not running? Open in Geany – fast, lightweight, no wait for a quick glance.

The script

Drop this in ~/.local/bin/open-php:

  1. #!/bin/bash
  2. # Open .php files: use PhpStorm if running, otherwise Geany
  3.  
  4. if pgrep -x "phpstorm" > /dev/null || pgrep -f "phpstorm" > /dev/null; then
  5.     # PhpStorm is running - open file in existing instance
  6.     phpstorm "$@"
  7. else
  8.     geany "$@"
  9. fi

Then chmod it +x. Also make sure obviously that your IDE and light editor binaries are in the path. To find the exact name of your IDE process, you can use pgrep -a -f [Pp]hp[Ss]torm for example.

The setup

Set open-php as the default application for .php files. Right-click any .php in your file manager, pick "Open With Other Application", point to ~/.local/bin/open-php, and set it as default.

Alternatively, at least if using Nemo (Linux Mint for instance), you can create ~/.local/share/applications/open-php.desktop with the following:

  1. [Desktop Entry]
  2. Name=Open PHP File
  3. Exec=/home/YOUR_USERNAME/.local/bin/open-php %F
  4. Type=Application
  5. NoDisplay=true
  6. MimeType=application/x-php;text/x-php;

Replace YOUR_USERNAME with your own user name, then register it as the new handler:

  1. update-desktop-database ~/.local/share/applications
  2.  
  3. xdg-mime default open-php.desktop application/x-php
  4. xdg-mime default open-php.desktop text/x-php

From now on, double-clicking a PHP file does the right thing automatically. Heavy tooling when it's already warm, or the lightweight editor when it's not. Best of both worlds!

Leave a Reply

Your email address will not be published. Required fields are marked *