Skip to content

Instantly share code, notes, and snippets.

@jpablobr
Created March 15, 2011 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpablobr/871755 to your computer and use it in GitHub Desktop.
Save jpablobr/871755 to your computer and use it in GitHub Desktop.
# 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment