Bash Magic

Ctrl + A 	Go to the beginning of the line you are currently typing on
Ctrl + E 	Go to the end of the line you are currently typing on
Ctrl + L 	Clears the Screen, similar to the clear command
Ctrl + U 	Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H 	Same as backspace
Ctrl + R 	Let's you search through previously used commands
Ctrl + C 	Kill whatever you are running
Ctrl + D 	Exit the current shell
Ctrl + Z 	Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W 	Delete the word before the cursor
Ctrl + K 	Clear the line after the cursor
Ctrl + T 	Swap the last two characters before the cursor
Esc + T 	Swap the last two words before the cursor
Alt + F 	Move cursor forward one word on the current line
Alt + B 	Move cursor backward one word on the current line
Tab 	        Auto-complete files and folder names

 

Contents

  • 1 !! (Full contents of previous command)
  • 2 !$ (Last arg of previous command)
  • 3 Sets
  • 4 OSX specific
  • 5 References

!! (Full contents of previous command)

How many times have you tried to edit a file, then realize that you need to be root to do it? Use bang-bang to quickly repeat the previous command, with other commands before or after.

vi /etc/init.d/mongrel_cluster
=> Permission Denied
sudo !!
=> Now opens the file as root

!$ (Last arg of previous command)

Sometimes I need to reuse the last argument with another command, like here where I forgot to quote a string.

wget http://weather.yahooapis.com/forecastrss?p=98117
=> wget: No Match
wget '!$'
=> Now it works

Sets

{a,b} (A set)

How often do you rename just part of a file? The {} syntax is convenient.

mv file.{txt,xml}
=> Expands to 'mv file.txt file.xml'
mv file{,.orig}
=> Expands to 'mv file file.orig'
mkdir foo{1,2,3}
=> Expands to 'mkdir foo1 foo2 foo3'

OSX specific

pbcopy and pbpaste

In Mac OS X, you can copy things to the clipboard and read them back out. This is nice because you can reuse it in the shell or back in the OS with Apple-C or Apple-V.

./generate_random_password | pbcopy
pbpaste > file.txt

References

http://nubyonrails.com/articles/useful-shell-shortcuts

http://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/

This entry was posted in Bash, Linux, Mac OS X, Software. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.