Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

open or cd to the directory of a file path

# open directory of a file path
# can be used together with the [esc-.] key sequence
function odf() { /usr/bin/open "$(/usr/bin/dirname "$@")"; return 0; }

# cd to the directory of a file path
function cdf() { cd "$(/usr/bin/dirname "$@")"; return 0; }


ls -l /Library/Desktop\ Pictures/Nature/Tranquil\ Surface.jpg
ls -l /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns 

odf [esc-.]
cdf [esc-.]


odf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/
cdf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings

Two shortcuts for displaying system_profiler data types

man system_profiler

function sptypes() {
   /usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/sort | /usr/bin/nl
   return 0
}

unset -f sptype
function sptype() {
   declare -i num
   declare sptypename
   num=$1
   #sptypename="$(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d'  | /usr/bin/sort | /usr/bin/nl | /usr/bin/egrep "^[[:space:]]+${num}[[:space:]]" | /usr/bin/awk '{print $NF}')"
   sptypename="$(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/sort | /usr/bin/nl | /usr/bin/grep -w "${num}" | /usr/bin/awk '{print $NF}')"
   printf "\n\e[1m%s\e[m\n\n" "${sptypename}:"
   system_profiler "${sptypename}"
   return 0
}


sptypes
sptype 5

sptypes | grep -i pref
sptype 26 | less -R


#------------------------------------------


# interactive version
unset -f sptype
function sptype() {
   declare -a array
   declare -i num
   array=($(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/tr '\n' ' '))
   echo
   printf "%s\n" "${array[@]}" | /usr/bin/nl
   echo
   printf "\n\e[1m%s\e[m" "Please select a number:  "
   read num
   num=$num-1
   echo
   printf "\n\e[1m%s\e[m\n\n" "${array[${num}]}: "
   system_profiler $(echo "${array[${num}]}")
   #system_profiler $(echo "${array[${num}]}") | less -R
   return 0
}

sptype

Tapping the Bash command line history

export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'


help fc
help history
help bind
help set
help shopt
open /usr/share/doc/bash/builtins.pdf


cp -ip ~/.bashrc ~/.bashrc.orig
cp -ip ~/.inputrc ~/.inputrc.orig
cp -ip ~/.bash_history ~/.bash_history.bak

cp -ip ~/.bashrc{,.orig}  # shortcut version
cp -ip ~/.inputrc{,.orig}
cp -ip ~/.bash_history{,.bak}


history
history | head -n 3
history | tail -n 1
history | egrep 5
history | egrep '^[[:space:]]*5'
history | egrep -m 1 '^[[:space:]]*5'
history | egrep '^[[:space:]]*5[[:space:]]+'

fc
fc -e nano
fc -l | nl
fc -ln | sed 's/^[[:space:]]*//'
fc -l 0
fc -l 1 -1
#exec /bin/bash     # ... in case of "fc: history specification out of range"
#source ~/.bash_login
fc -l -1
fc -l -5
fc -l 5 "cd "
fc -l -r 5 "cd "
fc -l -5 | grep cd | awk '{print $1}'
fc -s cd     # execute last cd command
fc -e vim 3
fc -e nano "cd "


set +o history   # disable adding commands to history list
set -o history   # enable adding commands to history list


/bin/cat >> ~/.bashrc <<-'EOF'

bind Space:magic-space     # enable option to expand & edit a command before running it by entering a [space]
#shopt -s histverify     # expand & edit a command before running it by entering [return]
shopt -s cmdhist
shopt -s histappend

# suppress history recording for the specified commands including commands beginning with a space
export HISTIGNORE="&:[ \t]*:ls:[bf]g:history*:clear:exit"

#export HISTCONTROL=ignorespace
#export HISTCONTROL=ignoredups
#export HISTCONTROL=ignoreboth

#PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}"

EOF

source ~/.bashrc


/bin/cat >> ~/.inputrc <<-'EOF'

