tpai
2/1/2015 - 12:11 PM

my mac usage guide

my mac usage guide

Shortcuts

u                   # undo
Ctrl-r              # redo
>                   # indent
gc                  # comment / uncomment
,ag                 # find file grep keyword
,t                  # fuzzy file selector
,b                  # buffer selector better be a file you've opened
R                   # refresh NERDTree
:%s/search/replace/g# search and replace
//                  # clear the search
%                   # jump to closing tag
:Gblame             # show git blame of current file
:set ff=unix        # convert line ending to a same format
:e ++ff=dos         # convert line ending to a same format

Ref: Convert DOS line endings to Linux line endings in vim

Cheatsheet

/* Pane */

Ctl-a %|v             # vertical split
Ctl-a "|s             # horizontal split
Ctl-a x               # kill
Ctl-h,l,j,k           # focus
Ctl-a o               # next
Ctl-a Shift-h,j,k,l   # resize
Ctl-a PgUp|PgDown     # scroll up|down

/* Window */

Ctl-a c               # new
Ctl-a n               # next
Ctl-a p               # prev
Ctl-a &               # kill

/* Session */

Ctl-a d               # detach
tmux list-sessions
tmux new -s <id>
tmux attach -t <id>
tmux kill-session -t <id>

.agignore

For ag silver searcher to ignore files.

bundle.js
vendor.js
*.min.js
*.min.css

For loop

#!bin/bash

