Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

31–40 of 288 posts

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

#31

function convert-decimaltobinary(){ n="$1" bit="" while [ "$n" -gt 0 ]; do bit="$(( n&1 ))$bit"; : $(( n >>= 1 )) done printf "%s\n" "$bit" } function convert-binarytodecimal(){ echo "$((2#$1))" } function convert-hextodecimal(){ echo $(( 16#$1 )) }

I think I'll use them more if they're cdb, cbd, chd. Thanks!

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

#32

    alias continue-branch='charm $(git diff master --name-only)'  # Open all files changed in the current branch wrt master in PyCharm

    function record-window {
	recordmydesktop --windowid=$(wmctrl -l | grep $1 | awk '{print $1}') -o ~/Videos/$1_$(date +"%Y_%m_%d_%H_%M_%S").ogv
    }

    alias ping='grc ping'
    alias ifconfig='grc ifconfig'
    alias netstat='grc netstat'

These last three use the awesome 'grc' output colorizer

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

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

I like

  alias h="cd -"
to go to the last directory I was in. Inspired by how you move the cursor to the left in vim.

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

#35
This is tiny, but I've gotten a surprising amount of mileage out of aliasing 'fx' to

  firefox --new-instance --profile $(mktemp -d)
which pops open a new instance of Firefox with a completely clean, temporary profile.

Super useful for testing sites or add-ons without influencing (or being influenced by) by my normal browsing profile.

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

#36
Not as fancy as other tips here, but I bet this is really time saving in the long run for most developers.

My core work is alway around ~10 directories, which are mostly code packages. As soon as I start working on a project/team, I create aliases for directories. They take the form:

  alias agg="cd /ws/ProjectName/src/PackageName/ &&
             echo -n -e '\033]0;PackageName\007' &&
             tab-color-devil-blue"
The first part of course navigates to the directory. The second part sets the tabName of my iTerm to the package name. The last part sets the background color of the tab to something unique for the package. This makes it easy to spot the right tab when I have like 10 iTerm tabs open.

In case you want to see the definition of the last part:

    set_tab_color() {
        echo -ne "\033]6;1;bg;red;brightness;$1\a"
        echo -ne "\033]6;1;bg;green;brightness;$2\a"
        echo -ne "\033]6;1;bg;blue;brightness;$3\a"
    }
    tab-color-devil-blue() { set_tab_color 34 112 147 }
Tested only in iTerm.

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

#37

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…

The first one is pretty much CTRL+R (reverse incremental search)

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

#38
For Python programmers, I have a bash/zsh function called `pycd` that takes any Python package/module name as an argument, and changes your current working directory to be the library or site-packages location of that library, according to the current python interpreter in your $PATH. Here it is:

    pycd () {
        pushd `python -c "import os.path, $1; print(os.path.dirname($1.__file__))"`;
    }
Example usage, with abbreviated $PWD shown after "»" and shell commands entered after "$" in this example session:

    » (~/repos/web)
    $ pycd flask
    
    » (~/./v/3/e/w/l/p/s/flask)
    $ pwd
    ~/.pyenv/versions/web+site/lib/python3.6/site-packages/flask
    
    » (~/./v/3/e/w/l/p/s/flask)
    $ head app.py                                            
    # -*- coding: utf-8 -*-
    """
        flask.app
        ~~~~~~~~~
    
        This module implements the central WSGI application object.
    
        :copyright: © 2010 by the Pallets team.
        :license: BSD, see LICENSE for more details.
    """
    
    » (~/./v/3/e/w/l/p/s/flask)
    $ popd      
    ~/repos/web ~
                                    
    » (~/repos/web)
    $
So, usually lets you explore the source code, at the command-line, for any Python libraries you have installed.

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

#40
Annoyingly not built in, pathadd and pathrm to make sure re-sourcing your bashrc doesn't hose your environment.

    pathadd() {
        newelement=${1%/}
        if [ -d "$1" ] && ! echo "$PATH" | grep -E -q "(^|:)$newelement($|:)" ; then
            if [ "$2" = "after" ] ; then
                PATH="$PATH:$newelement"
            else
                PATH="$newelement:$PATH"
            fi
        fi
    }

    pathrm() {
        PATH="$(echo $PATH | sed -e "s;\(^\|:\)${1%/}\(:\|\$\);\1\2;g" -e 's;^:\|:$;;g' -e 's;::;:;g')"
    }
Post reply on HN