💡 Just harness Command-Line spells! 💫

danescher98

Danyiel Colin

Posted on June 14, 2023

💡 Just harness Command-Line spells! 💫

Do your genius command-line ideas wander aimlessly without a home? Fear not, for just is here to save the day! While tools like make excel at build automation, just takes a different approach by focusing on human-readable command execution. In this tutorial, we'll use it to set up a workflow to effortlessly capture, organize, and execute your oneliner commands in a clean and concise manner. Let's embark on this adventure and bring your command-line brilliance to life! 🌟

Justfile

To install just follow the official instructions.

The easiest way to install it is with gh (the Github CLI) and its extension install. Install them and then run the following to install just at $HOME/.local/bin:

gh install casey/just
Enter fullscreen mode Exit fullscreen mode

Then add the following lines to your shell config file to create an alias, so you don't mess with just if you use it for other projects.

# For Bash at ~/.bashrc
alias spell="just --justfile ~/Justfile"

# For Nushell at ~/.config/nushell/config.nu
alias spell = just --justfile ~/Justfile
Enter fullscreen mode Exit fullscreen mode

Now, a basic Justfile could look like this. Very similar to a Makefile with a simplified syntax.

set shell := ["bash", "-c"]

# Print the contents of `~/Justfile`
self:
    bat --plain -n --language=make ~/Justfile
Enter fullscreen mode Exit fullscreen mode

One of the best parts of using just is that the comments above each snippet generates its own documentation :D As you can see, it's possible to add arguments for each command.

just list

Suggested Justfile

The following is my personal Justfile where I keep my occasional and funny occurrences. I located it at $HOME/. Also, this example may give a fair idea of the possibilities of just, as argument handling, optional arguments and command pipelines. Use it as an inspiration! 🤓

set shell := ["bash", "-c"]

# Print contents of `~/Justfile`
self:
    bat --plain -n --language=make ~/Justfile

# Generate a random DNA sequence
dna:
    @cat /dev/urandom | tr -dc 'AGCT' | fold -w 32 | head -n 5

# Get a random quote with a playful display
fortune:
  @fortune -s | trans --brief en:es \
   | tee >(cowsay -f "$(ls /usr/share/cowsay/cows | sort -R | head -1)" | lolcat --random >/dev/tty) \
   | espeak -v spanish -k20

# As tldr, gives a summarized man-page
cheats CMD:
    @curl -sS cheat.sh/{{CMD}} | bat --style=plain

# Returns all files called when CMD is executed
usefile CMD:
  strace {{CMD}} 2>&1 | grep "^openat.*$HOME" | sed 's/.*"\(.*\)".*/\1/'

# Search through the history with fzf
dejavu WORD:
  @# @nushell -c "history" -m light | awk '{$1=""}1' \# Replace the below line if you use Nushell
  @history | awk '{$1=""; print $0}' | sort | uniq \
    | fzf --tac --layout=reverse --height=40% +s -e -q "{{WORD}}"

# Use ChatGPT from the command line
gpt PROMPT:
  @podman run --rm \
    --env OPENAI_API_KEY="YOUR_PERSONAL_TOKEN" \
    --volume gpt-cache:/tmp/shell_gpt ghcr.io/ther1d/shell_gpt "{{PROMPT}}"
Enter fullscreen mode Exit fullscreen mode

Now you'll have a convenient place where to put all those pilgrim ideas

spell-b
spell-c
spell-d
spell-e

Conclusion

In the previous Justfile I've used a couple of commands that I encorage you to checkout ✨:

  • bat A cat(1) clone with wings
  • lolcat Rainbows and unicorns!
  • fzf A command-line fuzzy finder
  • espeak A speech synthesizer
  • podman A daemonless tool to manage containers

Hope you've enjoyed this tutorial. For further details the just official docs. Happy scripting!

💖 💪 🙅 🚩
danescher98
Danyiel Colin

Posted on June 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related