Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

21–30 of 288 posts

Re: Ask HN: Best things in your bash_profile/aliases?

#21
Used to work primary using docker on my laptop, but been working on some bigger applications that need to be run on beafier machines, so just set environment variable and have these:

    $ cat ~/bin/docker
    #!/bin/bash
    
    if [ "$DOCKER_HOST" != "" ]
    then
      HOST_ARG="-H $DOCKER_HOST"
    fi
    
    /usr/bin/docker $HOST_ARG $@

    $ cat ~/bin/docker-compose
    #!/bin/bash
    
    if [ "$DOCKER_HOST" != "" ]
    then
      HOST_ARG="-H $DOCKER_HOST"
    fi
    
    /usr/local/bin/docker-compose $HOST_ARG $@

Re: Ask HN: Best things in your bash_profile/aliases?

#22
This lets you easily search your command history by typing the beginning of a command and pressing the up arrow.

    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
This one automatically lists the files in the current directory, but limits the number of lines.

    function cd {
        builtin cd "${@}"
        if [[ "$(stat -c "%b" .)" -lt "100" ]]
        then
        if [ "$( ls -C -w $COLUMNS | wc -l )" -gt 30 ] ; then
            ls -C -w $COLUMNS --color=always | awk 'NR 

Re: Ask HN: Best things in your bash_profile/aliases?

#23
post #3

Since I'm working in git repos the majority of the time, I type these hundreds of times per day: alias s="git status" alias d="git diff" I prefer looking at this stuff in a terminal rather than an IDE. Reducing them to a single character just feels so good. Note: not a git alias like "git s", literally just the single character "s" in Bash itself. And coming in third: alias ..="cd .."

alias ..="cd .." If you use zsh, typing a directory name changes to it automatically. It’s almost always a very delightful thing.

add z to it!

Re: Ask HN: Best things in your bash_profile/aliases?

#29
I am using zsh/iterm on my mac so here are mine:

    # branches that were touched lately 
    nb() {
      git for-each-ref --sort=-committerdate refs/heads/ -- format='%(committerdate:short) %(authorname) %(refname:short)' | head -n 10
    }

    # squash!
    squash() {
      if [ -n "$1" ]; then
        git reset --soft HEAD~$1 && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
      fi
    }

    # open pr page on github for current branch
    pr () {
      local repo=`git remote -v | grep -m 1 "(push)" | sed -e "s/.*github.com[:/]\(.*\)\.git.*/\1/"`
      local branch=`git name-rev --name-only HEAD`
      echo "... creating pull request for branch \"$branch\" in \"$repo\""
      open "https://github.com/$repo/pull/new/$branch?expand=1"
    }

    # check who uses port 
    port() {
      lsof -i tcp:"$@"
    }

    # rebases local branch from origin /master and force pushes it
    repush(){
      git fetch origin master
      git stash
      git rebase origin/master
      git push --force
      git stash pop
    }

Re: Ask HN: Best things in your bash_profile/aliases?

#30

This one is pretty useful! Open current OSX Finder directory in Terminal: cdf () { target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'` if [ "$target" != "" ] then cd "$target" pwd else echo 'No Finder window found' >&2 fi }

And `open .` does the opposite: opens Finder to your current directory.
Post reply on HN