Love hopes all things

The Truelove

There is a faith in loving fiercely
the one who is rightfully yours,
especially if you have
waited years and especially
if part of you never believed
you could deserve this
loved and beckoning hand
held out to you this way.

I am thinking of faith now…

Well Love hopes all things and I hope you. I love you

Advertisements

Howto remap Alt key to Meta key and Windows key to Option key on a PC keyboard for OSX?

Apple is famous for beautiful design with poor usability. After typing for a while, my fingers hurt so I decided not to use the Mac keyboard and replace it with a PC keyboard, in my case is the shiny and hefty Realforce 87UW keyboard. The downside of PC keyboard is the position of the Windows ~ Apple key  is swapped with the Alt key. In one of my old post, I used DoubleCommand to remap those keys, yet I have been feeling there must be a easier way. Now I found a way w/o installing any external apps, here is how:

1. Go to System Preferences > Keyboards > Modifiers Keys

2. Select the keyboard: Realforce 87 (yours differs)

3. Select Apple Command for Option Key

4. Select Option Key for Apple Command

OSX Tip: Open Terminal Here at Folder

For whom who has to interact with Terminal daily, automating repetitive task would save time thus improve productivity. In this tutorial, I would like to share with you a very simple trick where you can Open Terminal at Folder using Finder. Traditional method is to drag the folder to your Terminal after line cd. The better way is:

1. Open System Preferences > Keyboards > Keyboard Shortcuts
2. Select Services > New Terminal at Folder

Open Terminal at Folder

Now you can right click on a folder and select New Terminal at Folder 🙂

New Terminal at Folder

Shell Tips: How to recursively archive many folders

In this short tutorial, I’ll show you how to recursively archive (tar.bz2) folders into archives file with same folder names. I’ll use find to list all directories; then archive each folder using tar and parallel.

Scenario

Let’s assume that we have following directories:

drwxr-xr-x 37 macbookair staff 1258 Nov 22 18:16 folder_number_one
-rw-r--r-- 1 macbookair staff 35342662 Nov 22 18:16 foo_file.txt
drwxr-xr-x 9 macbookair staff 306 Nov 22 18:16 folder_number_two
-rw-r--r-- 1 macbookair staff 16505 Nov 22 18:16 foo_bar.txt
drwxr-xr-x 7 macbookair staff 238 Nov 22 18:16 folder_number_three
drwxr-xr-x 7 macbookair staff 238 Nov 22 18:17 folder with space

Objective

The objective is to recursively archive folder_number_one, folder_number_two, folder_number_three into folder_number_one.tar.bz2, folder_number_two.tar.bz2, folder_number_three.tar.bz2 respectively.

Solution

One magic command does ALL:

$ find ./* -type d -print | parallel tar cjvf {}.tar.bz2 {}

How it works?

$ find ./* -type d -print
./folder with space
./folder_number_one
./folder_number_three
./folder_number_two

Next, I used parallel to parse in the list to tar:

find ./* -type d -print | parallel tar cjvf {}.tar.bz2 {}

{} represents every single folder name in the list. You could customize the archive name easily by amending to this {} symbol. For example:

find ./* -type d -print | parallel tar cjvf {}-archived.tar.bz2 {}

which append -achived into the filename to give you:

folder_number_one-archived.tar.bz2
...

Why this solution is better than using ls and awk?

There are many solutions, one is to use ls -l to list all directories then pipe the output to awk to grep the last field start with d (that is directory type). Yet that solution suffers one flaw: directory names’spaces will be omitted in the output.

TextMate Tip #6: Jump to line

TextMate Go to Line

To quickly Go to a line number, simply use this shortcut Apple/Command L

TextMate Tip #5: Duplicate a line

Forget about the inefficient conventional Copy & Paste with Apple/Command C then Apple/Command V. I’ll show you a faster macro, that is Ctrl Shift D on the line that you want to duplicate 🙂

Fix Postgres permission denied issue on OSX Lion

The good thing is that Apple decides to bundle PostgresSQL 8.4 with OSX Lion which cause conflict if you install Postgres 9.x using brew or Macport. You will likely bump into following error:

    $ psql -d postgres -U postgres
    psql: could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? 

So what’s going wrong here? Because the stock postgresql has higher precedence in PATH.

To fix this, simply make /usr/local/bin highest precendence for $PATH.

Local scope (recommended):
Edit ~/.bash_profile

export PATH=/usr/local/bin:$PATH

System-wide scope:
Edit your /etc/paths and move your /usr/local/bin to the top.

$ sudo nano /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Ruby Tip: Reference to previous line

Travis Winters showed me a nifty trick to reference to previous line in rib by using the symbol _ (aka underscore). Look at the example below:

$ irb
ruby-1.9.2-p290 :002 > "hello world"
ruby-1.9.2-p290 :002 > @hello = _
ruby-1.9.2-p290 :002 > puts @hello
hello world
 => nil

Rails Tip: See ActiveRecord’s SQL Statements log output under Rails Console (rails console)

A good trick for prototyping/testing ActiveRecord is to have the SQL statement output to your console as you work. It could be achieved so with:

$ rails c
Loading development environment (Rails 3.0.10)
ruby-1.8.7-p352 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)

Whenever your code relates to AR, the SQL statement will be piped directly to STDOUT:


ruby-1.8.7-p352 :002 > Person.find 1
  Person Load (0.1ms)  SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1
 => #<Person id: 1, name: "one", age: 23, created_at...

Ruby Tip: Fetch a terminal command output using bartick directive

If you need to extract text from a command output within Ruby, the bartick directive could come in handy.

Syntax is: `command`

One of the useful examples is to retrieve a path of a command:

gcc_path = `which gcc`

There are more applications, I guess I should leave it to you to discover.