The Secret To A 10x Faster Terminal Workflow in Rails

The Default Terminal: Functional, But Basic

Think of the default Terminal like a basic, reliable car. It gets you where you need to go, but it doesn’t have all the modern conveniences. The standard Terminal lacks some crucial features that developers rely on. For example it doesn’t have split screen feature or convenient search options.

Introducing iTerm2: Your Upgraded Cockpit

iTerm2 is a fantastic replacement for the standard Terminal. Imagine it as upgrading to a high-performance sports car. Here’s what makes iTerm2 so great:

  • Split Panes: Work on multiple tasks side-by-side within the same window. No more switching between multiple terminal windows, or resizing. Imagine being able to run your server on one side of the screen, and edit your code on another.
  • Hotkey Window: Access iTerm2 instantly with a keyboard shortcut. This is incredibly useful for quickly running commands without having to search for an open terminal window.
  • Search Highlighting: Easily find what you’re looking for in your terminal output.
  • Mouseless Copy/Paste: Copy and paste text with keyboard shortcuts, for faster workflow.
  • Paste History: No more searching for the command you ran earlier, just use the history to go back.
  • Instant Replay: Rewind and replay your terminal session to review what you did.
  • Multiple Smart Profiles: Create custom profiles for different projects or tasks with their own unique settings and colors.

How to Get iTerm2: Visit the iTerm2 official website to download and install it.

Making It Look Good: Fonts and Colors

A good-looking terminal isn’t just for aesthetics; it can actually make it easier to read and understand your code and output.

  • Fonts: Monospaced fonts are ideal for programming because each character takes up the same amount of space, making alignment easier. Recommended fonts include:
    • Source Code Pro: A popular font from Adobe, perfect for coding.Inconsolata: Another excellent choice with great readability.Menlo: A clean and clear font that comes pre-installed on macOS.
    Set your font size to around 13-14pt for comfortable viewing, use Regular font style and ensure that anti-aliasing is enabled for smoother edges.You can download the Source Code Pro from Font Squirrel
  • Color Schemes: Choose a color scheme that is easy on the eyes. Here are some popular options:
    • Smyck: A great scheme for a dark theme, especially for Ruby developers.
    • Solarized Dark: A popular, well-balanced color scheme.
    • Molokai: A bold and vibrant theme that helps keep your attention.

Replacing Bash with Zsh: The Modern Shell

The shell is the program that interprets the commands you type in the terminal. The default shell on macOS is Bash, which is older and has limitations. Zsh is a more modern and powerful alternative with better features.

  • Why Zsh?
    • It’s more advanced than Bash, especially for developers.
    • It has much better features out of the box.
    • It’s still familiar if you’re used to Bash.
  • Key Features of Zsh
    1. Interactive Tab Completion: Just press tab and it will autofill commands and file names.
    2. Partial Path Completion: Start typing a path, press tab, and Zsh will do the rest.
    3. Autocomplete for:
      • kill commands (for stopping running programs)
      • ssh commands (for connecting to remote servers)
      • scp commands (for copying files over networks)
    4. Path Replacement: You can quickly modify your current directory path.
  • Installing Zsh
    1. Open your Terminal (or iTerm2) and run this command:
brew install zsh zsh-completions
  • This command uses brew, which is the Homebrew package manager, if you don’t have it, please install it first. Instructions on brew website)
  • Then, set Zsh as your default shell:
chsh -s $(which zsh)
  • Restart your terminal for the changes to take effect.
  • Zsh Configuration: Zsh is configured using the .zshrc file, which is similar to .bashrc for Bash.

Oh My Zsh: Supercharging Your Shell

Oh My Zsh is a popular framework for managing your Zsh configuration. It comes with themes, plugins, and other features that make your terminal even more powerful.

  • What Does it Do?
    • Provides pre-configured themes to change the look of your terminal.
    • Offers a plugin system to extend Zsh’s capabilities.
    • Manages all your configurations in an organized way.
  • Installing Oh My Zsh:
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • Basic Configuration Edit your .zshrc file to enable plugins and set the theme:
# Example plugin configuration
plugins=(git bundler osx rake ruby)

# Example theme setting
ZSH_THEME="ys"
  • Plugins: Extend your zsh functionality, git plugin adds git aliases and helpful features. bundler plugin allows to use bundle command shortcuts, like bi instead of bundle install
  • Theme: This determines how your prompt looks, ys is an example theme. You can try different ones and choose your favorite.

Alternative Framework:
* Prezto: A lightweight alternative to Oh My Zsh, that is very modular. If Oh My Zsh seems too heavy or you want to try something different you can check it out.

Make Terminal Accessible With iTerm2 Visor

iTerm2’s Visor feature allows you to bring your terminal to the front with a keyboard shortcut, which is very convenient, as you don’t have to search for a terminal window, and can access it anytime from anywhere in the OS. You can enable it under iTerm settings in “Keys” section and set a desired keyboard shortcut (Ctrl + ` is often recommended). Consider mapping your Caps Lock key to Ctrl for easier access.

Enhancing Command Line Experience with Zsh

Global Aliases In Zsh, you can define aliases that work anywhere in the command line. For example:

  • alias -g G='| grep' to search (like ls -la G text instead of ls -la | grep text)
  • alias -g H='| head' to show the first few lines
  • alias -g T='| tail' to show the last few lines
  • alias -g CP='| pbcopy' to copy the command output to clipboard

Command Chaining: Zsh is designed to work well with the Unix philosophy of combining simple tools to do complex things. You can chain multiple commands using the pipe | symbol. For example ls -la | grep “test” will list all the files, and pipe the output to the grep which will filter by “test” text.

Command Auto Completion: Zsh helps you by completing commands, options, and flags for git, services like Heroku. Enable gitfast plugin in .zshrc for better Git completion performance.

Fast File Navigation

Fasd: A tool for quickly jumping to directories or files that you use often. Install using brew install fasd. It will build a frequency database based on your usage.

Rails Command Optimization

Use the Oh My Zsh Rails plugin for shortcuts for common tasks:

alias devlog='tail -f log/development.log' # to see the development log
alias rc='rails console' # to open rails console
alias rdm='rake db:migrate' # to run migrations

Binstubs Project-specific executables located in bin/ folder, an alternative to bundle exec.

HTTP Tools

HTTPie A modern command-line HTTP client.

  • Intuitive syntax, syntax highlighting, session management and pretty-printed output.
  • Install with brew install httpie.
  • Enable the httpie plugin in Oh My Zsh for autocompletion.
  • You can create custom sessions by creating config.json file with required headers, like for authentication.
{
  "headers": {
    "Accept": "application/json",
    "Cookie": "<cookie_content>"
  }
}

Project Management

Rails Notes System:

  • Use TODO, FIXME, and OPTIMIZE comments in your Ruby code to make notes.
  • Use rake notes in terminal to list all the annotations for review.
#TODO: handle edge case
#FIXME: breaks under condition
#OPTIMIZE: performance issue

Further Resources:


Posted

in

by