Skip to content

Instantly share code, notes, and snippets.

@do-me
Last active October 10, 2024 11:03
Show Gist options
  • Save do-me/f6e9ef4b9d6932c60a462da5513e6d79 to your computer and use it in GitHub Desktop.
Save do-me/f6e9ef4b9d6932c60a462da5513e6d79 to your computer and use it in GitHub Desktop.
personal wiki with bash & zsh, searches a directory with .txt files. includes command for new note
wiki() {
# Combine arguments into a single string for multi-word search
search_string="$*"
# Perform case-insensitive grep search with the combined string
grep -Hni --color=always "$search_string" /Users/dome/work/wikifiles/*.txt | awk -F':' '
BEGIN {
prevfile=""
}
{
if ($1 != prevfile) {
print "\033[1;34m==============================\033[0m" # Blue separator line
print "\033[1;36mπŸ“„ File: \033[0m" $1 # File name in cyan
}
# Ensure only the first two fields (filename and line number) are split by colon
print " \033[1;33m➜ Line "$2":\033[0m " substr($0, index($0,$3)) # Print line content starting from $3 to avoid cutting URLs
prevfile=$1
}'
}
note() {
# Get the current date and time in the format YYYY-MM-DD_HH-MM-SS
local timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
# Define the directory where the note should be created
local directory="/Users/dome/work/wikifiles"
# Define the note filename with the timestamp and full path
local filepath="${directory}/${timestamp}.txt"
# Create the new file and open it with micro editor
touch "$filepath" && micro "$filepath"
}
# usage & example results
(base) ➜ ~ note # opens micro editor for new note, can be replaced with vim or nano
(base) ➜ ~ wiki pandoc # search results
==============================
πŸ“„ File: /Users/dome/work/wikifiles/Pandoc HTML to markdown.txt
➜ Line 1: pandoc input.html -t gfm -o output.md
==============================
πŸ“„ File: /Users/dome/work/wikifiles/Pandoc Word to Markdown.txt
➜ Line 1: pandoc -s hll.docx --reference-links -t markdown -o hll.md
==============================
πŸ“„ File: /Users/dome/work/wikifiles/markdown regex replace.txt
➜ Line 19: 1. mit pandoc zu md
➜ Line 24: siehe auch pandoc wiki file
@do-me
Copy link
Author

do-me commented Oct 9, 2024

For zsh shell. Add the function to ~/.zshrc and update the shell with source ~/.zshrc.

Results are nicely formatted with colors and clickable with the mouse when pressing CTRL

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment