Bash shell shortcuts - mikec964/chelmbigstock GitHub Wiki

Some Mac OS X things

Robin:~(0) mikec$ su admin
Password:
Robin:mikec(0) admin$ sudo visudo
Password:
Robin:mikec(0) admin$ sudo cat /etc/sudoers | grep mikec
mikec	ALL=(ALL) ALL

Shortcuts

  • cd ~ Go to home directory
  • cd # Go to root
  • sudo !! sudo last command
  • <cmd> $! Run on arguments of prior command

Keyboard shortcuts

You'll find more key bindings here.

On a Mac, Alt+ is Esc, then .

Move

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
Alt + f    Move forward a word
Alt + b    Move backward a word

Edit

Ctrl + d   Forward delete
Ctrl + w   Delete the word before the cursor
Ctrl + k   Clear the line after the cursor
Ctrl + u   Clears the line before the cursor position
Alt + .    Yank last argument to previous command
Alt + d    Delete word forward
Alt + t    Transpose previous two words
Alt + c    Capitalize word
Alt + u    Make word uppercase
Alt + l    Make word lowercase

Shortcuts

Ctrl + r   Search through previously used commands
Tab    Auto-complete files and folder names
<command> !$   Applies command to the path of the previous command
    $ ls bin
    ...output...
    $ ls -a !$
    ls -a bin
    ...output...

Control

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.

Info

Alt + ?    Show current completion list
*, Tab, Tab   Show subdirectories, excluding hidden ones
@, Tab, Tab   Show possible hostname completions (/etc/hosts)

Mostly useless

Ctrl + xx   Move between EOL and current cursor position
Ctrl + l   Clears the Screen, similar to the clear command
Ctrl + h   Same as backspace
Ctrl + t   Swap the last two characters before the cursor
Alt + 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

Color prompt

Add this to .bashrc:

echo "original prompt: $PS1"
export PS1="\[\e[37;41;1m\]\h:\W \u$\[\e[0m\] "

Shell Hacks: Bash Colors explains this.

Note for Mac OS

Usually .bashrc is run per terminal window, .bash_profile is run only at login. Mac OS Terminal.app runs .bash_profile for each new terminal window.

The best practice: Put PATH and other settings in .bashrc, then add this to .bash_profile

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

Globbing

ls *c  #all files that end with 'c'
ls *.[ch]  #files that end with c or h
ls *[0-9]*.c  #files that have a digit
ls *ab?de.c  #files with 1 letter between ab and de.

Aliases

You can create your own command shortcuts with aliases. Here are 30 examples. To remove an alias, use unalias.

alias ll='ls -la'
alias ..='cd ..'
alias ping='ping -c 5 -s.2'
alias dfs='bin/hadoop dfs'
alias bashrc="vim ~/.bashrc && source ~/.bashrc
⚠️ **GitHub.com Fallback** ⚠️