Art Of Programming

musings by Dmytrii Nagirniak

Use Your Terminal Like a Pro

Keep your config in a separate dotfiles directory and under source control with the structure like:

--+ bashrc, bash_profile
--+ ssh/config
-->--bash -> (env, config, aliases). bash has source bach/env, bash/config etc.

ln -s ~/.bashrc ~/bin/dotfiles
echo ". ~/.bashrc" > ~/.bash_profile

Put everything into .bashrc except any long running tasks.
.bash_profile - for interactive shells. But you can just usually source .bashrc.
Now some keyboard shortcuts:

  • Cntrl-a - go to the beginning of line
  • Cntrl-e - go to the end of line
  • Cntrl-k - delete everything to the right
  • Cntrl-w - delete previous word
  • ESC->-b - go back one word
  • ESC->-f - go forward one word

If you often connect via SSH like so: ssh -p 2323 Then you can shorten it to ssh my-server if you will add entry to ~/.ssh/config:

Host my-server
    Port 2323
    User username
Then you can use it as:

# Tunnelling
ssh -L7997: my-server

# Copy folder to server
scp my_folder my-server:my_other_folder/

# Or even use it with git
git clone my-server:repo-name.git

Now some useful things you can do in the terminal:
  • !! - run previously executed command (you can do `sudo !!` for example)
  • !$ - last argument of previous command
  • !60 - run the command #60. Number comes from history command
  • (for example: run tail file.log and then cat !$ is same as cat file.log
  • !echo - rerun last command starting with echo
  • Ctrl-r - interactive search on history of commands; Use Ctrl-j to abandon.
  • !?file.log?! - run the last matching command (not interactive)
  • ^file.log^another-file.log - run the last matching command replacing file.log with another-file.log
  • !?file.log?! - run the last matching command (not interactive)
  • mv README.{txt,markdown} - same as mv README.txt README.markdown.

You will also appreciate power of the shell when using aliases and functions:

alias ss="./script/server"
alias s="git status"
alias gca="git commit -am"
alias zipr="zip -r"

function take {
  mkdir $1
  cd $1
# take create-and-go-here

function gg() {
  git commit -v -a -m "$*"
# gg Commit message with no quotes

But when functions get more complicated, you might want to use a scripting language (Ruby, Python etc).
Put those scripts into your ~/bin directory and add it to your PATH variable.

There are also number of Mac specific tricks.
One is is open anything command that will open the file/directory in the default application.
You can also force it to use a specific app: open -a /Applications/ index.html.
This one becomes a good candiate for an alias.

You can also pipe output of any command into pbcopy command to copy it to the clipboard.
For example: cat index.html | pbcopy will copy the content of the file into clipboard.

You can also do it the other way around: pbpaste > index.html or pbpaste | grep "jquery".

Another useful thing OpenTerminal (drag the app to finder toolbar). It allows you to open terminal with the current directory set to the Finder window.

You can also use the keyboard to open terminal using LaunchBar. It does much more though. It acts like a global terminal for the system.

You can also try to use zsh shell. It will work with the existing bash aliases just fine.

Most of the tips here come from the PeepCode’s Advanced Command Line screencast.
Hope this helps you on your way to becoming a Terminal Guru.