Wednesday, July 9, 2014

Useful Mac Terminal Commands

Here are some terminal commands which I wish I knew earlier.

pushd and popd are used to store and change directories.
A trick is by using pushd without any parameter, you can
move back and forth between two folders.

a short example
pushd ~/data
pushd ~/analysis
pushd # now the directory is changed to ~/data
pushd # now the directory is changed to ~/analysis

2.clean the screen
This is same as clear command but I prefer to type 2 keystrokes vs 6 keystrokes
In addition, it also works in the console of Rstudio where clear does not.

control + L 

3.screen for ssh connection
When executing a program in a remote computer by ssh,
if the connection lost, the execution will be aborted,
screen command can solve this perfectly.

4.alias for frequently used commands
alias ep="nano ~/.bash_profile"
alias sp="source ~/.bash_profile"
alias cdr="cd ~/Documents/repo/"
alias cdd="cd ~/Dropbox/"
alias ll="ls -Gl"
alias play="~/apps/spotify play"
alias pause="~/apps/spotify pause"
alias n="~/apps/spotify next"
alias p="~/apps/spotify prev"

5.move to the beginning or the end of the line
Control + a # to the beginning
Control + e # to the end 

6.Execute last command as root - sudo !!
A short example - forget to add sudo when install ruby gem
$ gem install gnuplot
Fetching: gnuplot-2.6.2.gem (100%)

Use sudo !! as a shortcut for sudo gem install gnuplot

$ sudo !!
sudo gem install gnuplot
Fetching: gnuplot-2.6.2.gem (100%)
Successfully installed gnuplot-2.6.2
Parsing documentation for gnuplot-2.6.2
Installing ri documentation for gnuplot-2.6.2

6.Open current directory in Finder from command line

open .

7.Edit bash_profile to enhance the functionality of shell

# change the style and color of the prompt 
PS1='\e[0;32m\u: \e[0;31m\w \e[0;30m'

# add autocomplete for git
   source ~/.git-completion.bash

8.Name the tab

# add a function to bash_profile 
# nt for nametab
function nt {
        echo -ne "\033]1;$1\007"