Guides

This pages shows various ways of configuring leader to work with common tools.

Available guides:

Integrating Git

Using git from the command line can be quite tedious, especially when frequently having to run the same commands. Users of emacs often use magit and the setup presented here can best be thought of as an approximation of magit’s functionality outside of emacs.

Configuration

Add the following to your ~/.leaderrc:

{
  "keys": {
    "g": {
      "name": "git",
      "loopingKeys": ["s"],
      "keys": {
        "p": "git pull",
        "P": "git push",
        "c": "git commit -m",
        "s": "git status",
        "a": "git add -p .",
        "f": "git fetch",
        "b": "git select-branch",
        "B": "read -e -p 'Create branch: '; git checkout -b \"$REPLY\"",
        "g": "read -e -p 'Pattern: '; git grep \"$REPLY\"",
        "z": {
          "name": "stash",
          "keys": {
            "z": "git stash save",
            "p": "git stash pop"
          }
        }
      }
    }
  }
}

Most of the bindings should be obvious. A few bindings might need some explaining words:

Interactive selections with fzf

Sometimes commands that you bind to keys with leader need some extra information, for example which git branch to check out or which manual page to open.

Most of the times that extra bit of information comes from a set of existing items: the list of branches in your git repository or the list of manual pages installed on your system.

fzf is a command line utility for presenting interactive selections in the terminal. This guide examines a few possibilities of using fzf together with leader.

Prerequisites:

Changing git branches

Place the following script as git-select-branch in a directory on your PATH:

#!/bin/bash

BRANCH_NAME=$(git branch --all --list --format='%(refname:lstrip=2)' | fzf --height=10)
git checkout "$BRANCH_NAME"

Then add the following binding to your ~/.leaderrc, whereever you already have bindings for git:

    "b": "git select-branch"

The end result:

asciicast

Quickly opening man pages

Place the following script as select-man-page in a directory on your PATH. The scripts uses the output of apropos(1) to show a summary of manual pages installed on your system and then presents you with a selection of all those man pages.

#!/bin/bash
apropos . |
fzf --height=10 |
tr -d '()' |
awk '{print $2 " " $1}' |
xargs man

Then add the following binding to your ~/.leaderrc:

  "m": "select-man-page"

The end result:

asciicast

Jumping around in the filesystem

Since commands issued through leader are evaluated in the context of the current shell, leader can be used to change the shell’s current working directory and other shell-local settings (such as key bindings).

This can be used for quickly accessing common directories and navigating through the filesystem with single key presses.

Here is an example configuration showing some of the possibilities:

{
  "keys": {
    "j": {
      "name": "jump",
      "loopingKeys": ["j", "k"],
      "keys": {
        "k": "pushd .. && pwd",
        "j": "popd && pwd",
        "\\": "cd $GOPATH/src/github.com/dhamidi/leader"
      }
    }
  }
}

The example above includes a shortcut to jump into the leader’s repository on disk to make changes there, since it’s a often used directory. The keys k and j are used to navigate up and down through the directory hierarchy. Since both of these keys are listed as looping keys, they can be pressed repeatedly to quickly navigate through the filesystem.

This could be further extend with a helper script that lists the alphabetically next/previous directory in the current directory, which would allow for “lateral” movement between the children of the current directory and could be bound to h and l.

Ruby on Rails

When developing with Ruby on Rails there are many common commands that are invoked frequently. leader can remove a lot of friction from the development process by providing quick access to many of these commands.

Configuration

Add the following to your ~/.leaderrc:

{
  "keys": {
    "r": {
      "name": "ruby",
      "keys": {
        "d": {
          "name": "database",
          "keys": {
            "m": "bundle rake db:migrate",
            "r": "bundle rake db:reset"
          }
        },
        "s": "bin/rails server",
        "c": "bin/rails console",
        "t": "bundle exec rake test"
      }
    }
  }
}

Don’t forget that you can add project local bindings for project-specific rake tasks by placing an additional .leaderrc in your project’s root directory.