One of the beautiful things about the bash terminal is its optimization for the power user. With that in mind, here are a few of my favorite one-liners for increasing efficiency.

Prelude

Package manager. Make sure you have a package manager. As always, if you’re on a Mac, I suggest Homebrew.

Bashrc. Of course, you need somewhere to put these commands. I use ~/.bashrc. On Macs, be sure to add source ~/.bashrc to your ~/.bash_profile. For more information on Macs’ interaction with bash startup files, read this.

History. Allow your bash history file to have an unlimited number of lines here. This step is life-changing. You can also set which file to use as your history file. Mine is set with export HISTFILE=~/.bash_eternal_history.

Looking around

Looking around with can be painful and slow. These four commands ease navigation and visualization.

  • ll
    • Prettier, more informative version of ls
    • alias ll="ls -lhAG"
  • cl
    • Change directory and list the files right away
    • function cl() { cd "$@" && ll; }
  • cwd
    • Copy the working directory to your clipboard (Mac version)
    • alias cwd="pwd | tr -d '\n' | pbcopy"
  • tree
    • See a tree-like view of the current directory and subdirectories
    • May have to install tree first
    • alias tree="tree -alC -I 'node_modules*|.git*|.meteor*'"

Searching

Jumping straight to a command or file has never been fuzzier.

Prelude

  • Install ag
    • Blazing fast search
    • Allows a custom .agignore file (like .gitignore, but for searching)
    • If you don’t want to make the plunge, ack is more seasoned and has never given me issue
  • Install fzf
    • Fuzzy search
    • Synergizes with ag
  • export FZF_DEFAULT_COMMAND='ag -g ""': have fzf use ag as its search tool
  • alias ag="ag -U --nonumbers": use just your ~/.agignore file for search exclusions; --nonumbers makes below work

Commands

  • histf

    • Use a fuzzy search on your history file to find the command you want, then execute that command
    • alias histf='__tmp=$(ag "^\w" $HISTFILE | gtac | fzf) && history -s "$__tmp" && eval "$__tmp"'
    • gtac is the name of the tac command when using the default coreutils installation with Homebrew
      histf example
  • vf

    • Use a fuzzy search on your current directory to find the file you want, then open that file in vim
    • alias vf='__tmp=$(fzf) && history -s "vim $__tmp" && vim $__tmp'
      vf example
  • hist <some text>

    • Search through your history file and print lines matching <some text>
    • function hist() { ag "$@" $HISTFILE; }
  • histt <some text>

    • Like hist, but includes metadata (and is slower)
    • function histt() { history | ag "$@"; }

Updating your bashrc

Smooth the process of continuous updates.

  • bashrc
    • Quick access to your bashrc
    • alias bashrc="vim ~/.bashrc"
  • src
    • alias src="source ~/.bashrc"
    • Apply changes to your bashrc to the current session

Path

Should really come built-in.

  • path
    • List your path in a readable format (borrowed from nixCraft)
    • function path() { __tmp=$IFS; IFS=:; printf "%s\n" $PATH; IFS=$__tmp; }

Full list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export FZF_DEFAULT_COMMAND='ag -g ""'
export HISTCONTROL=ignoreboth
export HISTFILE=~/.bash_eternal_history
export HISTFILESIZE=-1
export HISTSIZE=-1
export HISTTIMEFORMAT="[%F %T] "
alias ag="ag -U --nonumbers"
alias cwd="pwd | tr -d '\n' | pbcopy"
alias emacs="echo '-> shame' && vim"
alias histf='__tmp=$(ag "^\w" $HISTFILE | gtac | fzf) && history -s "$__tmp" && eval "$__tmp"'
alias ll="ls -lhAG"
alias tree="tree -alC -I 'node_modules*|.git*'"
alias vf='__tmp=$(fzf) && history -s "vim $__tmp" && vim $__tmp'
function cl() { cd "$@" && ll; }
function hist() { ag "$@" $HISTFILE; }
function histt() { history | ag "$@"; }
function path() { __tmp=$IFS; IFS=:; printf "%s\n" $PATH; IFS=$__tmp; }

Happy bash-ing.