Skip to content

Instantly share code, notes, and snippets.

@alfeugds
Last active January 9, 2023 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfeugds/3a6be772738f2f60e58972ddd594a044 to your computer and use it in GitHub Desktop.
Save alfeugds/3a6be772738f2f60e58972ddd594a044 to your computer and use it in GitHub Desktop.
Terminal Reference file lookup

Terminal Reference file lookup

Simple bash script to search a text content from a reference text file.

Useful if you keep a bunch of information in a text file and want them to be easily accessible through your terminal. E.g. shell commands, useful links, commonly used node tasks.

Setup:

Create an executable file:

mkdir ~/bin
vim ~/bin/ref

Paste the content from ref.sh into the new ~/bin/ref file

Make ~/bin/ref executable

chmod +x ~/bin/ref

Add the following entry in your ~/.bashrc file:

PATH="$PATH:$HOME/bin"

Editing the reference text file

In a new terminal, run

ref

It will open the reference file in a text editor. You can edit the location of the reference file by changing the REF_FILE variable value in ~/bin/ref.

You can add information in any format you want. The search logic will print all occurrences case-insensitive.

However if you want an optimized search result, you can format your content in the following way:

search term >> desired value

Examples:

local smtp email for dev >> docker run -p 3000:80 -p 2525:25 -d --name smtpdev rnwood/smtp4dev
eslint vscode reference >> https://github.com/microsoft/vscode-eslint/issues/733

Searching in terminal

Run ref <search term>, e.g.

ref email

This will print:

all search results:
local smtp email for dev >> docker run -p 3000:80 -p 2525:25 -d --name smtpdev rnwood/smtp4dev

search:

local smtp email for dev

commands:

docker run -p 3000:80 -p 2525:25 -d --name smtpdev rnwood/smtp4dev
REF_FILE=~/tools/reference.txt
search_parameter=$@
if [ -z "$search_parameter" ]; then
# open the reference file in a text editor
code $REF_FILE
# you can also use vim, nano, gedit, etc
else
echo "all search results:"
grep --color -E -i "$search_parameter" $REF_FILE
echo ""
SEARCH=$(grep -i "$search_parameter" $REF_FILE | awk -v FS='>>' '{ print $1 }' | awk '{$1=$1;print}')
OUTPUT=$(grep -i "$search_parameter" $REF_FILE | awk -v FS='>>' '{ print $2 }' | awk '{$1=$1;print}')
echo "search:"
echo ""
echo "$SEARCH"
echo ""
echo "commands:"
echo ""
echo "$OUTPUT"
echo ""
# echo "copied last one to clipboard using xclip"
# echo "$OUTPUT" | tail -1 | xclip -selection clipboard
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment