Ubuntu Keyboard Shortcut: Open a selected file in Sublime Text 2

Friday, March 29, 2013
|
tags: ruby, rails, bash, linux, ubuntu

Whenever I'm looking at backtraces, logs, or failing tests in the terminal, I often need to open one of those files in my text editor. It was previously a semi-arduous process that involved highlighting, copying, pasting and the return key. Now, all I need to do is double-click or highlight a line, and then press a keyboard shortcut to open that file & line in my text editor (currently Sublime Text 2.) I've also added a thing to my $PROMPT_SCRIPT that stores my terminal's most recent directory in ~/.cwd~, so that the script can handle relative paths. (Most of the time I'm just in the root folder of a given project.)

One extra feature for Ruby developers is support for backtrace lines like this:

app/models/post.rb:225:in `sharing_is_caring'

If you double-click that file, you'll end up with the following selection: app/models/post.rb:225:in. The script will automatically strip the trailing :in, so you can just double-click instead of manually highlighting.

Requirements

  • Ubuntu
  • xclip (install with sudo apt-get install xclip)
  • A text editor, such as Sublime Text 2.

Installation

mkdir -p ~/bin
wget https://raw.github.com/ndbroadbent/dotfiles/master/bin/open_selected_in_editor -O ~/bin/open_selected_in_editor
chmod +x ~/bin/open_selected_in_editor
  • Modify the script to use your preferred text editor

Set up current working directory support

Add the following line to your ~/.bashrc:

PROMPT_COMMAND+="pwd > ~/.cwd~;"

This means that every time you press return in the terminal, the script can use your current directory to determine an absolute path for a highlighted file. It's not completely foolproof, but good enough for me.

Set up a keyboard shortcut in Ubuntu 12.04

  • Go to System Settings -> Keyboard -> Shortcuts -> Custom Shortcuts
  • Click the + to add a new shortcut with:
    • Name: Open selected in editor
    • Command: ~/bin/open_selected_in_editor
  • Set the keyboard shortcut. I like Ctrl+Shift+X.

Ubuntu Keyboard Shortcuts

All done! Now you can highlight a filename in the terminal, press your keyboard shortcut, and open it in your editor without the need to copy & paste filenames. Please let me know if you need any help, but I'm sorry I don't know how to do this in OS X or Windows.

Open files generated by 'rails generate' in your editor

Wednesday, December 19, 2012
|
tags: ruby, rails, scripts, rails-3

Note: This post is for people who use the terminal to run Rails generators. It probably won't interest you if your editor has a plugin to run them.

After running a Rails generator, you'll often need to edit the generated files in your text editor.

Rails 4 may soon have the ability to open generated files in your text editor if you pass the --editor option on the command line. You can follow my pull request for more details.

If you use the --editor option (or its -e alias), the generator will open all generated or copied files in your text editor. The default editor is your GUI_EDITOR or EDITOR environment variable, but you can set the editor manually with --editor=manual-code-editor.

Rails 3 isn't accepting any new features, so here's how you can use the --editor option for your Rails 3 apps:

Single Rails 3 app

To install the --editor option in a single Rails 3 app, run the following command from your app directory:

curl https://gist.github.com/raw/4342095/rails -o script/rails

This will update script/rails, and you can commit the change.

Globally for all Rails 3 apps

If you are developing a lot of different apps, it might be too much hassle to update all of them. Instead, you can download the custom rails script to your /bin directory, and add some shortcut functions to your .bashrc.

Download script:

sudo curl https://gist.github.com/raw/4342095/rails3_with_editor -o /bin/rails3_with_editor
sudo chmod +x /bin/rails3_with_editor

Add shortcut functions:

Add the following to your ~/.bashrc:

rge() { rails3_with_editor generate "$@" --editor; }
rgm() { rails3_with_editor generate migration "$@" --editor; }

Now you can run rgm to generate a migration from any Rails 3 app.

Automatically run 'bundle install' when Bundler can't find a gem

Monday, December 17, 2012
|
tags: ruby, rails, bash, bundler, rubygems

Every Rails developer has probably experienced the following error:

Could not find <gem> in any of the sources
Run `bundle install` to install missing gems.

This happens if you or someone else adds a gem to your Gemfile, or if a gem version is updated in Gemfile.lock, and you forget to run bundle install before running a Rails command.

Here's a simple function that handles this automatically, called bundle_install_wrapper(). It first tries to execute the command you pass to it. However, if Bundler exits with status code 7 (GemNotFound), then it will run bundle install. Finally, it retries the original command.

bundle_install_wrapper() {
  # Run command
  eval "$@"
  if [ $? = 7 ]; then
    # If command crashes, try a bundle install
    echo -e "\033[1;31m'$@' failed with exit code 7."
    echo    "This probably means that your system is missing gems defined in your Gemfile."
    echo -e "Executing 'bundle install'...\033[0m"
    bundle install
    # If bundle install was successful, try running command again.
    if [ $? = 0 ]; then
      echo "'bundle install' was successful. Retrying '$@'..."
      eval "$@"
    fi
  fi
}

Usage

Drop the function in your ~/.bashrc, and add aliases for rails commands:

alias rs="bundle_install_wrapper rails server"
alias rc="bundle_install_wrapper rails console"
# etc.

If you want aliases that support both Rails 2 and 3 applications, you can use something like this:

# Run rails commands on either 2.x and 3.x
rails_cmd(){
  if [ -e ./script/rails ]; then bundle_install_wrapper rails $@
  elif [ -e ./script/$1 ]; then bundle_install_wrapper ./script/$@
  else echo "== Command not found. (Are you sure this is a rails 2.x or 3.x application?)"
  fi
}
alias   rs="rails_cmd server"
alias  rsd="rails_cmd server -u"
alias   rc="rails_cmd console"
alias   rg="rails_cmd generate"

See the Ruby on Rails section in my .bashrc if you're interested in more aliases, and please leave a comment if you have any tips to share.