Open phpstorm:// uri:s using xdg-open in Linux

I couldn’t get this to work properly on my Linux box; so I wrote a litte script that parses the uri scheme and launches phpstorm.

Script

<?php
$arg = $argv[1];
$parts = parse_url($arg);
$query = $parts['query'];
parse_str($query, $output);
$file = $output['file'];
$line = $output['line'];
$cmd = '/usr/bin/phpstorm --line ' . $line . ' ' . $file;
exec($cmd);

Then create a new file in ~/.local/share/applications/phpstorm-protocol.desktop

[Desktop Entry]
Name=PhpStorm Protocol Handler
Exec=/path/to/the/script %u
Terminal=false
Type=Application
MimeType=x-scheme-handler/phpstorm

Make it available, run:

update-desktop-database

and then register it with xdg-open

xdg-mime default phpstorm-protocol.desktop x-scheme-handler/phpstorm

Now, when you click a phpstorm:// link in your browser choose to open it with xdg-open.

Firefox lpr workaround

For some reason unknown to mankind, Firefox doesn’t allow you to print via lpr any more.

It used to be that if you had lpr enabled on the line gtk-print-backends= in your ~/.config/gtk-3.0/settings.ini, you would be able to print directly to lpr.

But recently (I’m using Firefox 60 at the moment) this doesn’t work.

After this workaround I only kept ”file” in my settings.ini.

[Settings]
gtk-print-backends=file

Workaround

My workaround was to create a simple script that watches a folder, if it finds a PDF, print it using lpr and then remove the file.

Then a simple systemd user script to get it running in the background.

The ”printy” script

Most beautiful bash script ever.

#!/usr/bin/env bash

PRINTDIR="/tmp/printy"

mkdir -p "$PRINTDIR"

while true; do 
  sleep 2

  for file in "$PRINTDIR"/*.pdf; do
    if [ -f "$file" ]; then
      cat "$file" | lpr -l && rm "$file"
    fi
  done
done

Change PRINTDIR to where you’ll print PDF:s to from Firefox.

systemd unit script

[Unit]
Description=Fucked up Firefox printing workaround script

[Service]
ExecStart=/home/ogg/bin/printy
Restart=always
RestartSec=2

[Install]
WantedBy=default.target

Save file in ~/.config/systemd/user/printy.service.

To keep the script running after you login/logout and reboot. Enable ”lingering” in systemd.

As root, run: systemctl enable-linger ogg

Then enable and start the script:

systemctl --user enable printy

and

systemctl --user start printy

Conclusion

If you do the above, whenever you need to print anything from Firefox, use ”Print to file” and place the PDF in /tmp/printy (if you use my script above).

”Wrong” vim color scheme for root

On (what I’ve noticed so far) Ubuntu based distros, one thing that bugged me was that on a fresh install when you sudo into root and edit for example a config file, all comments were in dark blue color (which on a terminal with black background is terrible) but when doing it as a normal user the color theme was more pleasing (readable).

This is how I ”fix” this.

First, add these two lines to /etc/profile (which is read by /bin/bash /bin/dash /bin/sh etc at login)

sudo echo 'export COLORFGBG="15;0"' >> /etc/profile

Then, add this line in your /etc/sudoers file to keep the setting.

Defaults        env_keep += "COLORFGBG"

Which makes sudo remember the COLORFGBG setting. You can add other stuff here as well, for instance maybe you want’t to be able to run stuff that only you have in your PATH with sudo.

Now when you login, and sudo edit a file you get bright colors. Yay! But wait, It don’t work you say? Well, I’m guessing you run KDE or some other fancy DE and it’s terminal. Most terminals for X do run bash, but they don’t run it as a login shell, so they don’t source /etc/profile and hence the settings above won’t do any good.

My solution is to add ”-l” to the command line starting bash, which makes bash behave as it’s running as a login shell.

Here’s how to do it in Konsole in KDE:

konsole_settings konsole_shell