Manage a Cheatsheet from the Commandline

Tags: commandline 

Contents

Introduction

I maintain a personalized cheat-sheet in a plain-text file with a fish shell script.

The script relies on Awk and fzf.

Updates (12th Feb, 2025):

  • Remove dependency on awk.
  • Use fzf --accept-nth option introduced in version 0.60.0.

The script

The code for cheat.fish script given below:

function cheat -d "Cheatsheet"
  if test -d "$HOME/RESOURCES"
    set file ~/RESOURCES/Lists/cheatsheet.txt
  else
    set file ~/storage/shared/Documents/COMPUTER/RESOURCES/Lists/cheatsheet.txt
  end

  set selected (
    cat $file | fzf \
      -d ' # ' \
      --accept-nth 1 \
      --header "Cheatsheet" \
      --preview="echo -n {1}" \
      --preview-window="up,25%:wrap:follow" \
      --query=(commandline) \
      --bind="ctrl-y:execute(echo -n {1} | fish_clipboard_copy)+abort" \
      --no-bold +m
  )

  # replace query with selected
  if test $status -eq 0
    commandline -r -- $(echo $selected)
  end

  commandline -f repaint
end

bind \e\ce cheat

Below is a little segment from the cheatsheet.txt file:

exiftool -d /dir/%Y-%m-%d "-directory<CreateDate" ./VID*.{mp4,jpg} # Move photos/videos to directory based on date created

mogrify -strip -quality 75 <image> # Compress image quality to 75%

ffmpeg -i <input> -c copy -an <output> # Remove audio

xrandr --output <connected-display> --brightness 0.5 # Change brightness

sed -E "s|([0-9]{2})/([0-9]{2})/([0-9]{4})|\3-\2-\1|" file # Change date format with sed (dd/mm/yyyy to yyyy-mm-dd)

Breakdown

The script first checks the $HOME directory structure, and assigns a variable for the cheat-sheet file path.

fzf -d ' # ' separates the command from the inline comment by using # as the delimeter. I include the comments to help search the command, both in the cheat-sheet file and in shell history.

I bind the script to CTRL-ALT-E for quick execution.

This is a simple script that you can tweak to suit your needs.

Comment on this post with email.

Return to Top of the page.
See all Posts.
Subscribe to RSS feed.