Dual monitors with Openbox and Xrandr

Some of the popular desktop environments make it really easy to use two monitors. Some even automatically detect and arrange them for you. However, Openbox doesn’t do this (it’s a window manager, anyway, not a DE) out of the box. And I don’t like to install heavy apps to arrange my displays when I can do it quickly with Xrandr from a terminal emulator.

First, type xrandr in a terminal. This will display the detected monitors and their resolution.

Now, you can enter a command to adjust the resolution and layout of your displays: xrandr –auto –output VGA-1 –mode 1440×900 –right-of LVDS-1

That command is for the external monitor I usually have to the right of my laptop. VGA-1 is the external monitor and LVDS-1 is my laptop’s built in monitor. This command tells xrandr to arrange the external monitor to the right and set the resolution to 1440×900.

I then use this command: xrandr –auto –output LVDS-1 –mode 1280×800 –left-of VGA-1 to set the resolution of my internal display. I usually add these two commands to openbox’s autostart script (/etc/xdg/openbox/autostart) so that Xrandr sets my displays properly when openbox starts up.

Normal Mode Shortcuts in Vim

One of the most powerful things about Vim is the ability to assign arbitrary key shortcuts or mappings. This allows you to make your most frequent activities into very fast mappings of just a few keystrokes. For example, I have the movement from the current tab to the previous one assigned to “gr” in normal mode.

You can do this by going to normal mode and entering

:map gr :tabp

and then pressing enter. This tells vim to enter :tabp and a carriage return whenever it sees you press “gr” in normal mode. Otherwise you would have to enter “:tabp” and press enter yourself.

Entering the shortcut in Vim like this will work, but it will only last until you close that instance of Vim. If you want the mapping to work every time you use Vim, you can save it in a file called .vimrc in your home directory. Open this file with

vim ~/.vimrc

and then save that shortcut in it. The next time you open Vim, it will already know about your mapping.

This is a simple normal mode mapping, but it saves keystrokes that take time. You can do all sorts of complicated mappings in normal mode and Vim’s other modes. Play around and experiment to see how much you can do with just pressing a few keys.

Pare down your read-it-later list by going old school

Do you follow too many subjects across the Interwebs? How about subscribe to a firehose like Hacker News (the RSS feed spouts 83.3 items per day)? If you answered yes to either of those questions, then you probably have an extensive collection of things you want to “read later”.

If you take a look at my Google Reader trends above, you can see that I read a lot of RSS items daily. I rarely click “Mark all as read”, so that ~163 items/day statistic is quite accurate. However, I often only have time to browse the titles and star the items that I promise myself I’ll read later. You guessed it: my number of starred items is insane (131 just from the last 30 days) and I have no hope of ever reading them all.

In the end, there’s many more items that I think I’ll read later than I actually do. So starring items ends up being a waste of my time (I tell myself reading this much tech news isn’t a waste of time, though!).

To combat this, I’ve added a layer of work between myself and my read-it-later list. Now, instead of starring an interesting item (just an easy tap on the “S” key), I make myself copy and paste the link into a text file. This extra work, although slight, causes me to think twice and keeps me from saving items that I know will be sitting at the bottom of my read-it-later collection for months. So save yourself some time (or even guilt!) and protect your read-it-later stash with this little trick.

Making a simple query to a MySQL database via PHP

To make a query to a MySQL database using PHP, you will first create the PHP script. I’m assuming that you have created your database and have the connection information.

/*declare your connection information*/
$host = 'yourHost';
$databaseUser = 'yourUser';
$databasePassword = 'yourPassword';
$databaseName = 'yourDatabase';

/*Connect to the host and select the database*/
@mysql_select_db($databaseName) or die( "Unable to select database");

/*create a query. here we will select all entries from the posts table*/
$query = "SELECT * FROM `posts`";

/*execute the query and assign the result to a variable*/
$result =  mysql_query($query);

/*grab the number of rows returned by the query*/

/*close the connection*/

/*the following while loop goes through all the rows of the result 
and outputs the 'title' of each entry of the posts table*/

echo "<html>";
while ($i < $rowCount) {

    $title=mysql_result($result, $i, "title");

    echo "$title <br>";
echo "</html>";

Then you can take this script and create a webpage where the title to each entry will be outputted in HTML.

Receive an alert when a terminal command completes

It’s pretty common to issue commands in a Linux terminal that take a little while to complete. Instead of sitting and waiting for the program or process to complete, you can issue the alert command so you know when it completes.

For example, if you issue the command sleep 5; alert, your terminal will run the sleep program for 5 seconds and then you will receive a pop up alert on your desktop. This lets you know that the process has finished. Here it is in action:

This is great for long-running dd commands or installation of new programs that take a while. As an aside, I did have to install libnotify-bin for this to work properly.

Wake Ubuntu with USB mouse or keyboard

I tend to suspend my Ubuntu machine quite a bit. A three second wakeup is a lot faster than a 30 second boot and I can keep my windows open while the machine is asleep without using much more power than a fully shut down computer.

Instead of having to reach for a power button on my computer’s case, I have set up Ubuntu to come out of suspend mode when I press any key on my keyboard.

After some research, I found I could issue this command:

sudo sh -c 'echo "USBE" > /proc/acpi/wakeup'

I have seen some people use USB0 instead of USB0, but since it was not listed under wakeup devices by acpi, I used USBE. You can judge for yourself as to which you should use.

There you have it. Your machine should wake up when you press a key, given your keyboard is USB.

Loading images from a directory with PHP

This is how you can use PHP to display all of the images in a directory (folder) where the PHP page lives.

$files = glob("*.jpg");
$fileCount = count($files);
for ($i = ($fileCount-1); $i >= 0; $i--) {
  echo '<img src="' . $files[$i] . '"><br />';

Note: this will only display images with the extension .jpg. You will have to modify it to display other image types (png, gif, etc.).