Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

91–100 of 288 posts

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

#91
Well the most used, and first thing i add when i get to a new system is:

   rcbash='~/.bashrc' 
   alias src="source $rcbash"
   alias vrc="vim $rcbash"
   alias vimo="vim -O"  # vsplit files

And then, a little snippet which made my "bashrc" less repetitive when it comes to folder aliases.

   gr4_folderize() {
    # creates aliases and variables for a path
    #
    # $1 = used abbreviation
    # $2 = folder path
    #
    # gr4_folderize "esp32" "/srv/dd/esp32"
    # creates aliases `cdesp32`, `lsesp32` and `diresp32` env

    # ${param:?word} writes `word` to stdout when param is unset or null
    local abbrev="${1:?No abbreviation alias.}"
    local folder="${2:?No directory to folderize.}"

    export dir${abbrev}="$folder"
    which > /dev/null 2>&1 cd${abbrev} || alias cd${abbrev}="cd $folder"
    which > /dev/null 2>&1 ls${abbrev} || alias lll${abbrev}o="lla $folder"
   }

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

#92
Here's a function you can use to generate a strong printable password on the fly:

    function gen_passwd () {
        openssl rand 150 |\
        tr -dc  '[:graph:]' |\
        cut -c 1-${1:-13}
    }
The default password length is 13 characters but you can pass a different length parameter instead.

Explanation:

* generate 150 pseudo-random bytes using openssl's rand

* discard non-printable characters

* cut to desired length

Note that it will only use the ASCII character set, if you prefer a solution that also uses german umlauts (öäüÖÄÜß) or something more exotic like emoji in your passwords you're on your own.

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

#93

Well the most used, and first thing i add when i get to a new system is: rcbash='~/.bashrc' alias src="source $rcbash" alias vrc="vim $rcbash" alias vimo="vim -O" # vsplit files And then, a little snippet which made my "bashrc" less repetitive when it comes to folder aliases. gr4_folderize() { # creates aliases and variables for a path # # $1 = used abbreviation # $2 = folder path # # gr4_folderize "esp32" "/srv/dd/e…

Plus `ask_yes` if you want to ask if user is sure.

   ask_yes () {
    read -r -p "$1 = Are you sure? [y/N] " response
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
    then
        return 0  # true = 0
    else
        return 1  # false = 1
    fi
   }
Plus `countdown timer` to make user think about what he has done, before he does it. (Code is a mess cause i used some snippet then created another from it)

  countdown_str () {
    # countdown "00:01:00" # = hh:mm:ss
    IFS=:
    set -- $*
    secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
    while [ $secs -gt 0 ]
    do
      sleep 1 &
      printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
      secs=$(( $secs - 1 ))
      wait
    done
    echo
  }

  countdown_5 () {
    countdown 5 2>/dev/null
  }

  countdown () {
    secs=${1:-"00"}
    mins=${2:-"00"}
    hours=${3:-"00"}
    chars=$(printf "%02d:%02d:%02d" $hours $mins $secs)
    echo "Countdown from $chars"
    countdown_str $chars 2>/dev/null
  }

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

#94
post #84

Not strictly an alias, but autojump has been a huge productivity booster for me. Installing it sets up an alias of `j` which guesses the best directory to cd to based on your cd history. So instead of having to type `cd ~/Projects/someproject` you could just type `j some` and it'll end up there. https://github.com/wting/autojump

+1. Very useful extension

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

#97

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 --inte…

Very nice! Thanks you for sharing this!

It really seems very useful to have timestamps for every line of output.

I have tried to approximate this in the past, by setting my bash prompt to be:

    export PS1='\[\e[00;37m\]$?\[\e[0m\]\[\033[1;32m\]\[\033[1;32m\][\t]\[\033[1;41m\]\u\[\033[1;41m\]@\[\033[1;41m\]\h:\[\033[0m\] \[\033[1;32m\]\w\[\033[0m\] \$ '
... which shows the previous command exit code, the current time (with second resolution) and the ordinary stuff like username host and working dir, so it looks somewhat like:

    0[21:20:18]delian66@mypc: ~/Work $ slow_command
    ....
    0[21:22:19]delian66@mypc: ~/Work $

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

#99
I use fzf [0] with zsh. I frequently use the Ctrl-T and Ctrl-R commands for finding files and searching history, respectively. Ctrl-R without fzf is great too, but with fzf you can see more of the history than just the currently selected entry. I hardly do `history | grep` anymore.

I've also been using trash-cli [1] for years with `rm` aliased to `trash-put`. It's saved me a handful of times when I've carelessly `rm`d something I shouldn't have.

[0] https://github.com/junegunn/fzf

[1] https://github.com/andreafrancia/trash-cli - Command Line Interface to FreeDesktop.org Trash.

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

#100
post #80

Can I add something from .vimrc? If you put the commands: set undofile set undodir=~/.vim/undodir You get infinite undo in your files. Now when you go into a file, delete or add some text and close your terminal down, the next time you enter vim you can (re|un)do previous operations. Has saved my ass more times than I care to mention over my career!

I can't believe I didn't know about this! Thanks so much :)
Post reply on HN