Pages

Showing posts with label mac. Show all posts
Showing posts with label mac. Show all posts

Thursday, August 30, 2012

Add Git Branch to Bash prompt and a git goodie - SourceTree

Adding git branch to your bash prompt has become easier than before.

Edit your ~/.bash_profile to add following code

# Set git autocompletion 
if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
  . /usr/local/git/contrib/completion/git-completion.bash
fi

GIT_PS1_SHOWDIRTYSTATE=true

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi

PS1='\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

It not only adds branch name but also indicates the current status of your project. For example, when you add files to staging area, + is added after branch name. 
If you make further changes to staged files, it adds * after branch name to indicate there are unstaged files.

The above script also enables Git autocompletion which will help saving some keystrokes.

If you prefer GUI tool and even if you prefer command line, SourceTree is a must have tool for Mac users. It shows you all branches, their commits, the changes. You can do everything using it.


For newbies, it will make using git a lot more fun!

Monday, July 30, 2012

How to Install Ruby On Rails on Mac - Updated

This post will guide you to prepare your Mac OS to install Ruby on Rails using rvm. It will also show you how to create a sample rails app to enjoy all the efforts involved in installation.

There is a good news and a bad news. First the good news. Ruby comes preinstalled on all Mac's.
To check which version you have, open up Terminal and type
$ ruby -v
The most current version as of July 2012 is 1.9.3.
Now the bad news, the version you have may not be the version you want and it's a pain to manage different ruby versions manually. Ruby Version Manager (rvm) does great job so it carry the burden of installing and managing different versions.

Step 0. Install git and gcc 

To check if you already have git, inside Terminal run this command
$ git --version
If you don't have git installed, download the recent installer from
http://code.google.com/p/git-osx-installer/downloads/list?can=3
Open the .dmg file and you will find the installer package. Follow the instructions to install git.

Ruby is compiled locally using gcc, hence make sure you have gcc installed.
$ gcc --version
Gcc does not come preinstalled on Mac. You need a developer account to install it separately.
  • If you are running Lion, login using Apple ID and download Apple's official Command Line Tools for Xcode. Simply double click .dmg file and then the enclosed .pkg file to start the installation process. 
  • If you on Snow Leopard, you can download Xcode (4 GB in size) and install it, which includes gcc. The easier option would be to download the unofficial Gcc compiler and related tools from osx-gcc compiler downloads page.
Step 1. Install RVM

Open up Terminal and execute this command -
$  curl -L https://get.rvm.io | bash -s stable
It downloads and runs RVM installation script.

If curl complains about certificates, try with -k option
$ curl -kL https://get.rvm.io | bash -s stable
If you run into other issues, make sure you have git installed. Don't worry, it's easy to setup.

Once rvm installtion is completed it shows the files it modified. rvm is now ready to be used. Close the current Terminal window and open a new one. Test rvm using
 $ rvm --version
You should see a version number with no errors. In case it was not recognized as a command you will have to load the rvm script into the shell so that shell can recognize 'rvm' as a valid command. You do that by executing following command verbatim.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile

Step 2. Install Ruby using rvm
We will install 1.9.3 as part of this tutorial. To get a feel of what rvm is capable of installing, run this command
 $ rvm list known
It lists different versions of Ruby packages that can be installed and managed by rvm.
To install ruby use this format rvm install for example, 
 $ rvm install 1.9.3
It downloads the source code for ruby 1.9.3 and then compiles it locally. If there are any errors, rvm will also tell you how to fix them. Once done, it displays "Install - #complete"

To verify, list the installed versions using
 $ rvm list
It should list the installed version like this -

~$ rvm list

rvm rubies

 * ruby-1.9.3-p194 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

and now to double check, verify the ruby version itself using
 $ ruby -v
Now set it as your default ruby version.
 $ rvm --default 1.9.3
Optionally, you might want to generate Ruby core documentation by running
 $ rvm docs generate

Step 3. Install Rails and other Gems
It's simple. Run this command and it will install rails and all other dependencies which work with your ruby version. It also install the docs for al the gems. Takes some time ...
 $ gem install rails
To install SQLite 3, first verify you have sqlite3 installed on your system by typing
 $ sqlite3 -version
It will respond with a long version string. Now install ruby binding for sqlite using
 $ gem install sqlite3

Step 4. Ruby On Rails Test Drive
Now that you have all the required components in place, let's create a your first Ruby on Rails app to make sure everything is working smoothly. Again inside a Terminal window,  navigate to a directory where you want to create your new app. ~/rails-app for example. Run these commands one by one
$ rails new todos
This will create the template application with model, view and controller and some static files.
$ cd todos
Tell rails to generate scema for your todos app database and also generate controller and views for standard actions.
$ rails g scaffold todo name:string completed:boolean
Run database migration
$ rake db:migrate
Then tart the rails WEBrick server on default port 3000
$ rails s
Finally, point your browser to http://localhost:3000/ If you see a welcome page, your app is running fine. Congratulations! Your todos can be managed at http://localhost:3000/todos

