If you are a power user, you probably have some terminal commands that you like to use. Post them here. Follow the format outline below, either one command per reply or grouped by similar function to make browsing easier, and if you find a bug, respond to the message with the specific command. I’ll start with a few examples.
EDIT: Okay, maybe more than a few…
EDIT2: Trust but verify! Read the MAN page for the command to make sure you won’t do something irreparable.
Group Title only if grouping multiple commands (# for Heading 1)
Command Title (## for Heading 2)
Further explanation in necessary (no formatting)
Terminal command. Use > to create block text (forum does not seem to support 4 spaces for code)
Rebuild LaunchServices to remove outdated apps from Open With…
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user
In my self built automation system I use that a lot, however I am looking at moving from belt and suspenders stuff into keyboard maestro which would handle that for me
I often need to compare two lists of things either to find what is missing or remaining or what is common between them. I use the comm command for that. The following examples all use the same two text files (list1.txt and list2.txt) in the same order.
I tend to find it faster to search and move through my files from the terminal.
Z learns from your history
z Notes
Z
z
Tracks the most used directories and enables quickly navigating to them using string or regex patterns.
- Go to a directory that contains "foo" in the name:
z foo
- Go to a directory that contains "foo" and then "bar":
z foo bar
- Go to the highest-ranked directory matching "foo":
z -r foo
- Go to the most recently accessed directory matching "foo":
z -t foo
- List all directories in `z`'s database matching "foo":
z -l foo
- Remove the current directory from `z`'s database:
z -x .
FZF fuzzy search with preview
fzf --preview=“pygmentize -g {}” java
fzf
fzf
Command line fuzzy finder.
- Start finder on all files from arbitrary locations:
find path/to/search -type f | fzf
- Start finder on running processes:
ps aux | fzf
- Select multiple files with `Shift + Tab` and write toa file:
find path/to/search_files -type f | fzf -m > filename
- Start finder with a given query:
fzf -q "query"
- Start finder on entries that start with core and end with either go, rb, or py:
fzf -q "^core go$ | rb$ | py$"
- Start finder on entries that not match pyc and match exactly travis:
fzf -q "!pyc 'travis"
Ripgrep super fast regex searcher in files and directories
rg -t py coderJSON
rg
ripgrep
A fast command-line search tool.
- Recursively search the current directory for a regex pattern:
rg pattern
- Search for pattern including all .gitignored and hidden files:
rg -uu pattern
- Search for a pattern only in a certain filetype (e.g., html, css, etc.):
rg -t filetype pattern
- Search for a pattern only in a subset of directories:
rg pattern set_of_subdirs
- Search for a pattern in files matching a glob (e.g., `README.*`):
rg pattern -g glob
And the one that’s actually my most used command now is a function I made combining fzf and rg and some borrowed code from the web.
fs
# Modified version where you can press
# - CTRL-O to open with `open` command,
# - CTRL-E or Enter key to open with the $EDITOR
# - CTRL-W to search inside files
# - CTRL-C to copy file path to clipboard
# - CTRL-D to cd to directory of file
# - CTRL-N to make a new markdown file.
fs() {
local out file key
IFS=$'\n' out=($(fzf -i --preview="pygmentize -g {}" --query="$1" --exit-0 --expect=ctrl-o,ctrl-e,ctrl-w,ctrl-m,ctrl-c,ctrl-d,ctrl-x,ctrl-n --bind '?:toggle-preview'))
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
esfile=$(printf %q "$file")
if [ -n "$file" ]; then
[ "$key" = ctrl-o ] && open "$file" ||
[ "$key" = ctrl-w ] && infile "$1" ||
[ "$key" = ctrl-c ] && echo "$file" | pbcopy ||
[ "$key" = ctrl-d ] && cd $(dirname "$file") ||
[ "$key" = ctrl-n ] && code "$1.md" ||
${EDITOR:-code} "$file"
fi
}
infile() {
rg "$1" | fzf --height 40% | sed 's/:.*$//g' | sed 's/ /\\ /g' | sed 's/&/\\&/g' | xargs -0 -I {} /bin/zsh -c 'code {}'
}
This looks awesome. Immediately in my mind I think of students who have been added or removed from my classes (I have over 300 students spread out over a couple of sections). I would love to be able to do a compare at the end of the week to see who to follow up with or stop worrying about. If I pull lists out and compare them week to week this is what I would use?
FYI Take Control Books has a useful ebook on this subject: TAKE CONTROL OF
the Mac Command Line with Terminal. $15 (with free download of pdf and epub versions), though TC Books occasionally offers discount coupons…
You can download a pdf sample from the ebook here.