Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

11–20 of 288 posts

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

#13
The one I use most often is undoubtedly `g` for `git`. Then I have many aliases inside Git for all kinds of regularly done things, quite a few lifted from Mercurial and then extended; some just shorter names (ci = commit, glog = log --graph) and some more advanced like these three:

  fixup = "!f() { TARGET=\"$(git rev-parse \"$1\")\"; shift; git commit --fixup=\"$TARGET\" \"$@\" && git -c core.editor=true rebase --interactive --autostash --autosquash \"$TARGET^\"; }; f"
  in  = "!f() { case \"x$1\" in x-*|x) branch=;; *) branch=\"$1\"; shift;; esac; git glog \"$branch..$branch@{upstream}\" \"$@\"; }; f"
  out = "!f() { case \"x$1\" in x-*|x) branch=;; *) branch=\"$1\"; shift;; esac; git glog \"$branch@{upstream}..$branch\" \"$@\"; }; f"
My most recent addition to my shell profile (a couple of weeks ago) enhances ts from the moreutils package. ts provides timestamps (relative to process execution or absolute) at the start of each line of stdout. My enhancement makes it cover stderr as well, so that stdout lines get a green timestamp and stderr lines get a red timestamp. Due to how interlacing works, and the fact that I commonly pair this with concurrency in the form of `make -j8`, it’s not perfect (can end up with a line possessing both green and red timestamps, and then none on the next line), but it’s still really helpful. I just need to get round to adding something to it to make programs still colour their own output. `script` can do that, I think.

It’s currently this shell function:

  ts-fancy() {
    ((("$@" 2>&1 1>&3 3>&-) | ts -s "^[[91m[%H:%M:%.S]^[[m") 3>&1 1>&2) | ts -s "^[[92m[%H:%M:%.S]^[[m"
  }
(Each ^[ shown there is a literal ␛.) ts-fancy was the temporary name I gave it at first, haven’t renamed it to anything more permanent yet, so it’ll probably stay that way forever.

Usage, `ts-fancy make -j8 --keep-going` and things like that. It’s proving really helpful when figuring out what’s slow in something that verbosely logs, and also in highlighting error output mixed in with normal output.

This exercise made me wish for a smarter terminal that kept track of timestamps and whether output went to stdout or stderr itself (at the character level, even, to resolve the problems of this line-based approach). I shouldn’t have to do this, and you know how often you think after the fact that it’d be nice to have timestamps?

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

#16
I work at a company with micro services so we have a lot of repos. We use nodejs and in some cases use different node versions among different services. Having the prompt show what branch I'm in and what node version I'm on is quite helpful:

  PS1="\[\033[01;31m\]Node \`node --version\`: \[\033[01;32m\]\`pwd\` \`git status -sb 2> /dev/null | head -n 1\`\n\[\033[01;34m\]\`date +'%a, %B %-d, %-l:%M:%S%P'\` \[\033[01;31m\]\$ \[\033[00m\]"

Pruning branches both remote and locally are also very helpful to do in these alias/functions:

  function gitpruneremote {
    git remote prune origin
  }
  
  function gitprunelocal {
    git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 
Git pulling in every repo:

  function gitpullall {
    cd ~/yourCodeDir
    find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull \;
  }
This one is simple, and was useful in my LAMP dev days, to tail apache logs with line breaks formatted:

  function tailerr() {
    tail -f /path/to/errorlog | sed 's/\\n/\n/g'
  }

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

#17
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 .."

I have the same exact aliases but they check for a Git or SVN repo and then use the right tool accordingly.

I also have `..`, but also `...` which maps to `cd ../..`

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

#18
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.

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

#19
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
}

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

#20
I have a whole repository also including several dotfiles that I maintain for already several years [1]. Following are some snippets from the dotfiles/.alias and dotfiles/.functions files from that repository that probably are the most interesting and which I am using on a regular basis:

    # easier navigation
    alias ..="cd .."
    alias ...="cd ../.."
    alias ....="cd ../../.."
    alias .....="cd ../../../.."
    alias ~="cd ~"

    # Get current public ip address
    alias ip="dig +short myip.opendns.com @resolver1.opendns.com"

    # Alias for summing a column
    alias mksum="paste -sd+ - | bc"

    # Export pbcopy / pbpaste to linux 
    if [[ "${OSTYPE}" == "linux"* ]]; then
        alias pbcopy="xclip -selection clipboard"
        alias pbpaste="xclip -selection clipboard -o"
    fi

    # create a new directory and enter it
    function mkd() {
        mkdir -p "$@" && cd "$_";
    }

    # plot stuff directly from the command line.
    # Example: seq 100 | sed 's/.*/s(&)/' | bc -l | plot linecolor 2
    # -> Generate 100 numbers, wrap it in s() and calc sin()
    function plot() {
        { echo 'plot "-"' "$@"; cat; } | gnuplot -persist;
    }

    # wrapper for easy extraction of compressed files
    function extract () {
      if [ -f $1 ] ; then
          case $1 in
              *.tar.xz)    tar xvJf $1    ;;
              *.tar.bz2)   tar xvjf $1    ;;
              *.tar.gz)    tar xvzf $1    ;;
              *.bz2)       bunzip2 $1     ;;
              *.rar)       unrar e $1     ;;
              *.gz)        gunzip $1      ;;
              *.tar)       tar xvf $1     ;;
              *.tbz2)      tar xvjf $1    ;;
              *.tgz)       tar xvzf $1    ;;
              *.apk)       unzip $1       ;;
              *.epub)      unzip $1       ;;
              *.xpi)       unzip $1       ;;
              *.zip)       unzip $1       ;;
              *.war)       unzip $1       ;;
              *.jar)       unzip $1       ;;
              *.Z)         uncompress $1  ;;
              *.7z)        7z x $1        ;;
              *)           echo "don't know how to extract '$1'..." ;;
          esac
      else
          echo "'$1' is not a valid file!"
      fi
    }
[1] https://github.com/NewProggie/Dev-Utils
Post reply on HN