"\033[3~": delete-char             # get a proper forward delete key in Terminal.app
"\e[A": history-search-backward    # search command history backward with up arrow key
"\e[B": history-search-forward     # search command history forward with down arrow key

#set show-all-if-ambiguous on        # enable single-tab completions
"\t": menu-complete                # enable single-tab completions through a series of completions inline

set mark-symlinked-directories on
set completion-ignore-case on
#set visible-stats on
#set bell-style visible

#$if Bash   # same effect as "bind Space:magic-space" in ~/.bashrc
#  Space: magic-space
#$endif

EOF


exec /bin/bash    # reinitialize ~/.inputrc
source ~/.bash_login ~/.bashrc


help bind
bind -l | less
bind -P | less
bind -p | less    
bind -V | less
bind -v | less

bind -p | egrep '^"' | egrep -v 'do-lowercase-version|self-insert' | nl
bind -p | egrep '^"\\C' | egrep -v 'do-lowercase-version|self-insert' | nl
bind -p | egrep '^"\\M' | egrep -v 'do-lowercase-version|self-insert' | nl
bind -p | egrep '^"\\e' | egrep -v 'do-lowercase-version|self-insert' | grep -i complete | nl


# insert the last word of the previous command line
echo hello world1
echo hello world1 and world2
echo hello world1 and world2 and world3
echo hello
echo

[esc-.]     # press the esc-. key sequence repeatedly to iterate through the history list
[ctrl-u]    # clear the current command line
echo !$
printf "%s\n" $_


!1[space]            # expand the first command in the history list without executing
!12[space]           # expand command no. 12
sudo !![space]       # expand last command
!-4[space]           # expand the fourth last command
!cd[space]           # expand the last cd command
!?world?[space]      # expand the last command containing the specified word
!$[space]            # last word of the preceding command line
!:1[space]           # first argument of the preceding command
!-1:1[space]         # same
!-1:1-[space]        # all arguments of the previous command except the last on
!-1:1*[space]        # all arguments of the previous command
!-1:1-$[space]       # same
!-2:3[space]         # third argument from the command before the last one

abc def !#[space]     # double the entire command line typed so far

cd ~/Desktop
!cd:p
!cd[space]
[ctrl-u]
!?cd?:p                 # make the last cd command the last command in the history list and print it without executing
history | tail -n 1

cd[up_key]    
cp[up_key]              # hit the up & down arrow keys again and again to iterate through the commands
cp[down_key]           

[ctrl-r]cp              # press ctrl-r repeatedly to iterate through the previous commands
[esc] or [ctrl-j]       # quit searching with ctrl-r
[ctrl-k] + [ctrl-u]     # quit searching with ctrl-r and clear the command line
[ctrl-y]                # recalls the last string removed by ctrl-k or ctrl-u


# cf. http://nubyonrails.com/articles/2007/05/26/useful-shell-shortcuts
cp foo !#^.bak[space]            # !#^ refers to the first word after the command
cp -i foo !#^.bak[space]    
cp -p -i foo !#^.bak[space]    
cp !#$.bak[space] 
cp foo !#$.bak[space]            # !#$ refers to the preceding word
cp -i foo !#$.bak[space]    
echo abc def !#$.bak[space]
echo abc def !#$.bak ghi jkl !#$.foo mno[space]


# tab completion

# see above:
# set show-all-if-ambiguous on        # enable single-tab completions
# "\t": menu-complete                # enable single-tab completions through a series of completions inline

#[tab]  # all available commands
#[esc][esc]   # alternative
#ds[tab]  # all available commands beginning with ds
#cd ~/Desktop
#cd ~/Des[tab]
#$[tab]  # all set system variables
#~[tab]  # users
#~[esc][esc]   # alternative
#*[tab] 
#=[tab]
#/[tab]

# disable completion for a particular command
#help complete
#complete -r cd[tab]
#cd[tab]