Move to the End and Beginning of a Word in PHPStorm

Another Macintosh keyboard shortcut that will be second nature to most Mac users is using ⌥← to move to the beginning of a word and ⌥→ to move to the end of a word. This moves the cursor to the left of the first character or the right of the last character. Try it in Mail. In TextEdit. In another editor you have installed like Sublime Text. It works the same across any Mac application I can think of except for PHPStorm. It feels so… wrong.

There is no existing Keymap setting in PHPStorm to move to the beginning and end of a word. Instead, there are command for moving to the next and previous words:

Move to Next Word

To fix this abomination, and affront to Clarus the Dogcow, you will need to install a PHPStorm plug-in called “Missing in Actions.” “Missing in Actions” actually will add quite a bit of functionality but, really, the thing that I was really looking for was the proper handling for ⌥← and ⌥→. Read more about Missing in Actions at the plug-in’s Github repository.

Go to PHPStorm > Preferences > Plugins. From there, choose “Browse Repositories.” Search for “Missing in Actions” and click the Install button. The plug-in will download and then you will be prompted to restart PHPStorm for the changes to take effect.

After restart is complete (you may have time to refresh your coffee), you will see new options available in the Keymap settings of PHPStorm:

  • Move Caret to Next End of Word
  • Move Caret to Previous Start of Word

Set these to ⌥→ and ⌥← and all will be well again.

Move Cursor to the Top of the Page in PHPStorm

I have started using JetBrain’s PHPStorm editor at work on a Macintosh and one of the things that has irked me is that some of the standard Macintosh keyboard shortcuts don’t work out of the box. For example, if you’re a seasoned Macintosh user, you are most likely used to using ⌘↑ to go to the top of the document. By default, in PHPStorm, this brings up the breadcrumb navigation. To get the default Macintosh behavior, you’ll want to edit the Keymap Preferences.

Go to PHPStorm > Preferences > Keymap to bring up the Preferences dialog. On the right-hand side of the dialog, you’ll be able to search for the command that you want to set the keyboard shortcut for. It wasn’t obvious to me what the command was for scrolling and moving the cursor to the top in PHPStorm, but I finally found that it is “Move Caret to Page Top.”

Move Caret to Top

Double-click this entry and set it to ⌘↑. Since PHPStorm assigns this by default to the “Show Breadcrumbs” command, you will be given an option to remove this existing mapping.

In addition to setting this keyboard shortcut, you can also set “Move Caret to Page Top with Selection” to Shift-⌘↑ and the corresponding commands for moving the caret to the bottom of the page. In my mind, this makes PHPStorm act much more like you’d expect an editor on the Mac to work.

Setting up Mac navigation shortcuts in iTerm 2

If you’re a Macintosh user, it is probably second nature to use ⌥← to go back one word and ⌥→ to forward a word. Similarly, ⌘← will bring you to the beginning of a line while ⌘→ will jump you to the end. However, by default, iTerm 2, a great replacement for macOS’s included Terminal application, is not set up to work this way. Fortunately, it is possible to map your keys in a way to get this functionality.

In iTerm, go to your Preferences and then the Profiles tab. Click on the Profile you are using and choose the “Keys” tab.Keys preference

From there, you can click on the + button below the existing Key Mappings and use the following settings to emulate the default Macintosh behavior.

Back One Word

Back one word

Forward One Word

Forward one word

Go to Beginning of Line

Beginning of line

Go to End of Line

End of line

 

AppleScript for playing Rifftrax

Anyone that knows me, knows that I am a huge fan of Mystery Science Theater 3000 and, an off-shoot of that, Rifftrax. One of the great things about Rifftrax is that they sell some MP3-only commentaries. Since they don’t need to license the movie, they can target summer blockbusters like Star Wars: Rogue One, Twilight, and Harry Potter. The only downside to this is that you have to sync up the audio of the MP3 to the movie’s audio. This isn’t too big a deal, especially with Rifftrax’s new auto-syncing application for your phone. That said, sometimes I prefer to watch Rifftrax on my computer for the convenience of the audio coming from the single source (my laptop speakers).

At the beginning of a Rifftrax commentary, instructions are given for when to pause the MP3 and at what point in the movie to un-pause the MP3. That way, the jokes will line up with the movie audio. I will watch movies using VLC and listen to the commentary using QuickTime. Easy, right? But what happens when you need to pause the movie? You have two separate applications playing and, no matter how fast you are, you will not be able to hit the pause buttons of both applications at the same time.

To deal with this issue, I wrote up an AppleScript to hit pause on both applications when running the script. Compiling this script to an application, I can place it in the Dock of my Macintosh when playing a Rifftrax and click to stop and resume at any time. It is a really simple little script to handle this:

tell application "QuickTime Player"
    if document 1 is playing then
        pause document 1
    else
        play document 1
    end if
end tell
tell application "VLC"
    play
end tell