When you are done, you can stop the server by typing CTRL+C in the shell window where you started it earlier.

Extra Credit: More rvm commands

To revert to system version of ruby, i.e. pre-rvm ruby use this,
$ rvm system
To go back to 1.9.3 or any other version, use
$ rvm 1.9.3
To get the list of all rvm commands,
$ rvm usage

Hope it helped!

Sunday, February 12, 2012

Sublime Text 2 - I'm loving it!


Leaving Notepad++ was not easy. The recommendation for TextMate was loud and clear. But it didn't impress me and the search continued. There came this new kid on the block - Sublime Text 2. I must say it's really Sublime. If you haven't tried yet, download it now - http://www.sublimetext.com/2 Not just mac users, but my ubuntu friends are also leaving their "vi" and gEdit for Sublime Text 2.

My favorite features -

  • multiple cursors - it's incredibly useful!
  • column select
  • crazy fast file switching - include 'Go to anything' search (cmd + p)
  • context specific search and replace
  • side by side multi-pane editing (like :vsp in vim)
  • auto completion 
  • bookmarks (like in vim)
  • distraction free mode
  • Snippet support 
  • Zen coding support 

And there is a package manager which makes installing new packages a piece of cake.

Terminal command to open up Sublime Text 2
It's simple, just create s symlink and add it to your PATH
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
Edit your ~/.bashrc or ~/.bash_profile to add this -
PATH=$PATH:~/bin
export PATH

Install package manager-

Open Sublime text 2, you can use subl command in your Terminal now.
Open the console by clicking View -> Show Console  and paste this in the console and hit enter. import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
Restart Sublime Text 2 to complete the installation process.
Presse cmd + shift + p to access the command pallet and then type 'Install packages" and hit Enter.
This will open up a list of all available packages. Select the package from the list and Enter. That's it. It will download the package and install it for you.

Here is the list - http://wbond.net/sublime_packages/community

A very quick way to create new folders and file - cmd + alt + n -
Happy Coding!

Print Screen in Mac


When I opened my macbook, the first thing I wanted to do was to take a screenshot and email it to my friend. But there is no Print Screen? First Google result - cmd + shift + 3. Can it be more obscure? 
Sure, it has lot of good functionality built in but a dedicated key or fn+something is more user friendly, isn't it?

Here are all options - 
  • Cmd + shift + 3 -- Takes screenshot of the entire screen and saves it as a .png on the desktop
  • Cmd + shift + 4 -- Lets you select an area and then saves it as a .png on the desktop
  • Cmd + shift + 4 + space -- Lets you take a screenshot of just one window. and saves it as a .png on the desktop

To copy the contents to the clipboard, add control key with above combinations. e.g 
  • Cmd + +ctrl + shift + 3 -- Takes screenshot of the entire screen and saves it to the clipboard
Command Line -
For Terminal lovers, screencapture command can be used and it's especially useful for automated scripts. 
$ screencapture -iW ~/Desktop/screen.jpg

File Format 
Mac OX X 10.4 and later versions use .png as a default file format to save the screenshot but this can be changed by using the below command inside Terminal

$ defaults write com.apple.screencapture type image_format
$ killall SystemUIServer

where image_format can be jpg, tiff, pdf, png, bmp or pict. The second command causes SystemUIServer to restart so that the change takes effect immediately. 

There is also a native app - 
If you don't like so many keys for just a screenshot, you can also use an app called Grab, bundled by Apple by default, to grab a screenshot and view it using Preview application

So much for a simple print-screen!

Moom - Snap for Mac

If you miss the Snap feature of Windows 7 where you can quickly resize any window to occupy half of your screen space, here is a must have app from ManyTricks. It's called Moom - http://manytricks.com/moom/

It does a fantastic job of resizing and aligning multiple windows with simple mouse control. It also has keyboard mappings if you like. I find it extremely useful especially while working with multiple Terminal windows, and also while working on my Website. 

Thursday, November 3, 2011

Spruce up your Terminal

It's very annoying when mouse doesn't work inside Vim in Mac's Terminal and when Cmd + 1-9 doesn't work while switching tabs. With some googling here are simple steps for making these things work.

The first thing you need to install in SIMBL - (SIMple Bundle Loader) - pronounced like "symbol" or "cymbal" - enables hacks and plugins. Download it from http://www.culater.net/software/SIMBL/SIMBL.php Unzip it and use the .pkg installer.

Next step is to get the required plugins. Most of the plugins come with installer but it's easy to do it yourself.  Plugins can be copied to /Library/Application Support/SIMBL/Plugins for all users or ~/Library/Application Support/SIMBL/Plugins for just your account.

