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:
- #!/bin/bash
- # Open .php files: use PhpStorm if running, otherwise Geany
- if pgrep -x "phpstorm" > /dev/null || pgrep -f "phpstorm" > /dev/null; then
- # PhpStorm is running - open file in existing instance
- phpstorm "$@"
- else
- geany "$@"
- 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:
- [Desktop Entry]
- Name=Open PHP File
- Exec=/home/YOUR_USERNAME/.local/bin/open-php %F
- Type=Application
- NoDisplay=true
- MimeType=application/x-php;text/x-php;
Replace YOUR_USERNAME with your own user name, then register it as the new handler:
- update-desktop-database ~/.local/share/applications
- xdg-mime default open-php.desktop application/x-php
- 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!