Thursday, June 28, 2007

Turbocharge Your Command Line

I enjoy modern Java IDEs and most graphical programs.

But you will have to pry my shell, command-line windows from my cold, dead hands. I use the shell mostly for flying around directory structures and using find/grep in combination.

No Powershell for me, thanks. Just give me Bash aliases -- especially via CygWin's Bash on Windows.

Here are some favorite tricks... IMHO they can really improve productivity for navigating directory structures and copying files across parallel structures. (All of these aliases go into my .bashrc in my $HOME directory)

Moving up N directories

Have you ever been deep down in a directory structure and just want to get out of there? e.g. Something like this ( 7u = "7 up" )

$ pwd
/measter/bin/this/is/a/dir/structure/that/is/so/deep/im/scared
$ 7u
/measter/bin/this/is/a/dir

Here's how:
alias i='echo ; ls ; echo ; echo ; pwd '
alias 2u='cd ../.. ; i'
alias 3u='cd ../../.. ; i'
alias 4u='cd ../../../.. ; i'
alias 5u='cd ../../../../.. ; i'
alias 6u='cd ../../../../../.. ; i'
alias 7u='cd ../../../../../../.. ; i'
alias 8u='cd ../../../../../../../.. ; i'

Dynamically assigning directories

How about assigning directories to a local "clipboard" (CB) variable for later use?
In this case, 'cb' assigns the directory and 'gcb' = 'go to the CB directory'.
$ pwd
/measter/bin/this/is/a/dir/structure/that/is/so/deep/im/scared
$ cb
CB = /measter/bin/this/is/a/dir/structure/that/is/so/deep/im/scared
$ cd /measter ; pwd
/measter
.... do some more work ...
$ gcb ; pwd
/measter/bin/this/is/a/dir/structure/that/is/so/deep/im/scared
Here's how:
# show clipboard vars

alias showcb='echo CB is $CB'
alias showcb1='echo CB1 is $CB1'
alias showcb2='echo CB2 is $CB2'
alias cbs='showcb ; showcb1 ; showcb2'

# assign clipboard directories

alias cb='export CB=$PWD ; cbs'
alias cb1='export CB1=$PWD ; cbs'
alias cb2='export CB2=$PWD ; cbs'

# change to clipboard directories

alias gcb='cd $CB ; cbs'
alias gcb1='cd $CB1 ; cbs'
alias gcb2='cd $CB2 ; cbs'
Publishing dynamic directories to another shell

Imagine that we have assigned 2 directories, CB and CB1, in Shell A using the above tricks. Now we need those directories over in Shell B. Something like:
# from shell A
$ cbs ; # display the CBs
CB=/measter
CB1=/measter/bin/this/is/another/dir
$ putcbs ; # publish the CBs
# from shell B
$ getcbs ; # get published CBs
$ cbs ; # show CBs
CB=/measter
CB1=/measter/bin/this/is/another/dir

Here's one way to do it:
# placed on multiple lines for readability
alias putcbs=' rm /tmp/cbs_list ; touch /tmp/cbs_list ;
echo "export CB="$CB >> /tmp/cbs_list ;
echo "export CB1="$CB1 >> /tmp/cbs_list ;
echo "export CB2="$CB2 >> /tmp/cbs_list '

alias getcbs='. /tmp/cbs_list ; cbs '

The Gist

Use these and other ideas with aliases to save time and energy on your command-line:
  • Increase the # of dynamic CBs to as many as you like. I use 3
  • Use alias for common places in your project. E.g. gpro = 'cd /measter/project/home ; i'
  • Always use the 'i' alias (defined above) for your "goto dir" aliases. Saves typing 'ls'
  • Even shortening something with acc = ' ant clean compile ' can save precious millis.
What are your tricks?

1 comment:

Anonymous said...

Don't do this on a multi-user machine: "getcbs='. /tmp/cbs_list ; cbs '"

I'll happy do a "echo -- rm -rf > /tmp/cbs_list", else.

At least check the ownership of the tmp file.