lang=en,jp,kr,th
arr=(${lang//,/})
length=$(echo ${#arr[@]}-1|bc)

for ((i=$length; i>=0; i--)); do
    echo ${arr[$i]}
done

for i in "${arr[@]}"; do
    echo $i
done

Check if AWS cli exists

if ! which aws &> /dev/null; then
    curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
    unzip awscli-bundle.zip
    sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
fi

Check if jq exists

if ! which jq &> /dev/null; then
    echo "> Installing jq..."
    if [ "$(uname)" == "Darwin" ]; then
        brew install jq
    elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
        wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
        chmod +x ./jq
        cp jq /usr/local/bin
    fi
    echo "> Installed"
fi

Add super user(Ubuntu)

adduser [username]
usermod -aG sudo [username]

Disk Usage by directory

du -d [depth] [directory]

-h human readable
-g giga
-m mega
-k kilo

Query more information than nslookup

dig +noall +answer [domain]

Split string with '/' and pick field 2 and 3

# output: 2/3
echo "1/2/3" | cut -d'/' -f2,3

replace '/' with '\n'

# output: 1
2
3
echo "1/2/3" | sed 's/\//\n/g'

Split with tab or colon

# output: 2
echo "1   2   3" | awk '{print $2;}'
# output: 3
echo "1:2:3" | awk -F ":" '{print $NF}'

Disable ZSH for abandoning overwrite

setopt clobber

Kick user off your system

who -u
kill -9 [uid]

Ref: How do you kick a benign user off your system?

Get name from system

id -un

Make a link

ln -nfs <src> <dest> # soft (file or directory)
ln <src> <dest> # hard (file only)

Ref: http://stackoverflow.com/a/9587490/1924678

Who's listening this port?

# mac
lsof -Pni4 | grep LISTEN

# linux
netstat -plntu

Merged branches exclude master

git branch --merged master | grep -v master

Find files which contains 'text'

grep -rnw /path/to/somewhere/ -e <text>
// e.g. grep --exclude-dir={node_modules,tests,output,www,.git} --include=\*.{js,jsx} -rnw "." -e 'React'

Ref: http://stackoverflow.com/a/16957078/1924678

Mongo CLI

mongo <dbname> --eval "printjson(db.getUsers())"
mongo <dbname> --eval "db.auth({user: <username>, pwd: <password>})"
mongo <dbname> --eval "db.dropAllUsers()"
mongo <dbname> --eval "db.createUser({user: <username>, pwd: <password>, roles: ['readWrite', 'dbAdmin']})"

SSH Without Password

ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub | ssh user@host "cat - >> ~/.ssh/authorized_keys"
user@host> chmod 0700 ~/.ssh
user@host> chmod 0600 ~/.ssh/authorized_keys
ssh user@host

Change Default Shell

chsh -s /bin/{bash|zsh}

Port Forwarding

echo "rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 3000" | sudo pfctl -ef -

Install Specific Version of Yarn CLI Manually

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.5.1

Make Git From Source

apt-get update
apt-get install build-essential libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
git clone git://git.kernel.org/pub/scm/git/git.git
cd git
git checkout v2.14.1
make prefix=/usr/local all
sudo make prefix=/usr/local install

Make Python From Source With SSL

apt-get install openssl libssl-dev
wget --no-check-certificate https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
tar -xzf Python-2.7.11.tgz
cd Python-2.7.11
./configure  
sudo make && make install 

Software List

  • Required

    • fork (git with UI)
    • keka (unzip)
    • magnet (window management)
    • alfred (quick launch)
  • Optional

    • obs & wavtap (livestream)
    • vysor (control android phone on desktop)
    • aseprite (pixel painting)
    • duet (use ipad as extend monitor)
    • giphy capture (generate gif)
    • lumen (auto-adjust brightness)
    • dr. cleaner (clean your mac)
    • boxer (dos game emulator)
    • openemu (gba emulator)

Windows 10 Installation With Boot Camp 6

  1. Install Windows Support Software into your USB drive.
  2. Allocate bootcamp partition and install windows 10.
  3. Use WinClone to backup system as iso image if need to expand the partition size.
  4. Use Disk Utilities to reallocate bootcamp partition.
  5. Restore backup system using WinClone.

Ref: Install Windows Without USB or Flash drive

Key Settings

Key Repeat Rate & No Combination With function keys

Short Cut For Typing

Keyboard Function Adjustment

Karabiner-Elements handle three features below:

**Replace Capslock With Esc**

[[Seil](https://pqrs.org/osx/karabiner/seil.html)]

Escape key means a lot to vim user, but the sad part is that escape key is pretty small and far from hand. Also capslock is the key which not using a lot, so it's make sense to replace it with escape key.

* Change the capslock key: 51 => 53

**Exchange Command/Option Key**

[[Karabiner](https://pqrs.org/osx/karabiner/)]

This trick is useful for mac user want to use external keyboard. Just need to exchange option key with command key, and also the opposite.

Check all these options:

* Change Command_L to Option_L: 55 => 58
* Change Command_R to Option R: 54 => 61
* Change Option_L to Command_L: 58 => 55
* Change Option_R to Command_R: 61 => 54

**Map Function Keys**

When using external keyboard for mac device, you must map function keys so that you can perfectly trigger these function.

1. Change F1-F19 key & functional key
2. For PC Keyboards
3. Check the first option with "You can use standard function key fn-f1.....fn-f12".

Package Manager for OS X - Homebrew | Cask

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap caskroom/cask

Refs:

Install vim-devicons for NERDTree

Clone And Install Nerd Font

git clone https://github.com/ryanoasis/nerd-fonts.git
./install.sh DroidSansMono

Install with vim-plug

Plug "ryanoasis/vim-devicons"
vim +PlugInstall

.vimrc

set guifont=Droid\ Sans\ Mono\ for\ Powerline\ Plus\ Nerd\ File\ Types:h18
set encoding=utf8

Next, Change iTerm Font Setting.

And done!

Troubleshoot

Rename Laptop

Preferences > Sharing > Computer Name

Refs: http://blog.e-happy.com.tw/?p=2318

Default Font Size of iTerm2

Preferences > Profiles > Text > Regular Font & Non ASCII Font > 18px

Turn Off System Bell Of iTerm

Preferences > Profile > Terminal

Refs: http://archive.robwilkerson.org/2009/07/27/disable-the-system-bell-in-iterm/

Tab Autocomplete Not Working

rm -f ~/.zcompdump; compinit

Refs: https://github.com/skwp/dotfiles/issues/572#issuecomment-121677625

Run Chrome Browser With EN-US Interface In Mac

Can't change locale directly before chrome 49, so the only option to do this is type the command below in terminal.

defaults write com.google.Chrome AppleLanguages '(en-US)'

Git Remote Rejected (pre-receive hook declined)

The branch has been protected by git system, please change setting that allow developer push.

Ref: git-push-error-pre-receive-hook-declined

Sudo Command Not Found

alias sudo=”sudo env PATH=$PATH”

Ref: http://pickerwengs.blogspot.tw/2011/10/sudo-command-not-found.html

__git_ps1 Not Found

Because of 'sudo su' won't source the user's .bashrc, so need to simulate a login by executing 'sudo su -l'.

Ref: http://stackoverflow.com/a/11668766/1924678

Fix xcode-select path

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

Update Ruby Of MacOS Yosemite

\curl -sSL https://get.rvm.io | bash -s stable
rvm list known
rvm install ruby-2.4.1

Ref: https://stackoverflow.com/a/38194139

Awesome Tools

Cheatsheet