I think the code is pretty readable. QuickTime needs to be told to play or pause the audio if the file is actually being played or not. VLC will just toggle between the two so we don’t need to bother checking. To use this script, simply paste the code into Script Editor, which should have come with your Macintosh, and Export the script as an Application.

Export as Application

That is all there is to it. “It’s time for Rifftrax.”

A Couple Quick Mac Tips

Just a couple of quick Mac tips. First, many programs support using Command-<number> to choose that tab in the program. Some programs are explicit about it and might show the shortcut, like iTerm 2, but this will work in all major browsers I have tried as well as Sublime Text.

Also, most times when you are using a key combination to cycle through something (like tabs in a browser or the Command-Tab program switcher), using Shift will reverse the direction. So if you go past your intended target, you can often go backwards instead of cycling all the way back through.

Quick Macintosh Tips

It is sometimes hard to explain to people why I enjoy working on a Macintosh. It isn’t because they are shinier or I have fallen prey to the “reality distortion field.” Rather, there are a lot of nice tweaks, that are sometimes not so obvious, that make working with the Macintosh such a nice experience.

Quit or hide an application through the app switcher

When using the Command-Tab key combination to cycle through your open applications, you can easily quit or hide the currently selected application. Simply use the standard key combinations of Command-Q to quit the selected application or Command-H to hide it. This is a quick way to get rid of the clutter that can accumulate through a long work session. Just keep one finger pressed down on the Command key and hit Q or H instead of tab to move onto the next application. Oh, and also, to switch direction, hit Shift when Command-tabbing in case you go past the application you wanted.

Getting the folder structure of a file in an application

I love this one and use it all the time. In almost any application, when you are working on a document and you would like to see it in the Finder, Command-click on the application’s title bar.

Title bar

Then, select any of the parent’s folders to jump to it in the Finder.

Most everything is draggable

Speaking of the title bar of an application, often times you can drag the icon to wherever you need it. No need to go to the Finder, get the file and go from there. Just drag the icon from the title bar to your email to attach it or a website to upload it. Oftentimes, if I have a screenshot that I have opened in Preview, I will just drag and drop the icon onto a website like Jira or Zendesk to upload it.

Scroll background items

I really like not having to set the focus to another window to scroll it. For example, if I am working on an Excel document next to an Word window where I am taking notes, I can scroll to view more columns without leaving Word. I simply hover over Excel and use the multi-touch functions to scroll the Excel document to where you need it – vertically or horizontally.

Using MailCatcher for local mail testing

When developing web applications on your local machine, you will often want to test e-mails. For the longest time, I would use an environmental switch in PHP depending on the domain (for example, I would set different values if the domain was stirrell.second-cup-of-coffee or www.second-cup-of-coffee.com) and include values for email addresses. I had my local MAMP really sending out emails and this worked fine. Of course, there is always the chance you’re going to miss an email somewhere and inadvertently send out a real email when you just want to test.

A good solution to this problem is to use a “fake” SMTP server – a program that simulates sending an email and allows you to see what would have been sent – but only exists on your local machine. I had been using one on the Macintosh – MockSMTP which is available on the Mac App Store. It had been working fine for me. I set it up according to the instructions here and it all worked swimmingly… until I upgraded to the newest Mac OS X, Mavericks. I did not receive any errors but I also never received any test emails. Bummer. I sent a tweet to the folks who make MockSMTP but never got a response – the app itself has not been updated since 2011 so I fear that an update might not be coming.

I searched around for an alternative to MockSMTP. Many of the ones I saw were Java-based multi-platform solutions which did not appeal to me. I finally decided to give the free MailCatcher a try. MailCatcher is a Ruby Gem so is installed via the command line and called through the command line. I followed the instructions at their site and it worked straight away.

To get MailCatcher working with PHP, I edited the php.ini configuration files to use catchmail for the sendmail path. In MAMP, you simply go to File > Edit Template > PHP and then choose the version of PHP you are using. You can tell what version of PHP you are using with MAMP by going to the “Server” tab:

PHP version

I added the following line to my configuration file:

sendmail_path = /usr/bin/env catchmail

Update for macOS Sierra – I found that I needed to change this line to:

sendmail_path = /usr/bin/env /usr/local/bin/catchmail -f catcher@mailcatcher.me

