Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

41–50 of 288 posts

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

#42
I use sd (kinda like cd) when working with on mac with finder. With sd, you can select a file in finder, CMD+C and sd filepath.txt. The nice thing is that it goes to the same directory as the file so you don't have to delete the last part of the path manually. If you are sd-ing into a directory, it behaves like cd.

    sd () {
        if [ -f "$*" ]
        then
                cd "$(dirname "$*")"
        else
                cd "$*"
        fi
    }
Also cwd which is like pwd but it also copies the current path to your clipboard.

    cwd='pwd | tr -d "\n" | pbcopy && pwd'

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

#44
post #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 ~" # Ge…

You might enjoy replacing your extract function with atool (https://www.nongnu.org/atool/) which provides aunpack. It works similarly, but has niceties like creating and unpacking into a subdirectory to avoid accidentally spraying files all over the current working directory if the archive has multiple files at its root.

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

#45
I lifted most of this from the guy mentioned in the hn post in the comments below. I don't know what shell/platform he used, but it didn't work from twitter so I tweaked it to work on the linux boxes I typically see.

It creates a different history file from each shell session. Most of my shell history goes back about 3 years now. The downside is you can't up arrow or crtl R except for your current history, but you can use the histgrep function to search everything.

I keep meaning to make another processed file which would uniq the history without the datestamp and maybe hook into fzf, but this has been good enough for now.

  # tricks from https://news.ycombinator.com/item?id=10162189

  if [ ! -d ~/.history/$(date -u +%Y/%m/) ]; then
        mkdir -p ~/.history/$(date -u +%Y/%m/)
  fi
  #cat .history/2016/09/* |sort -n| uniq

  export HISTFILE="${HOME}/.history/$(date -u +%Y/%m/%d.%H.  %M.%S)_${HOSTNAME}_$$"
  export HISTSIZE=
  export HISTCONTROL=ignoreboth
  export HISTCONTROL=erasedups
  export HISTTIMEFROMAT="%d/%m/%Y-%H:%M:%S"
  #export PROMPT_COMMAND='history -a'
  export HISTIGNORE='&:bg:fg:clear:ls:pwd:history:exit:make*:* --help:'
  export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
  histgrep () 
  {
  grep -r "$@" ~/.history
  history | grep "$@"
  }

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

#49
post #34

alias grpe=grep Nothing comes close to the amount of time I saved with this one.

Hehe I liked the 'lcoate' one on this page too, same idea. It might be super-useful to have a function that, if command not found and if there's an anagram of what you typed that is a command, asks if you meant that instead, y/n? (That would make computers seem a lot more intelligent!)

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

#50
Truncate the $PWD to ¼ of the terminal width and put the result into the command prompt:

    # truncate the $PWD
    function cut_pwd
    {
      newPWD="${PWD/#$HOME/\~}"
      local pwdmaxlen=$((${COLUMNS:-80}/4))
      if [ ${#newPWD} -gt $pwdmaxlen ]
      then
         newPWD=".+${newPWD: -$pwdmaxlen}"
      fi
    }

    PROMPT_COMMAND=cut_pwd
    PS1="\h:\${newPWD} \\$ "
Post reply on HN