Skip to content

Latest commit

 

History

History
620 lines (459 loc) · 33.4 KB

CodeSnippets.md

File metadata and controls

620 lines (459 loc) · 33.4 KB

Code snippets

copy, paste, return

update='1'; IFS="$(printf ' \t\n|')"; IFS="${IFS%|}"; command clear; command clear
printf '                 .___       __\n __ ________   __\174 _\057____ _\057  \174_  ____\n\174  \174  \134____ \134 \057 __ \174\134__  \134\134   __\134\057 __ \134\n\174  \174  \057  \174_\076 \076 \057_\057 \174 \057 __ \134\174  \174 \134  ___\057\n\174____\057\174   __\057\134____ \174\050____  \057__\174  \134___  \076\n      \174__\174        \134\057     \134\057          \134\057\n a Lucas Larson production\n\n' && command sleep 1.0; unset PS4
printf '\n\360\237\223\241  verifying network connectivity'; command sleep 0.5; i='0'; while [ "${i-}" -lt "$(
printf '2 ^ 7 + 1\n' | command bc)" ]; do if [ "$((i / 3 % 2))" -eq '0' ]; then
printf '.'; else
printf '\b'; fi; i="$((i + 1))"; done; unset i
printf '\n\n'; set -u; { command ping -q -i 1 -c 1 -- one.one.one.one >/dev/null 2>&1 && command ping -q -i 1 -c 1 -- 8.8.8.8 >/dev/null 2>&1; } || {
printf 'No internet connection detected.\nAborting update.\n'; exit "${update:-1}"; }
printf '\360\237\215\272  checking for Homebrew installation...'; if command -v brew >/dev/null 2>&1; then
printf '  \342\234\205  found\n'
printf '\360\237\215\272  checking for Homebrew updates...\n'; command brew update; command brew upgrade --greedy; command brew upgrade --cask --greedy; command brew install --debug --verbose --include-test --HEAD --display-times --git -- git; command brew install --debug --verbose --include-test --HEAD --display-times --git -- mackup; command brew install --debug --verbose --include-test --HEAD --display-times --git -- shellcheck; command brew install --debug --verbose --include-test --HEAD --display-times --git -- shfmt; command brew install --debug --verbose --include-test --HEAD --display-times --git -- zsh; else
printf 'No Homebrew installation detected...\n\n'; fi
printf '\360\237\217\224  checking for Alpine Package Keeper installation...\n'; if command -v apk >/dev/null 2>&1; then
printf '\360\237\217\224  apk update...\n'; command apk update --progress --verbose --verbose
printf '\n\360\237\217\224  apk upgrade...\n'; command apk upgrade --update-cache --progress --verbose --verbose
printf '\n\360\237\217\224  apk fix...\n'; command apk fix --progress --verbose --verbose
printf '\n\360\237\217\224  apk verify...\n'; command apk verify --progress --verbose --verbose
printf '\360\237\217\224  apk verify complete...\n\n'; else
printf 'no Alpine Package Keeper installation detected...\n\n'; fi
printf 'checking access to Software Update...\n'; if command -v softwareupdate >/dev/null 2>&1; then
printf '\360\237\215\216  '; command softwareupdate --list --all --verbose; else
printf 'no Software Update installation detected...\n\n'; fi
printf 'checking for Xcode installation...\n'; if command -v xcrun >/dev/null 2>&1; then
printf 'removing unavailable device simulators...\n'; command xcrun simctl delete unavailable; else
printf 'no Xcode installation detected...\n\n'; fi
printf '\342\232\233\357\270\217  checking for Atom installation...\n'; if command -v apm >/dev/null 2>&1; then command apm upgrade --no-confirm; else
printf 'no Atom installation detected...\n\n'; fi
printf 'checking for Rust installation...\n'; if command -v rustup >/dev/null 2>&1; then command rustup update; else
printf 'no Rust installation detected...\n\n'; fi
printf 'checking for npm installation...\n'; if command -v npm >/dev/null 2>&1; then
printf '...and whether this device is can update Node quickly...\n'; if [ "$((${COLUMNS:-80} * ${LINES:-24}))" -ge "$((80 * 24))" ]; then while [ -n "$(command find -- "${HOME-}/.npm-packages" -type f ! -name "$(
printf "*\n*")" -name '.DS_Store' -print)" ]; do command find -- "${HOME-}/.npm-packages" -type f ! -name "$(
printf "*\n*")" -name '.DS_Store' -print -exec rm -v -- {} \;; done; command npm install npm --global; while [ -n "$(command find -- "${HOME-}/.npm-packages" -type f ! -name "$(
printf "*\n*")" -name '.DS_Store' -print)" ]; do command find -- "${HOME-}/.npm-packages" -type f ! -name "$(
printf "*\n*")" -name '.DS_Store' -print -exec rm -v -- {} \;; done; command npm update --global --verbose; else
printf 'skipping Node update...\n\n'; command sleep 1
printf 'to update Node later, run:\n\n'
printf '    npm install npm --global \046\046 \134\n'
printf '    npm update --global --verbose\n\n\n'; command sleep 3; fi; else
printf 'no npm installation detected...\n\n'; fi
printf 'checking for RubyGems installation...\n'; if command -v gem >/dev/null 2>&1; then
printf 'updating RubyGems...\n'; command gem update --system; gem update; if command bundle update >/dev/null 2>&1; then command bundle update 2>/dev/null; fi; if command bundle install >/dev/null 2>&1; then command bundle install --verbose 2>/dev/null; fi; if command bundle exec pod install >/dev/null 2>&1; then command bundle exec pod install --verbose 2>/dev/null; fi; if command pod repo update >/dev/null 2>&1; then command pod repo update; command pod repo update; fi; else
printf 'no RubyGems installation detected...\n\n'; fi; if command -v rbenv >/dev/null 2>&1; then command rbenv rehash; fi; if command -v c_rehash >/dev/null 2>&1; then command c_rehash; fi; if command -v python >/dev/null 2>&1; then
printf '\n\360\237\220\215  verifying Python\342\200\231s packager is up to date...\n'; command python -m pip install --upgrade --verbose --verbose --verbose -- pip
printf 'verifying pip installation...\n'; if command -v pip >/dev/null 2>&1; then
printf '\n\360\237\220\215  updating outdated Python packages...\n'; for package in $(command pip list --outdated --format freeze); do command pip install --upgrade --verbose --verbose --verbose -- "${package%%=*}"; done; unset package; fi
printf 'checking for pyenv installation...\n'; if command -v pyenv >/dev/null 2>&1; then
printf 'rehashing pyenv shims...\n'; command pyenv rehash; else
printf 'no pyenv installation detected...\n\n'; fi
printf 'checking for Conda installation...\n'; if command -v conda >/dev/null 2>&1; then command conda update --yes --all; else
printf 'no Conda installation detected...\n\n'; fi; fi; if command -v omz >/dev/null 2>&1; then { set +e; set +u; } 2>/dev/null; omz update 2>/dev/null; fi; { set +e; set +u; set +v; set +x; } 2>/dev/null; [ -r "${HOME-}/.${SHELL##*[-./]}rc" ] && . "${HOME-}/.${SHELL##*[-./]}rc"; if command -v rehash >/dev/null 2>&1; then rehash; fi; unset update
printf '\n\n\342%s\234\205  done\041\n\n' "${update-}"; builtin exec -l -- "${SHELL##*[-./]}" # 2021-11-30

