public
Created

  • Download Gist
Fine-tuning-history.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
# See also:
# Fine-tuning tab completion in Bash,
# http://codesnippets.joyent.com/posts/show/1690
 
 
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 --login # ... in case of "fc: history specification out of range"
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
 
# if ~/.bashrc is getting sourced from ~/.bash_login
# exec /bin/bash --login
 
 
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]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.