60 votes

Scripts I wrote that I use all the time

14 comments

  1. xk3
    Link
    Found a couple neat ideas that I'm going to steal--thx for sharing; but I must mention: Should probably be pgrep and pkill Should probably be setsid -f or at least use the double subshell trick...

    Found a couple neat ideas that I'm going to steal--thx for sharing; but I must mention:

    running foo is like ps aux | grep foo

    Should probably be pgrep and pkill

    bb my_command is like my_command & but it really really runs it in the background

    Should probably be setsid -f or at least use the double subshell trick (ie. ((command &) &) which immediately backgrounds the process and orphans it, effectively daemonizing it)

    7 votes
  2. ShroudedScribe
    Link
    A lot of cool ideas here, I especially like nato bar - I feel there's been a handful of occasions lately where I'm trying to read a string of letters and numbers verbally and it's been difficult....

    A lot of cool ideas here, I especially like nato bar - I feel there's been a handful of occasions lately where I'm trying to read a string of letters and numbers verbally and it's been difficult. It may just be a better web app for my use case though - since it's often a phone call issue, having it on my phone would be the most beneficial.

    5 votes
  3. [7]
    Narry
    Link
    I tend to string commands together that I use all the time into aliases in zsh. Some of my favorites: # common ways I style ls commands alias lsc="ls --color" alias lsa="ls -lha --color" alias...

    I tend to string commands together that I use all the time into aliases in zsh. Some of my favorites:

    # common ways I style ls commands 
    alias lsc="ls --color"
    alias lsa="ls -lha --color"
    alias lsd="ls -d --color */"
    alias lsall="ls -latrh --color */"
    
    # I need cls like I need cd and ls
    alias cls="clear"
    
    # If you connect via SSH it'll ask for a fingerprint of your machine on each new client.
    # I got tired of looking up and copying this command
    alias fingerprint="ssh-keygen -E md5 -lf <(ssh-keyscan localhost 2>/dev/null)"
    
    # Technically only . ~/zshrc is doing anything useful.
    #The rest is
    # 1) an anonymous function that echos what I'm doing
    # 2) does what I said I'd do
    # 3) then confirms we're still in zsh.
    alias src="() { echo 'Reloading ZSH'; . ~/.zshrc; echo $0 }"
    
    # I honestly forgot that these aren't real commands I use them so dang much.
    alias home="cd ~/"
    alias whichenv="echo $VIRTUAL_ENV"
    

    What this does is it it updates homebrew, upgrades any packages that need upgrading, and then it clears the cached downloads. (Edited to take out the mkcd command as it... doesn't work.)

    3 votes
    1. [2]
      jackson
      Link Parent
      re home, you can get the same effect by just running cd!

      re home, you can get the same effect by just running cd!

      10 votes
      1. Narry
        Link Parent
        I knew there had to be something like that out there. Thanks!

        I knew there had to be something like that out there. Thanks!

        1 vote
    2. [2]
      Boojum
      Link Parent
      For things like your lsc, I prefer to just alias ls to itself along with some others where I pretty much never want the vanilla defaults. alias ls="ls --color=auto" alias grep="grep --color=auto"...

      For things like your lsc, I prefer to just alias ls to itself along with some others where I pretty much never want the vanilla defaults.

      alias ls="ls --color=auto"
      alias grep="grep --color=auto"
      alias fgrep="fgrep --color=auto"
      alias egrep="egrep --color=auto"
      alias less="less -i"
      alias du="du -hc"
      alias ps="ps -Heo pid,pcpu,user,rss:7,args"
      alias strings="strings -a"
      alias hexdump="hexdump -C"
      

      For the very rare occasions where I actually do want the default behavior, prefixing the command with backslash disables alias expansion. E.g., \ls. This way, I give my common case the shorter name, and the uncommon case the longer name.

      4 votes
      1. Narry
        Link Parent
        Nice! I’ll probably incorporate some of that into my zshrc tonight after work! Thank you.

        Nice! I’ll probably incorporate some of that into my zshrc tonight after work! Thank you.

    3. [2]
      xk3
      Link Parent
      One alternative you could do is SSH Art 🎨😎 cat .ssh/config VisualHostKey=yes

      I got tired of looking up the fingerprint

      One alternative you could do is SSH Art 🎨😎

      cat .ssh/config
      VisualHostKey=yes
      
      1 vote
      1. Narry
        Link Parent
        There’s always something new and interesting to learn about POSIX. Very cool! Thanks!

        There’s always something new and interesting to learn about POSIX. Very cool! Thanks!

  4. DaveJarvis
    Link
    $ alias alias clip='xsel --clipboard --input < ' alias cls='clear' alias dj='cd $HOME/dev/java/keenwrite' alias gc='git commit -a -m' alias gp='git push' alias rd='rmdir' The dj alias points to...
    $ alias
    alias clip='xsel --clipboard --input < '
    alias cls='clear'
    alias dj='cd $HOME/dev/java/keenwrite'
    alias gc='git commit -a -m'
    alias gp='git push'
    alias rd='rmdir'
    

    The dj alias points to whatever project has my attention. The clip alias will copy the contents of a file to the clipboard, ready for pasting.

    Scripts of note:

    2 votes
  5. Boojum
    (edited )
    Link
    I'm definitely going to have steal some of these. (Both from the article, from this thread, and from the HN thread.) Anyway, in the spirit of sharing here are some Zsh aliases and functions of my...

    I'm definitely going to have steal some of these. (Both from the article, from this thread, and from the HN thread.)

    Anyway, in the spirit of sharing here are some Zsh aliases and functions of my own:

    alias rshare="chmod -R go=u-w"
    alias runshare="chmod -R go="
    alias runexec="chmod -R a-x+X"
    

    rshare recurses through a directory tree and for each file or directory copies my user permissions, minus write access, to group and other. I usually run locked down with umask 077, so rshare makes things accessible to others. runshare revokes those perms. runexec is useful for cleaning up directory trees copied from Windows filesystems where the executable bit comes set on everything.

    function go() { local i; for i ($@); do xdg-open $i; done }
    

    I use this one constantly. It opens up a file or list of files with whatever desktop program is assigned to them. Or for directories, it shows me the contents in the desktop file browser, etc. (Regarding the name, I was using a version of this alias long before Golang existed, damnit!)

    alias ec="emacsclient -n"
    function cde() { local d=`emacsclient -e "(buffer-local-value 'default-directory (window-buffer))"`; cd ${~d:1:-1} }
    

    Quick edits in Emacs via the client. I have Emacs set to launch the server on startup, and ec just tells that instance to open whatever files I give it and then immediately returns. It's useful when I'm just cding around in the terminal and want to pop a file into my Emacs window. cde kind of goes the opposite direction and tries to cd me into the directory holding the active buffer's file.

    alias opng="oxipng -o max -Z -v -v"
    alias toimg="pygmentize -Ofont_size=16,line_number_chars=3,font_name=Cascadia\ Mono,style=borland -o"
    function pdf2png() { gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r600 -dEPSCrop -dUseCropBox -dFirstPage=1 -dLastPage=1 -sOutputFile=$2 $1 }
    

    Some image utilities: opng crunches down PNG files as much as possible, toimg renders stdin or a text/source file to a syntax highlighted image, pdf2png renders the pages of a PDF to images.

    alias pdiff="git --no-pager diff --no-index --patience --color=auto -U15"
    function attach() { gdb --pid `ps -o pid= -C $@` ;}
    function alarm() { perl -e 'alarm shift; exec @ARGV' "$@" ;}
    function countdn() { local c; for c in {$1..1}; do echo -ne "  $c... \\r"; sleep 1; done }
    function vbreak() { eval `resize`; echo "\033[1;95m\n"; for r in {1..3}; do for c in {1..$COLUMNS}; do echo -n \#; done; done; date; echo "\n" }
    function echob() { echo "\033[1;31m$@\033[m" }
    

    And finally a grab bag: pdiff uses Git's patience diff on two arbitrary files (which may be outside of repos), attach tries to attach the GDB debugger to a running process by name so I don't have to look up the PID, alarm takes a time in seconds and a command to run and kills the command after the time is up, countdn takes a time in seconds and gives me a little count down (useful when I want to delay or stagger the start times of some commands to run unattended), vbreak shows a honking big bold magenta divider bar with timestamp that I can't miss in the scrollback buffer, and echob just echoes some text in bold red.

    2 votes
  6. tomf
    Link
    I use this a lot to create a directory and enter it in one shot mkcd() { mkdir $1 && cd $_ } in skhd I have skip ahead and skip back in big and small for Kodi. Its on a different box with a screen...

    I use this a lot to create a directory and enter it in one shot

    mkcd() {
      mkdir $1 && cd $_
    }
    

    in skhd I have skip ahead and skip back in big and small for Kodi. Its on a different box with a screen just to my left -- so instead of moving my hands for the remote or the remapped keypad, I can use hyper + vim keys to skip ahead etc.

    hyper - k : curl -s -u "username:password" -X POST http://pear:8080/jsonrpc -H "Content-Type: application/json" -d '{"jsonrpc
    ":"2.0","id":1,"method":"Player.Seek","params":{"playerid":1,"value":{"step":"smallbackward"}}}
    

    I have similar key combos to control Hue lights, too -- but that calls a proper script.

    1 vote
  7. text_garden
    Link
    Good article, and good thread to keep an eye on! I use the following oneliner to open a document of notes for the day: #!/bin/bash exec $EDITOR $HOME/docs/notes/$(date --iso-8601).txt I'll warn...

    Good article, and good thread to keep an eye on!

    I use the following oneliner to open a document of notes for the day:

    #!/bin/bash
    exec $EDITOR $HOME/docs/notes/$(date --iso-8601).txt
    

    I'll warn that it's for GNU date and I don't know that *BSD or busybox implement --iso-8601.

    I use this to browse and search my music library with fzf and play back albums using audacious:

    #!/bin/bash
    cd $HOME/storage/musik/shared
    case "$1" in
            "") exec fzf --walker=dir --bind "enter:execute($0 play {})" ;;
            play) audacious "$2" & disown ;;
    esac
    

    The library is organized in an <artist name>/<album name> hierarchy, and I tend to listen to music in units of albums. Consequently I don't use the library features of audacious.

    I also use the following script to synchronize the music library to VLC on my phone (an Iphone) using ifuse and rsync:

    #!/bin/bash
    set -e
    mkdir ~/iphone-syncmusic
    trap "rmdir ~/iphone-syncmusic" EXIT
    idevicepair pair
    ifuse --documents "org.videolan.vlc-ios" ~/iphone-syncmusic
    rsync -Lrvu --progress --delete ~/storage/musik/shared/ ~/iphone-syncmusic/shared/
    umount ~/iphone-syncmusic
    
    1 vote
  8. first-must-burn
    Link
    I have a gitviz/gitvizl that visualizes the commit tree in the terminal with nice coloring. The regular variant just shows the head of every branch and the merges, the l suffix shows every commit....

    I have a gitviz/gitvizl that visualizes the commit tree in the terminal with nice coloring. The regular variant just shows the head of every branch and the merges, the l suffix shows every commit.

    For a Particular project or environment, usually make a cgrep (code grep) that aliases grep with exclusions for folders I don't want to search like .git, node_modules, build, dist, coverage, etc.

    I wrote a program called ask that passes the rest of the line to an llm as a prompt and asks it to return a command and only a command, then offers to cool it to the clipboard. It has a few optimizations, like if you mention git/commit/merge it will include the output of git status, and if you mention a file in the current directory it will inckude the first 500kb of it. Though I guess it's not a script (go compiled binary).

    I used to have a script called slackit that I could append to long running tasks to get a notification that they finished.