Manage a Cheatsheet with fish, fzf, and awk.

Tags: fish fzf awk plain-text 

Contents

Introduction

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

The script relies on GNU Awk and fzf.

The script

The code for my 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 (
  awk '!/^($|#)/' $file | fzf -d ' # ' \
    --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.

The script uses awk to parse the cheat-sheet file and then pipes the output to fzf.

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

Sidenotes:

  • awk '!/^($|#)/' ignores all blank lines and lines starting with a ’#‘. This helps me keep the file well formatted and well documented with comments.
  • fzf -d ' # ' separate the command from the inline comment. I include the comments to help search the command, both in the cheat-sheet file and in shell history.

Conclusion

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

The file is available in my dotfiles repo.

Reply to this post with email.

Go to Top
Return to Posts
Stay updated with RSS