Make working on multiple projects a breeze!
MIT License
I spend most of my time in a terminal and frequently issue commands that are almost similar. Unfortunately, almost makes a big difference. Here are a few things that can go wrong:
master
instead of develop
because anotherdevelop
and you were confused.bundle exec rails routes | grep <pattern>
only to discover this israke
instead of rails
.bash-ctx
helps you avoid such annoyances by helping you define
project-specific aliases and configuration variables. You can use it to:
.start
- doesn't matter whether it's.test
- again, no need to think about which tool themaster
and rebase the current branch on top of it just by calling.update
.Download and extract https://github.com/grn/bash-ctx/archive/master.zip and run
install
:
$ wget https://github.com/grn/bash-ctx/archive/master.zip
$ unzip master.zip
$ cd bash-ctx-master
$ ./install
This will install bash-ctx
in ~/.bash-ctx
. Remember to load it in your
.bashrc
:
# Load bash-ctx if available.
if [ -r "${HOME}/.bash-ctx/bash-ctx" ]; then
source "${HOME}/.bash-ctx/bash-ctx"
fi
The two essential concepts in bash-ctx
are:
~/.bash-ctx/ctx
. Each context is a file with bash commands to execute whenrails5
libYou first create a context and then enter it. If you enter a context then its
name is passed in an environment variable to a newly created bash child process
(instead of just sourcing it in the calling process). Your .bashrc
will then
source the right context file. The benefit of this approach is that you avoid
polluting your top-level shell with context-specific stuff and keep contexts
separated.
Create a new context for your project:
ctx new my-project
Edit the newly created context file with:
ctx edit my-project
You can add project specific aliases (e.g. alias .deploy='git push heroku'
),
set environment variables, change directories, and so on.
In order to enter the context run:
ctx enter my-project
This will launch a new bash
shell and run the context script you created
above. When you're done you can just quit this shell and return to the parent
shell (where you called ctx enter
).
You can also list all contexts with ctx list
:
ctx list
my-context
ctx_libs git rails5
# I often need to reset accounts but wouldn't like to reset the other tables.
function .cleanup() {
.run 'Account.update_all status: 0'
}
cd ~/Projects/MyProject
ctx_libs tmux
Libs are small shell script libraries that you can share across projects. It's
an easy way to define aliases, functions, and set environment variables.
Creating your own lib is easy. Just run ctx lib <name>
and call ctx_libs <name>
in your context file to include the lib in it.
Below is a list of provided libs.
git
Allowed commands:
.update [<remote>] [<branch>]
- stash changes, pull the specified remote$CTX_GIT_ORIGIN
and $CTX_GIT_BRANCH
which default toorigin
and master
) and rebase the current branch on top of it..squash <n>
- squash the last n
commits..amend
- edit the current commit message (git commit --amend -v
)..fix
- amend the current commit with the changes from the work treegit commit -a --amend -C HEAD
)..commit <message>
- equivalent of git commit -m <message>
.Allowed configuration variables:
CTX_GIT_ORIGIN
- the default origin. Defaults to origin
.CTX_GIT_BRANCH
- the default branch from origin. Defaults to master
.rails4
and rails5
All commands are prefixed by bundle exec
and use rake
(Rails 4) or rails
(Rails 5):
.migrate
- rails db:migrate
.rollback
- rails db:rollback
.seed
- rails db:seed
.reset
- rails db:reset
.start [<args>]
- rails s <args>
.run [<args>]
- rails runner <args>
.console [<args>]
- rails console <args>
.database [<args>]
- rails dbconsole <args>
.generate [<args>]
- rails generate <args>
.destroy [<args>]
- rails destroy <args>
.test [<args>]
- rails test <args>
.rspec [<args>]
- rspec <args>
.routes
- rails routes
.routes <pattern>
- rails routes | grep <pattern>
tmux
Does not define any commands but runs tmux
upon inclusion via ctx_libs
unless already in a tmux
session.
Upon entering a context you can:
screen
or tmux
session.PATH
or the prompt).I'd love to hear your feedback! If something doesn't work, or should work differently, just file an issue at https://github.com/grn/bash-ctx/issues/new.