Handy-Dandy Terminal Shortcuts

Hey all, here are some handy terminal shortcuts I’ve stumbled upon in school and work. Maybe they’ll make your life easier as you go about entering commands in a Unix bash shell.

The + sign is not part of the key combination, it’s just to show what buttons to hit together.

CTRL + “u” -> Delete current text in prompt and move cursor to the beginning
CTRL + “k” -> Similar to the last, but instead it kills the input
CTRL + “y” -> Return the last killed input
CTRL + “a” -> Jump cursor to the beginning of your terminal input line
CTRL + “e” -> Jump cursor to the end of your terminal input line
CTRL + “f” -> Move cursor forward one character (similar to right arrow key)
CTRL + “b” -> Move cursor backward one character (similar to left arrow key)
CTRL + “d” -> Backwards Delete (For all you Mac users)
CTRL + “h” -> Forward Delete
CTRL + “r” -> Extremely useful, lets you look up a previous used command. Just press CTRL plus the letter “r” then begin typing the command, the promo will return the last command that used the term.
CTRL + “l” -> Clear screen

I’ll add to this list as I come upon more helpful terminal shortcuts.

Aliases, loosing command identity…

Today, I was looking into my environment setup and realized, aliases are really useful. So for your enjoyment, here is a quick reference into how to setup and utilize the flexibility of aliases in your own Unix setup.

What is alias?

Aliases in any platform is a command that lets you replace one word for another.  Nothing really confusing here. The alias command is:

alias <NAME>=<VALUE>

An example:

alias list='ls –l'

This will replace any instance where you type list with “ls –l.”

Screen Shot 2014-01-16 at 6.52.45 PM

Listing isn’t a big deal, but say you have a really long command, imagine replacing that really long command with one word. Seeing the benefits now?

To get rid of an alias, either close your shell or type the command:

unalias <NAME>

This is great and all, but the alias will only work for the current shell it was called in. As soon as I close my shell, my list alias will disappear. What if I want an alias to be a permanent fixture. I want every shell I open to have my list alias. How do I do this? Well, the bash configuration files will help us here.

Bash Files for Unix Systems

When you login to a Unix system over bash shell, ~/.bash_profile is read. From this file, the shell gathers settings for that particular bash shell. If by chance ~/.bash_profile is corrupted or does not exist, ~/.profile is read instead. Say you are already logged into a Unix environment and you open a shell, instead of ~/.bash_profile being read, ~/.bashrc is instead reviewed for shell settings.

So ~/.bash_profile is read by a login shell and ~/.bashrc is read the other shell. I hate being repetitive. I want my aliases in one spot, I don’t want to have to manage two sets.

Well, there is a really easy fix for this problem! Ensure your ~/.bash_profile invokes ~/.bashrc! Check to see if your ~/.bash_profile has the following lines of code that perform the operation or just add them yourself.

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

Nice, now you can store all your aliases in one place, ~/.bashrc. I just stuck mine in at the bottom of the file.

Example Aliases

Sudo reboot every time:

alias reboot='sudo reboot'

Sudo update application package manager:

alias update='sudo apt-get upgrade'

List directories in color:

alias ls='ls --color=auto'

Forget vi, always use vim:

alias vi='vim'

Show open ports:

alias ports='netstat -tulanp'

Directory traversals made easy:

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

Go to your web directory:

alias www='cd /var/www/html'

Grep with color:

alias grep='grep --color=auto'

Remove recursively by force:

alias rm='rm –rf'

The possibilities are endless! Have fun!