capistrano_colors has been merged into capistrano

Wednesday, November 07, 2012
|
tags: rails, ruby, linux, capistrano, server, administration

capistrano_colors has been merged into capistrano. If you update capistrano to version 2.13.5, you will no longer need to include capistrano_colors in your Gemfile.
Don't forget to also remove require 'capistrano_colors' from config/deploy.rb.

If you've been using capistrano without the capistrano_colors gem, well, you've been missing out! Your logs will now be nicely formatted and colored by default, like this:

Capistrano logs with colors and formatting

However, if you happen to be a big fan of black & white logs, you can just add disable_log_formatters to config/deploy.rb. See the Formatting Logs wiki page for more information about customizing the colors and styles.

Thanks to @stjernstrom for creating the capistrano_colors gem, and to @carsomyr for merging my pull request!

Auto-reloading your .bashrc (or .zshrc)

Monday, October 29, 2012
|
tags: bash, linux, scripts

If you often make changes to your ~/.bashrc, you might be sick of typing source ~/.bashrc after every change. It can also be annoying when you switch to a different tab in your terminal, but your new aliases or functions aren't available until you type source ~/.bashrc.

While I was working on scm_breeze and my dotfiles repo, I grew tired of having to type this command, so I aliased it to sbrc. But I knew I could do better, so I created an auto-reload script that reloads my ~/.bashrc if there are any changes to itself, or any of the files that it loads.

When you run it at the beginning of your .bashrc, it wraps the source and . commands with a function that builds an index of all the sourced files. At the end of your .bashrc, you need to call the finalize_auto_reload function, which:

  • Removes the source and . overrides
  • Sorts the sourced file index and removes duplicates
  • Stores the mtime of the most recently modified source file in a variable
  • Adds the auto_reload_bashrc function to your PROMPT_COMMAND.

Whenever you start a new line in your terminal, the auto_reload_bashrc function reloads your .bashrc if any of the sourced files have changed. Changes are detected by looking up the most recent modification time from all of the sourced files, and comparing that time with the previous value.

My .bashrc sources 28 files from my dotfiles, scm_breeze, and RVM. Running the auto_reload_bashrc function to check for changes only takes 11 ms.

If you make a lot of changes to your .bashrc or .zsh, you can check out my auto-reloading script here: https://github.com/ndbroadbent/dotfiles/blob/master/bashrc/auto_reload.sh

Saving space in the terminal with symbols

Sunday, October 14, 2012
|
tags: linux, bash, ruby, scripts

I'm saving a little space in my terminal by replacing my username and group (ndbroadbent) with a single symbol. I'm doing this in my prompt, as well as in the output of ls commands:

ls and prompt with symbols

(My laptop's hostname is also represented by a symbol.)

For the ls output, it was a bit tricky to re-justify the username and group columns after substituting my username. I decided to do it in ruby, and then played some ruby golf:

o=STDIN.read;re=/^(([^ ]* +){2})(([^ ]* +){3})/;u,g,s=o.lines.map{|l|l[re,3]}.compact.map(&:split).transpose.map{|a|a.map(&:size).max+1};puts o.lines.map{|l|l.sub(re){|m|"%s%-#{u}s %-#{g}s%#{s}s "%[$1,*$3.split]}}

This little script parses the modified output of ls -lhv, calculates the max length of the user/group/size columns, and then pads those columns with the correct number of spaces.

My final ls command looks like this:

ls -lhv --group-directories-first | sed \"s/$USER/\$(/bin/cat $HOME/.user_sym)/g\" | rejustify_ls_columns

(where rejustify_ls_columns is a function wrapping the ruby script above.)