Once that is set, all email sent by PHP will go to MailCatcher. You can view mail sent to MailCatcher using a browser to connect to your local machine on port 1080 (http://127.0.0.1:1080/). You will get a simple web interface for viewing the mails that have come in. From then on, simply type “mailcatcher” in the Terminal to start up MailCatcher. This will give you a simple way of testing emails on your local machine without worry of emails getting sent to the client.

Sublime Text’s Extend Selection to Word in TextMate 2

One of the coolest features of Sublime Text 2, in my opinion, is the ability to use the command, “Extend Selection to Word” with Sublime Text’s multiple cursors. Using this command, one can highlight a word they want to change, select the command (or the key command equivalent) which selects the next instance of the word with multiple cursors and then you can replace both words (or as many words as you select) by typing once. I thought that TextMate 2 was missing this feature and that I would have to replace words one at a time or rely on replacing all within a selection. It turns out, though, that TextMate has this feature as part of the Text Bundle.

By default, the “Extend Selection to Include Next” command is bound to the Control-w keys and is available in the Text bundle under Menu Actions > Selecting.

Extend Selection to Include Next

You can, of course, change the key binding (I made it the same as I have it in Sublime Text 2 – Command-D). I absolutely love this feature and am glad to see that it is available in TextMate 2.

Adding a Prefixr command to TextMate 2

I was recently looking into how to add a Prefixr command to TextMate 2. For those of you not familiar, Prefixr is a way (created by Jeffrey Way of NetTuts) to take a new-ish CSS property like box-shadow or transform and add all the vendor-specific prefixes automatically. So:

transform: rotate(-7deg);

becomes:

-webkit-transform: rotate(-7deg);
-moz-transform: rotate(-7deg);
-o-transform: rotate(-7deg);
-ms-transform: rotate(-7deg);
transform: rotate(-7deg);

to accomodate the prefixes for Webkit browsers (Safari and Chrome), Firefox, Opera and Internet Explorer. These prefixes are used by the different browser vendors to experiment with newer CSS properties before the standard has been finalized. So, ideally, these prefixes would eventually go away as the these properties become standardized across browsers.

Jeffrey actually has an example of how to add a command to TextMate 1 in his introduction article to Prefixr and this works perfectly in TextMate 2. I simply went to “Bundles > Edit Bundles” and chose the CSS bundle. I then clicked “New” and chose “Command.” In my new command, I pasted in the command from Jeffrey’s article:

curl -sSd "css=$TM_SELECTED_TEXT" "http://prefixr.com/api/index.php"

I chose Selection for the Input so that my highlighted text would be acted on. I also entered “source.css” for the scope and… it worked great! But, I tested it in a SASS document (.SCSS) and, because of the scope… nada. It wasn’t available as an option when searching all my commands. Well, one can add more than one scope by separating them with commas. So I changed the scope to:

source.css,source.scss

And the command is now available to me in both CSS and SASS.

Sitewide settings in CodeIgniter

I recently wanted to set up a system to allow a client to edit sitewide configuration settings on a custom CodeIgniter site. I didn’t want to hardcode it in a configuration file and I wasn’t sure exactly how it would need to be added to in the future. I decided to use a system much like WordPress with a custom key/value table in the database – like WordPress’s wp_options table. So, I created a similar table – a primary, auto-incremented key, a settingKey varchar field, a settingValue text field and a modified date field with a trigger (just to be able to monitor changes on the setting). My MySQL for the table looks like this:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for `siteSettings`
-- ----------------------------
DROP TABLE IF EXISTS `siteSettings`;
CREATE TABLE `siteSettings` (
`settingID` int(4) NOT NULL AUTO_INCREMENT,
`settingKey` varchar(50) NOT NULL,
`settingValue` text,
`recordLastModDate` datetime DEFAULT NULL,
PRIMARY KEY (`settingID`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
delimiter ;;
CREATE TRIGGER `siteSettingsUpdateLastModDate` BEFORE UPDATE ON `siteSettings` FOR EACH ROW BEGIN
SET NEW.recordLastModDate = CURRENT_TIMESTAMP;
END;
;;
delimiter ;

Once the table was in place, I looked into ways to always have these configuration settings available to me through the CodeIgniter site. I saw some cool stuff online using hooks and I think that would have worked but it seemed a little intense for the simple site I was working on. So, I decided instead to create a model, settings_model.php, and then add the model to the autoload.php file so that it would always trigger:

$autoload['model'] = array('settings_model');

In the model itself, I had a function, LoadSitewideSettings that would simply get the settings from the database table and add them to CodeIgniter’s configuration settings. Here is my LoadSitewideSettings function:

public function LoadSitewideSettings() {
$settings = array();

// Get the settings from the database
$query = $this->db->get('siteSettings');
foreach ($query->result() as $settingRow) {
$settings[$settingRow->settingKey] = $settingRow->settingValue;
}

$this->config->set_item('siteSettings', $settings);
}

So, I just grab all the data from that database table, run through each item to get the keys, and then use CodeIgniter’s config->set_item function to assign the values to CodeIgniter’s configuration variables. I create an array and save all of those elements in the variable, ‘siteSettings,’ to try to avoid any naming collisions with other CodeIgniter variable names.

Finally, I call my LoadSitewideSettings function in the constructor of my model to make sure I grab the configuration values. When I want to access these configuration variables, I use CodeIgniter’s config->item command like this:

$this->config->item('defaultOrderEmails', 'siteSettings');

So, the record in my configuration file with the key, defaultOrderEmails, will be grabbed and made available to me anywhere on the site.