detail

xcode-select --switch /Applications/Xcode.app || xcode-select --switch /Applications/Xcode-beta.app || xcode-select --install && \
xcrun simctl delete unavailable && # via \
brew update --debug --verbose && # via, via \
brew upgrade && \
brew upgrade --cask && # via, via \
brew install mackup --head && # 0.8.29 2020-06-06 \
mackup backup --force --root \
omz update && # via \
git clone --recurse-submodules --depth 1 --branch main --verbose --progress # via, via \
git submodule update --init --recursive && # via \
npm install npm --global && # viavia \
npm update --global --verbose && # 6.14.5 2020-05-04 \
apm upgrade --no-confirm && # via npm analogy \
gem update --system && #  3.1.4 2020-06-03 \
gem update && \
gem install bundler --pre && #  2.1.4 2020-01-05 \
gem install cocoapods --pre && #  1.9.3 2020-05-29 \
bundle update && # via \
bundle install --verbose && \
bundle exec pod install --verbose && \
pod repo update && pod repo update && \
pod install && \
pod update && # via \
rbenv rehash && pyenv rehash && \
python -m pip install --upgrade pip && # 20.1.1 2020-05-19 via, via \
pip list --outdated --format freeze \
    | grep --invert-match '^\-e' \
    | cut --delimiter = --fields 1 \
    | xargs -n1 pip install --upgrade && # via \
