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!)

List & delete Safari history

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari history
function lsh() {
   /usr/libexec/PlistBuddy -c Print ~/Library/Safari/History.plist | \
      /usr/bin/awk '/^[[:space:]]+= /{sub( /^[[:space:]]+=[[:space:]]+/,""); print}' | \
      sed -E -n -e 's/^(.{1,100}).*$/\1/' -e '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sort -u | /usr/bin/nl
   return 0
}

lsh


# delete Safari history except ...
# ... for those domains specified by an egrep regex

function dshe() {

   declare array_indices historyplistfile index regex

   regex="${1:-$'\777'}"   # delete entire history if there is no argument $1; for \777 see man ruby
   historyplistfile="${HOME}/Library/Safari/History.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${historyplistfile}" | /usr/bin/awk '/^[[:space:]]+= /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}'" "${historyplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}':title" "${historyplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :WebHistoryDates:'${index}'" "${historyplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${historyplistfile}" &>/dev/null

   return 0

}


lsh
dshe unix
lsh
dshe
lsh

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]