MacOS Terminal

A collection of tools, commands, tips and tricks for using the terminal and shell on mac OS (and in some manners in general)

macOS specific

Some shell commands that are specific to macOS.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Pasteboard
pbcopy
pbpaste

## Utilities
mdfind # find files on system
screencapture 
launchctl # macOs launch agent

# Cocoa text engine (you can convert pages docs and more!)
textutil

# MacOs native image editing
sips

General

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Directory stack
pushd
popd


# Shell History
# This is the command that backs `history`
fc [number]     # edit command [number]
fc -l [count]   # list [count] history entries
fc -E 			# with EU dates
fc -i 			# with ISO dates
fc -I 			# only history from current shell session

git

1
2
3
git format-patch [branch] --stdout > [file]
git am [file]
git apply [file] # same as git am

Unix Essentials

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
xargs
find

grep
sort
uniq
wc
cut

sed
jq

cat
head
tail
more
less

Improved man

This opens manual entries in a new yellow terminal window. iTerm cannot open these sorts of pages so you have to use a workaround if your main terminal app isn’t Terminal.app:

1
2
3
4
5
6
7
8
# use this if Terminal.app is your main app:
function xm() { open x-man-page://$@; }

# otherwise:
function xm() { open -g x-man-page://$@; osascript -e 'tell application "Terminal" to activate'; }

alias oldman="$(which man)" #preserve old man
alias man="xm"

The -g flag stops open from switching spaces on you.

The idea for this is taken from: On Viewing man Pages.

Shell keyboard shortcuts (zsh)

History

Cursor

Delete

Others

Make new ones

To make new key bindings in zsh you can add use:

1
bindkey (-M [keyset]) [key] [command]

As an example I have the following in my ~/.zshrc file to bind ctrl + V to the command editor; it opens vim and allows me to write a multiline command there instead of directly on the prompt.

1
2
3
autoload edit-command-line # use  function and not a binary
zle -N edit-command-line # add to zle widgets
bindkey '^V' edit-command-line

zsh magic

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# History
!! or !-1 # retrieve last command
!-2 # retrieve second last command

!-3:1-2 # word 1-2 of third last command
!-3:9 # command word of third last command
!-3:* # all words of third last command
!-3:2-$ # word 2 to the end of third last command

# Modifiers
:t # everything after last slash
:h # everything before :t
:r # removes file suffix
:l # lower case
:u # upper case
:s/foo/bar/ # replace first occurance of foo with bar
:gs/foo/bar/ # global version of :s

^old^new^ # shortcut for: !!:s/old/new/;

Related Articles