Tag Archive | linux

Custom bash auto-completion using Ruby

Using what I showed you in the how to run Ruby code from within a bash script post, I will follow that up with an easy way to create a custom bash auto-completion function.

The Scenario…

I like to keep all my code and projects in my ~/Development directory. I typically create an alias which takes me to this path, and depending what projects I’m working on, I’ll add/remove aliases accordingly to get to the relevant projects quickly. But this is quite manual and cumbersome, and it requires constant maintenance. What I’d like is an auto-complete function that dynamically picks up any new directory I create in the ~/Development directory.

All the functions detailed below should either go into your .bashrc file, or a file that is sourced from your .bashrc file.

Get rid of that alias

The first thing to do is change my alias to a function. So, instead of this…

alias d="cd ~/Development"

…we want this:

function d { cd ~/Development/$1; }

Create the list of choices

Next, we need a function which returns a list of all the directories in the ~/Development directory, which will serve as our auto-completion choices. We can use Ruby here to make things nice and clean…

#!/usr/bin/env sh

/usr/bin/env ruby <<-EORUBY

class ProjectCompletion
  def initialize(command)
    @command = command
  end
  
  def matches
    projects.select do |task|
      task[0, typed.length] == typed
    end
  end
  
  def typed
    @command[/\s(.+?)$/, 1] || ''
  end
  
  def projects
    %x[ls ~/Development].split
  end
end

puts ProjectCompletion.new(ENV["COMP_LINE"]).matches

EORUBY

Bootstrap it!

Finally, all we need is to tell bash to use this script. We do that with this command (your own paths may vary of course):

complete -C ~/.dotfiles/completion_scripts/project_completion -o default d

Reload your shell and voila! You now have bash auto-completion for all your coding projects. This is very easily customised too, so you can create auto-complete scripts for all kinds of other things, locations, tasks etc.

You can now easily reach your coding projects using auto-complete, without the need for aliases. For example, if you had a directory structure like this…

~/Development
~/Development/project_1
~/Development/project_2

…you could easily see a list of projects doing this:

d proj<tab>

For more information see my dotfiles project.

Hope this helps.

Setting up Ubuntu 11.10 for Rails development… Just Right!

Further to my previous post on how to setup Ubuntu 11.10 for Rails development, I have now created a bash installer script to automate the whole laborious process for your pleasure… well it’s for my own pleasure really, but I like to share!

There are two installer scripts:

  1. Ubuntu user will setup your Ubuntu 11.10 environment will all the usual goodies you normally should install after a clean install / upgrade.
  2. Rails developer will setup your Ruby environment using RVM, along with all the supporting things that you will need to get up and running in no time.

The installers are updated regularly so check the git pages for full details, but some of the key things that are installed are…

The Ubuntu user installer will hook you up with these (among others)…

  • Medibuntu repositories
  • Ubuntu Restricted Extras
  • Configuration Editor (dconf-tools)
  • Compiz Config Settings Manager
  • Synaptic
  • Terminator
  • GNOME Tweak Tool
  • VLC
  • Jupiter
  • Caffeine
  • Web apps and Sushi file previewer
  • Simple LightDM Manager
  • Sysmonitor App Indicator
  • Dropbox
  • OpenJDK (Java 7) – optional

Some of the things the Rails developer installer will setup are…

  • SSH key – optional
  • Essential Ubuntu libraries
  • Ruby 1.8.7 from Ubuntu repostories with additional libraries
  • Git and Git Gui
  • MySQL 5.1 with admin GUI tools
  • MongoDB 2.0.1 – optional
  • ack
  • ImageMagick
  • RVM (Ruby Version Manager) with these rubies: 1.8.7 stable and 1.9.2 stable

Head over to the git repository for instructions on how to run the installers and also for more details about what it actually installs:

https://github.com/edjames/just_right

Simples.

Append the contents of one file to another

I often have to append a file to another file, usually when I’m adding a new key to a server’s authorized_keys file. So, here’s an example…

small_file.txt contains the following text:

my secret key

large_file.txt contains the following text:

some other secret key

To append the contents of small_file.txt to large_file.txt run the following command:

cat small_file.txt >> large_file.txt

That will result in the contents of large_file.txt looking as follows:

some other secret key
my secret key

Power-up your bash file

I have made my life considerably easier by adding some of my most-used bash commands to by bash_aliases file. Just remember to load this file in your bashrc file and you’re all set…

alias dev="cd ~/Development"

alias ssh_g="ssh user@10.193.152.32"

# console coloring
export PS1='\[\033[1;35m\]\h\[\033[1;33m\] \W\[\033[00m\] => '

# root file browser
alias ste="sudo gedit"
alias sn="sudo nautilus"

# edit bashrc
alias bn="nano ~/.bashrc"
alias bna="nano ~/.bash_aliases"

# edit hosts
alias hosts="sudo nano /etc/hosts"

# free memory
alias free="free -m"

# system helpers
alias update="sudo aptitude update"
alias install="sudo aptitude install"
alias upgrade="sudo aptitude update && sudo aptitude safe-upgrade"
alias remove="sudo aptitude remove"
alias clean="sudo aptitude clean"
alias search="sudo aptitude search"

# reload bash aliases
alias reload="source ~/.bashrc"

# git helpers
alias gu="git pull"
alias gp="git push"
alias ga="git add ."
alias gc="git commit -m \$1"
alias gs="git status"
alias gi="nano .gitignore"
alias gg="git gui &"
alias gk="gitk &"

alias gu_all="sso && gu && rd && gu && rt & gu & ws & gu"

# git config (globally)
alias ggmyname="git config --global user.name \$1"
alias ggmyemail="git config --global user.email \$1"

# git config (locally)
alias gmyname="git config user.name \$1"
alias gmyemail="git config user.email \$1"

# test cucumber features
alias ct="cucumber features -n"

# start ruby
alias rs="ruby script/server"
alias rc="ruby script/console"

# disk space and cls/clear
alias left="df -h"
alias cls="clear"

# flush dns cache
alias flushdns="sudo /etc/init.d/nscd restart"
alias installdnscache="sudo aptitude install nscd"

# logs
alias ld="tail -f log/development.log"
alias lp="tail -f log/production.log"
alias lt="tail -f log/test.log"
alias lc="tail -f log/cucumber.log"
alias lr="rm log/*.log"

# apache
alias ame="sudo a2enmod \$1"
alias amd="sudo a2dismod \$1"
alias ase="sudo a2ensite \$1"
alias asd="sudo a2dissite \$1"
alias arc="sudo /etc/init.d/apache2 reload"
alias arg="sudo apache2ctl graceful"
alias ars="sudo /etc/init.d/apache2 restart"

# capistrano
alias capsetup="cap deploy:setup"
alias capcheck="cap deploy:check"
alias capcold="cap deploy:cold"
alias capstop="cap deploy:stop"
alias capstart="cap deploy:start"

# rake
alias migrate="rake db:migrate"
alias migrate_t="rake db:migrate RAILS_ENV=test"
alias migrate_all="migrate && migrate_t"
alias redo="rollback && migrate"
alias rollback="rake db:rollback"
alias rollback_t="rake db:rollback RAILS_ENV=test"
alias redo_t="rollback_t && migrate_t"

# processes
alias pav="ps aux | grep \$1"

Simples!

Follow

Get every new post delivered to your Inbox.