• 1 Post
  • 4 Comments
Joined 7 years ago
cake
Cake day: January 31st, 2020

help-circle
  • These are my favorite Bash functions:

    #!/bin/bash
    
    alias ..="cd .."
    
    r() {
        # cd to git root
        cd "$(git rev-parse --show-toplevel 2>/dev/null)"
    }
    
    mkcd() { 
        mkdir -p "$1" && cd "$1"
    }
    
    pwc() {
        # copy $PWD into Wayland clipboard with shell quoting
        printf '%q' "$PWD" | wl-copy
    }
    
    tmpdiff() {
        old="$(wl-paste)"
        read -n1 -srp "Copy new content into your clipboard, then press any key to diff it"
        diff <(echo "$old") <(wl-paste)
    }
    
    m() {
        # Opens man page and searches for an option.
        # Example: m cat -v
        MANPAGER="less -p '^\s+(?-i)$2'" man "$1"
    }
    
    grep_browser() {
        # Search file contents with ripgrep, then narrow it down using fzf with bat preview
        filename=$(rg --smart-case \
                --line-number \
                --with-filename \
                --color=always \
                --field-match-separator $'\u00a0' \
                -- "$@" |
            fzf --delimiter $'\u00a0' \
                --ansi \
                --preview "bat --color=always {1} --highlight-line {2}" \
                --preview-window 'up,~8,+{2}-5' |
            rg --only-matching '(.+)\u00a0(\d+)' --replace '$1:$2' --color=never)
    
        [ -n "$filename" ] && "$VISUAL" "$filename"
    }
    



  • #!/bin/sh
    # Select a file with fzf from a database sorted by frecency and open it using
    # xdg-open. frece can be found at https://github.com/YodaEmbedding/frece
    
    DB_FILE=${FRECE_FILES_DB:-$HOME/.cache/frecent-files.csv}
    item=$(frece print "$DB_FILE" | fzf --tiebreak=index --scheme=path)
    
    [ -z "$item" ] &amp;&amp; exit 1
    frece increment "$DB_FILE" "$item"
    
    xdg-open "$item"
    
    #!/bin/sh
    # Update frece database
    
    DB_FILE=${FRECE_FILES_DB:-$HOME/.cache/frecent-files.csv}
    tmp_file=$(mktemp)
    fd -H . ~ > "$tmp_file"  # use ~/.fdignore file to exclude certain dirs
    frece update "$DB_FILE" "$tmp_file" --purge-old
    rm "$tmp_file"