It can’t hurt to share some of you current helpers. Either someone else might get inspired by or in some cases you get suggestions how to make it better. Bash, Zsh, Fish or whatever shell script, command or alias (or abbreviation) you have, share here. Always fun to do so! I share mine in the comments, just like everybody else.

  • Ordoviz@lemmy.ml
    link
    fedilink
    arrow-up
    9
    ·
    12 hours ago

    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"
    }
    
    • thingsiplay@lemmy.mlOP
      link
      fedilink
      arrow-up
      2
      ·
      10 hours ago

      There is a bit to unpack here. I particularly like the m() function here, to search for specific options in a man page. The grep_browser() is quiet involved and need to test it out myself. Meanwhile here is an “old” function of mine related to man pages, just searching all available pages, not their content:

      woman

      #!/bin/env bash
      
      # Lookup section meaning in `man man`.
      sections='1,8,6,5,7'
      
      show_manual () {
          man -- "${1%% *}" 2> /dev/null
      }
      export -f show_manual
      
      if selection=$(man -k . --sections="${sections}" \
                     | sort -t ' ' -k 2,2 -k 1,1 \
                     | fzf -q "${*}" \
                           --cycle \
                           --border=none \
                           --bind change:first \
                           --bind tab:down \
                           --bind shift-tab:up \
                           --bind esc:cancel+clear-selection \
                           --tiebreak=begin,chunk,length \
                           --reverse \
                           --preview='show_manual {}' \
                           --preview-window=down:70%:wrap:border-rounded)
      then
          show_manual "${selection}"
      fi