darwin aka macosx
darwin-xnu kernel
Shell and Utilities volume of IEEE POSIX.1-2017
Install Homebrew on macosx
$ curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh
$ chmod +x ./install.sh
$ ./install.sh
Install KornShell on macosx
$ brew install ksh
Enable ksh as default shell for the user
$ chsh -s /bin/ksh
Configure ~/.profile
export PS1="\${PWD#*/*/*/} \$ "
export VISUAL=/usr/bin/vi
export EDITOR=/usr/bin/vi
Copy-Paste text in Terminal
- Select text to copy > CTRL-C > Put cursor at insertion point > CTRL-V, and the text is copy-pasted to the insertion point
- Put cursor at insertion point > Select text to copy, and then release the mouse/trackpad pressure > Select the marked text and "pull" it slightly until the colored circle with "+" sign appear (direction not important) > Release the mouse/trackpad pressure, and the text is copy-pasted to the insertion point
Configuring a minimalistic but most useful command line prompt
-
For workstation only, don't need username, hostname or wasting a separate line with color time and other non-important information at each and every ENTER
-
For directory structures, in a hierarchical organized filesystems, consider using Partial Relative Path display instead of Absolute Path display (when confused pwd)
$ pwd /home/bjro $ export "PS1="\${PWD#*/*/*/} \$ " /home/bjro $ code/unix code/unix $ pwd /home/bjro/code/unix code/unix $ cd - /home/bjro $ pwer /home/bjro
-
For multi-user hosts only, where it is common to slip around/jump between hosts over TCP connections, and substitute/change user id (and real rooters don't clobber "/")
$ export "PS1="$LOGNAME@$(hostname -s):\${PWD#*/*/*/} \$ " bjro@server123:code/unix $ ssh honeyp321 bjro@honeyp321:code/unix $ su - root root@honeyp321:/ $ echo $LOGNAME $(hostname -s) $PWD root honeyp321 /
For ZSH,
PS1
can be defined similarly (~/.zshrc
)$ export PS1="%n@%m:%2~%(!.#.\$) "
-
Defaulting command line editor and command line visual editor (invoked with
ESC-v
to edit long command line code), add to~/.profile
export VISUAL=/usr/bin/vi export EDITOR=/usr/bin/vi
-
Load environment variables and aliases etc
- use dot
.
notsource
to load, too much to type without functional difference and thus contradict the "unix ethos"$ grep PS1 ~/.profile export "PS1="\${PWD#*/*/*/} \$ " $ . ~/.profile code/unix $
- use dot
-
Rename a file with spaces in the name, if it's the only file in the directory (NB. on some shell's need to escape each space separately)
$ mv * $(echo *|tr ' ' '_')
posix-vi and command line editing features (ESC one time to enter command mode, until insert/append/substitute/change command, then ESC again to get back to command mode). In interactive shell, ESC-v
will enter into $VISUAL
editor for the command line (save & exit will execute the edited command).
.
- Repeat last command
k
- Up one line
j
- Down one line
w
- Right one word
b
- Left one word
l
- Right one character
h
- Left one character
Y
- Copy one line
p
- Paste copy buffer to after cursor line
P
- Paste copy buffer to before cursor line
i
- Insert at cursor
I
- Insert at begining of line
a
- Append after cursor
A
- Append after end of line
s
- Substitute one character at cursor
S
- Substitute the whole line
u
- Undo last command
x
- Delete one character at cursor
X
- Delete one character at cursor move left
dw
- Delete one word from cursor forward
db
- Delete one word from cursor backward
cw
- Change one word from cursor forward
cb
- Change one word from cursor backward
.
- Repeat the last command, such as change the case of the current charcter
~
- Change the case of the current chararcter to the opposite (UPPER > lower || lower > UPPER)
ZZ
- Save file and exit
/
- Enter search mode (down)
?
- Enter search mode (up)
:
- Enter line command mode
:w
- Enter line command mode, and save file
:wq
- Enter line command mode, and save file and exit
:wq!
- Enter line command mode, and write quit forced
:r /etc/motd
- Enter line command mode, and read the content of the file "/etc/motd" into the editor (at the cursor)
:!ls
- Enter line command mode, and run the "ls" command in a subshell
:r!ls
- Enter line command mode, and run the "ls" command in a subshell, and read the the output into the editor (at the cursor)
:%!sort
- Enter line command mode, and for all lines in the file "%", run the "sort" command in a subshell and replace the content of the file
:%s/^XXX/YYY/g
- Enter line command mode, and for all lines in the file "%", change all "XXX" on all lines which start with "XXX" to "YYY"
:%s/$/;/g
- Enter line command mode, and for all lines in the file "%", add a ";" at the end of each line
:2,$!sort
- Enter line command mode, and for all lines from 2 until end of file "2,$", run the "sort" command in a subshell and replace the content of the file (if the file has a header line)
:!rm %
- Enter line command mode, and run the "rm" command on the edited filename "%"
:e /etc/motd
- Enter line command mode, and open the file /etc/motd into the editor
:n
- Enter line command mode, and switch the visual editor to the next file (when editing multiple files)
:e#
- Enter line command mode, and switch the visual editor to the previous file (when editing multiple files), toggling :e#
flips the editor between two files
:set
- Enter line command mode, and show editor configuration settings
:map
- Enter line command mode, and show editor key-action mapping
- Optional initialization command file
~/.exrc
, in this case set the tabstop to 4 instead of default 8
set tabstop=4
set expandtab " this is for Python & YAML, to expand each tab to spaces