Using MouseTerm Plugin -
MouseTerm is a SIMBL plugin for Mac OS X's Terminal.app that passes mouse events to the terminal, allowing you to use mouse shortcuts within applications that support them.
Download .dmg file from https://bitheap.org/mouseterm/
Run Install and restart Terminal app.  To enable mouse for all events inside vim, add this to your .vimrc file
if has("mouse")
   set mouse = a
endif
To check other information and possible values run :help inside vim.

Switching between tabs using cmd + 1-9
cmd + 1-9 lets you switch between different windows, but if you like to work with multiple tabs instead, you can change the behavior of this key combination using TerminalTabSwitching plugin.
Download it here.
Extract it in ~/Library/Application Support/SIMBL/Plugins

Changing bash prompt - 
To customize your bash prompt, you can refer to this post

Adding colors to bash prompt - 
Good explanation on how to add colors to bash prompt can be found here.

Adding git branch to bash prompt - 
This is really helpful, if you work with many branches in git. I have covered this in my earlier post.

Just a quick note, mouse events and tab switching works by default in iTerm 3 and it also supports many more features such as vertical / horizontal splits etc. Give it a shot. 

Wednesday, November 2, 2011

How to install Ruby on Rails on mac

Update: There is an updated version of this article at How to Install Ruby on Rails on Mac - Updated

This is first tutorial in ruby on rails series. This post will show you the exact steps you need to follow to get Ruby on Rails up and running on your Mac.

If you are new to Terminal and Mac os X in general; you will find this helpful.

Since mac gives us shell access, we can take advantage of Linux goodies. The most useful and recommended tool is rvm. It stands for Ruby Version Manager. It's a command-line tool which allows you to easily install, manage, and work with multiple ruby environments including sets of gems. With new versions of ruby getting released periodically, it's nice to have a version manager.

OS X comes with ruby installation by default. Check the version of installed ruby -
 ruby -v
Most likely, you will see ruby 1.8.7 pre-installed for you. Now it's time to upgrade to either 1.9.2 or 1.9.3.

Step 1 - git installation
rvm requires git to be installed on your system. If you already have git, skip to the next step.
git is a popular version control manager used by many open source projects including linux kernel and android. Download the appropriate installer from here
http://code.google.com/p/git-osx-installer/downloads/list?can=3
Open the .dmg file and you will find the installer package. Follow the instructions to install git.
To verify, inside Terminal type git and you should see a list of available git commands.

Step 2 - RVM installation
For GUI lovers, there is GUI version available just for Mac users. Download from here - http://jewelrybox.unfiniti.com/
For rest of of us who love Terminal, follow along. It's one step process. Inside Terminal run this command -
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
It downloads rvm and installs it for you.

Step 3 - Load RVM
This step is analogous to adding rvm to your $PATH or environment variable.
Again inside Terminal,
echo ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && source “$HOME/.rvm/scripts/rvm”‘ >> ~/.bash_profile
It add rvm scripts to your bash profile so that every time you open the terminal, rvm gets loaded in the Terminal.

Step 4 - Verify RVM installation
Restart Terminal. Use cmd + Q to close the application and open it again.
Now it's time to see how rvm looks. Type rvm in Terminal and you will be greeted by a long message.

Step 5 - Download and Install xcode
xcode covers most of the dependencies for new Ruby installation. It also includes gcc compiler and other important development tools. You can download Xcode from Apple’s App Store. Search for “xcode” and click the install button.

Step 6 - Upgrade to Ruby 1.9.2
We will use 1.9.2 for this tutorial as it's the most recent stable version.
Inside Terminal,
$ rvm list known
# MRI Rubies
1.8.6[-p420]
1.8.6-head
1.8.7[-p352]
1.8.7-head
1.9.1-p378
1.9.1[-p431]
1.9.1-head
1.9.2-p180
1.9.2[-p290]
1.9.2-head
ruby-head
...
This is the list of all components known and managed by rvm.

We will install ruby 1.9.2.
$ rvm install 1.9.2
Next, check the installed versions by rvm using
$ rvm list
Now start using 1.9.2 by this command
$ rvm use 1.9.2

Your current ruby version is 1.9.2 but you might want to make this as your default version.
$ rvm --default 1.9.2
Check the version in another Terminal window
$ ruby -v
$ which ruby
You are almost done!

Step 7 - Install gems
Ruby gems are managed by a command line tool called 'gem' and it's preinstalled for you by rvm. You can use it to install other gems such as rails or sinatra etc.
$ gem install rails
rvm chooses gems depending on the current version of ruby and you can switch between different version by using 'rvm use' command.

Step 8 - Create Your rails project (optional)
 cd ~
 mkdir rails-apps
 cd rails-apps
 rails new hello
 cd hello
 rails s
In your browser, open localhost:3000/. You have successfully installed Ruby on Rails on mac :)