pip install --upgrade $(pip freeze | cut --delimiter '=' --fields 1) && # via \
pipenv shell && # via \
pipenv install --dev && # via \
rustup update && # 1.44.1 2020-06-18 \
brew install carthage --head && # 0.36.0 2020-09-18 \
carthage update --verbose --no-use-binaries && # via \
brew install swiftgen --head && #  6.2.0 2019-01-29 \
swiftgen && \
brew install swiftlint --head && # 0.40.3 2020-09-22 \
swiftlint autocorrect && \
git gc && \
# gradle build --refresh-dependencies --warning-mode all && # via \
. ~/.${SHELL##*/}rc && \
printf '\n\n\xe2%s\x9c\x85 done\x21\n\n' "$update" && # viavia, via \
# note successful finish before restarting the shell \
exec -l "${SHELL##*/}" # via

Mackup

add

manual

lists
applications

Track changes to which applications are installed without syncing them. The instructions are Bash-compatible and refer to this document for instructions on regenerating the list.

saveApplications=1 && mkdir --parents "$DOTFILES" && mkdir --parents /Applications && cd /Applications && filename="$DOTFILES"/:Applications && touch "$filename" && pwd > "$filename" && date '+%Y-%m-%d' >> "$filename" && printf '—————————————\n' >> "$filename" && ls -F1 >> "$filename" && cd "$DOTFILES" && mackup backup --force --root && \
git fetch --all && git submodule update --init --recursive --remote && git diff "$filename" && unset filename && saveApplications=$filename && printf '\n\n\xe2%s\x9c%s\x85 done!\n\n' "$filename" "$saveApplications"

On Alpine Linux, generate a list of installed packages with:
apk -vv info|sort # via

Homebrew
listBrew="$DOTFILES/brew list --formula --verbose" && touch "$listBrew" && printf 'brew list --formula --verbose\n' > "$listBrew" && date '+%Y-%m-%d' >> "$listBrew" && printf '—————————————————————————————\n' >> "$listBrew" && brew list --formula --verbose >> "$listBrew" && unset listBrew && printf '\n\n\xe2%s\x9c\x85 done\x21\n\n' "$listBrew"
Cask
listBrewCask="$DOTFILES"/brew\ cask\ list && touch "$listBrewCask" && printf 'brew cask list\n—————————————\n' > "$listBrewCask" && brew cask list >> "$listBrewCask" && unset listBrewCask && printf '\n\n\xe2%s\x9c\x85 done\x21\n\n' "$listBrewCask"
MANPATH
saveManpath=1 && mkdir --parents "$DOTFILES" && filename="$DOTFILES"/manpath && touch "$filename" && printf '# \x24manpath\xe2\x80\x99s contents\n# ' > "$filename" && date '+%Y-%m-%d' >> "$filename" && printf '# ———————————————————————\n' >> "$filename" && <<<${(F)manpath} >> "$filename" && cd "$DOTFILES" && \
mackup backup --force --root && git fetch --all --verbose && git submodule update --init --recursive && git status && git diff "$filename" && unset filename && saveManpath="$filename" && printf '\n\n\xe2%s\x9c%s\x85 done!\n\n' "$filename" "$saveManpath"
man pages

Definintions of the numbers that follow man commands.

  1. Executable programs or shell commands
  2. System calls (functions provided by the kernel)
  3. Library calls (functions within program libraries)
  4. Special files (usually found in /dev)
  5. File formats and conventions, /etc/passwd for example
  6. Games
  7. Miscellaneous (including macro packages and conventions)
  8. System administration commands (usually only for root)

via

pip packages
pip list && mkdir --parents "$DOTFILES" && printf 'pip packages installed ' > "$DOTFILES"/pip && date '+%Y-%m-%d' >> "$DOTFILES"/pip && printf '—————————————————————————————————\n' >> "$DOTFILES"/pip && pip list >> "$DOTFILES"/pip && cd "$DOTFILES" && \
mackup backup --force --root && git fetch --all --verbose && git submodule update --init --recursive && git status && git diff pip && printf '\n\n\xe2\x9c\x85 done\x21\n\n'

apk

testing

apk add foo # unavailable? \
# then try \
apk add foo@testing # via

list everything recursively in a directory

with full paths

find -- . # via

and metadata

find -- . -ls

lines, words, characters

in, for example, a C++ project directory, measuring only .cpp and .hpp files

find -- * \( -name '*.cpp' -or -name '*.hpp' \) -print0 | xargs -0 wc | sort -n -r # via

search

grep

search for the word “example” inside the current directory which is “.”
grep --ignore-case --line-number --recursive 'example' .

locate all

for example, locate all JPEG files
locate -i *.jpg # via; see also § grep

PATH

entries

macOS

<<<${(F)path} # via

Linux

pathSave=1 && mkdir --parents "$DOTFILES" && cd "$DOTFILES" && printf 'path\n' > path && date '+%Y-%m-%d' >> path && printf 'automagically generated' >> path && printf '\n———————————————————————\n' >> path && <<<${(F)path} >> path && git fetch --all --verbose && git submodule update --init --recursive && git status && git diff path && printf '\n\n\xe2\x9c\x85 done\x21\n\n' && pathSave=0

text editing

export output

printf 'First Name\n' > ExampleFileWithGivenName.txt # create a text file with “First Name” and a new line
printf 'Other First Name\n' > ExampleFileWithGivenName.txt # the “>overwrites the existing file
printf "Last Name\n" >> ExampleFileWithGivenName.txt # the “>>appends to the existing document

sort

env > example.txt # save an unordered list of env variables
env | sort > example.txt # via save the variables in an alphabetically ordered list

EOL and EOF encoding

find (?<![\r\n])$(?![\r\n]) # via
replace \r\n

make invisible

chflags -hvv hidden example.txt
-h for symbolic links, if applicable, but not their targets
-v₁ for verbose
-v₂ for printing the old and new flags in octal to stdout

create an alias

ln --symbolic file shortcut # via
(just like cp existing new)

launch services

reset

remove bogus entries from Finder’s “Open With” menu (via)
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed -r -domain local -domain system -domain user && killall Finder

repair website disk permissions

find -- /path/to/your/wordpress -type d -exec chmod 755 {} \; && \
find -- /path/to/your/wordpress -type f -exec chmod 644 {} \; # via

date modified modify

touch -t 2003040500 file.txt # date modified → 2020-03-04 5:00am

C, C++

flags

-Wall -Wextra -pedantic
#ifdef __APPLE__
    -Weverything <!-- do not use (via) -->
#endif
-Woverriding-method-mismatch -Weffc++ -Wcall-to-pure-virtual-from-ctor-dtor -Wmemset-transposed-args -Wreturn-std-move -Wsizeof-pointer-div -Wdefaulted-function-deleted # via
-lstdc++ # via but this might – or might not – be helpful on macOS using gcc or g++

C++ features before wide support

for example, C++17’s <filesystem>
-lstdc++fs

apply clang-format recursively

via
program="clang-format" && if ! command -v "$program" >/dev/null 2>&1; then printf '\nerror: no %s installation detected;\nskipping code\xc2\xa0formatting\n' "$program" && return 1; fi; \ clangformat=${1:-2} && printf '\n%s\n\n' "$("$program" --version)" && sleep 1 && printf 'applying %s to all applicable files in %s...\n' "$program" "${PWD##*/}" && sleep 1 && printf 'setting \x60IndentWidth\x60 to %s\n\n\n' "$clangformat" && sleep 1 && find -- * -type f \( \ -iname '*.c' -or -iname '*.c++' -or -iname '*.cc' -or -iname '*.cp' -or -iname '*.cpp' -or -iname '*.cxx' -or -iname '*.h' -or -iname '*.h++' -or -iname '*.hh' -or -iname '*.hp' -or -iname '*.hpp' -or -iname '*.hxx' -or \ -iname '*.i' -or -iname '*.ii' -or -iname '*.java' -or -iname '*.js' -or -iname '*.m' -or -iname '*.mi' -or -iname '*.mii' -or -iname '*.mm' -or -iname '*.tcc' -or -iname '*.tpp' -or -iname '*.txx' \) -exec "$program" -i -style "{IndentWidth: $clangformat}" --verbose {} \; && printf '\n\n\xe2\x9c\x85 done\x21\n\n'

run cpplint recursively

cpplint --verbose=0 --linelength=79 --recursive --extensions=c++,cc,cp,cpp,cxx,h,h++,hh,hp,hpp,hxx . >> cpplint.txt

run cppcheck recursively

cppcheck --force -I $CPATH . >> cppcheck.txt

compile all files of type .𝑥 in a directory

C

via
gcc -std=c89 --verbose -save-temps -v -Wall -Wextra -pedantic $(find -- . -type f -regex '*.c')

C++

g++ -std=c++2a --verbose -Wall -Wextra -pedantic -save-temps -v -pthread -fgnu-tm -lm -latomic -lstdc++ $(find -- . -iname '*.cpp')

Clang

clang++ -std=c++2a --verbose -Wall -Wextra -pedantic -v -lm -lstdc++ -pthread -save-temps $(find -- . -iname '*.cpp')

Gatekeeper

do not disable it, because that would allow you to install any software, even if unsigned, even if malicious:
sudo spctl --master-disable # via

Git

init via GitHub

git push --set-upstream git@github.com:LucasLarson/$(git rev-parse --show-toplevel | xargs basename).git $(git rev-parse --abbrev-ref HEAD)

git add

via

content to add git command
modified files only `git add --updated
everything except deleted files git add .
everything `git add --all

git diff

more detailed git diff and how I once found an LF‑to‑CRLF‑only difference
git diff --raw

git commit

with subject and body

git commit --message 'subject' --message 'body' # via

in the past

to backdate a commit:
GIT_TIME='2000-01-02T15:04:05 -0500' GIT_AUTHOR_DATE=$GIT_TIME GIT_COMMITTER_DATE=$GIT_TIME git commit --message 'add modifications made at 3:04:05pm EST on January 2, 2000' # via

git config

editor

Vim
git config --global core.editor /usr/local/bin/vim
Atom via
git config --global core.editor "atom --wait"

git tag

git tag v𝑖.𝑗.𝑘 # where 𝑖, 𝑗, and 𝑘 are non-negative integers representing semver (semantic versioning) major, minor, and patch releases
git push origin v𝑖.𝑗.𝑘 # push the unannotated tag via

Numbers

Affixes

Definition Prefix Suffix
binary 0b𝑛 𝑛₂
octal 0o𝑛 𝑛₈
decimal 0d𝑛 𝑛₁₀
hexadecimal 0x𝑛 𝑛₁₆

Operating system

Identify

command -v uname >/dev/null 2>&1 && \
printf '\n\x60uname -a\x60:\n%s\n\n' "$(uname -a)"; \
command -v sw_vers >/dev/null 2>&1 && # via \
printf '\n\x60sw_vers\x60:\n%s\n\n' "$(sw_vers)"; \
command -v lsb_release >/dev/null 2>&1 && # via \
printf '\n\x60lsb_release --all\x60:\n%s\n\n' "$(lsb_release --all)"; \
[ -r /etc/os-release ] && # via \
printf '\x60cat /etc/os-release\x60:\n%s\n\n' "$(cat /etc/os-release)"

parameter expansion

via

parameter Set and Not Null parameter Set But Null parameter Unset
${parameter:-word} substitute parameter substitute word substitute word
${parameter-word} substitute parameter substitute null substitute word
${parameter:=word} substitute parameter assign word assign word
${parameter=word} substitute parameter substitute null assign word
${parameter:?word} substitute parameter error, exit error, exit
${parameter?word} substitute parameter substitute null error, exit
${parameter:+word} substitute word substitute null substitute null
${parameter+word} substitute word substitute word substitute null

redirection

via

syntax meaning POSIX compliance
>file redirect stdout to file
1>file redirect stdout to file
2>file redirect stderr to file
>file 2>&1 redirect stdout and stderr to file
&>file redirect stdout and stderr to file (Bash) 🚫
>>file 2>&1 append stdout and stderr to file
&>>/file append stdout and stderr to file (Bash) 🚫

rename files

brew install --upgrade rename && # via \
rename --dry-run --verbose --subst searchword replaceword *

split enormous files into something manageable

if your example.csv has too many rows (via)
split --lines 2000 example.csv; for i in *; do mv "$i" "$i.csv"; done

SSH

ssh username@example.com

ls on Windows

dir # via

variables

$PWD # the name of the current directory and its entire path ${PWD##*/} # via the name of only the current directory

wget

wgetServer='example.com' && (wget --mirror --continue --verbose --append-output=$wgetServer.log --execute robots=off --restrict-file-names=nocontrol --timestamping --debug --recursive --show-progress http://$wgetServer || wget --continue http://$wgetServer) && unset wgetServer || unset wgetServer

WiFi

Windows password

netsh wlan show profile WiFi-name key=clear # via

macOS password

security find-generic-password -wa ExampleNetwork # via

Xcode

signing

PRODUCT_BUNDLE_IDENTIFIER = net.LucasLarson.$(PRODUCT_NAME:rfc1034identifier);
PRODUCT_NAME = $(PROJECT_NAME);
DEVELOPMENT_TEAM = Z25963JBNP;
DevelopmentTeam = Z25963JBNP;

Zsh

.zshrc

troubleshooting

Add zmodload zsh/zprof at the top of ~/.zshrc and zprof at the bottom of it. Restart restart to get a profile of startup time usage. via

housekeeping

Docker

docker system prune --all # via

Flutter

cd ~/Code/Flutter && git pull && flutter upgrade && flutter precache && flutter doctor --verbose

Homebrew

brew doctor --debug --verbose && \
brew cleanup --debug --verbose && # via \
brew audit cask --strict --token-conflicts

npm

npm doctor && # creates empty node_modules directories \
find -- node_modules -empty -type d -delete # deletes them via

RubyGems

gem cleanup --verbose

Xcode, JetBrains, Carthage

trashDeveloper=1 && sleep 0.25 && \
mkdir --parents "${HOME}/Library/Developer/Xcode/DerivedData" && mv "${HOME}/Library/Developer/Xcode/DerivedData" "${HOME}/.Trash/Xcode-${RANDOM}" && \
mkdir --parents "${HOME}/Library/Developer/Xcode/UserData/IB Support" && mv "${HOME}/Library/Developer/Xcode/UserData/IB Support" "${HOME}/.Trash/Xcode⁄UserData⁄IB Support-${RANDOM}" && \
mkdir --parents "${HOME}/Library/Caches/JetBrains" && mv "${HOME}/Library/Caches/JetBrains" "${HOME}/.Trash/JetBrains-${RANDOM}" && \
mkdir --parents "${HOME}/Library/Caches/org.carthage.CarthageKit/DerivedData" && mv "${HOME}/Library/Caches/org.carthage.CarthageKit/DerivedData" "${HOME}/.Trash/Carthage-${RANDOM}" && \
unset trashDeveloper && printf '\n\n\xf0\x9f%s\x9a\xae data successfully trashed\n\n' "${trashDeveloper:-"$(printf '')"}"

delete

rm -ri /directory # via
rm  -i /document.txt # -i stands for interactive

empty directories

make a list of empty folders inside and beneath current directory . (via)
find -- . -type d -empty -print
if satisfied with the results being lost and gone forever, execute:
find -- . -type d -empty -delete

compare two folders

diff --recursive /path/to/folder1 /path/to/folder2 # via

purge memory cache

sudo purge # via