The stuff I do

Bash arrays [bash][array]

Initiate an array 🔗

myArray=( element_1 element_2 element_n )

Access item by index 🔗

echo ${myArray[2]}

Iterate through array 🔗

To iterate over the elements:

for element in "${array[@]}"
echo "$element"

To get both the index and the value:

for index in "${!array[@]}"
echo "$index ${array[index]}"

Append element to array 🔗

Bash dates [bash][date]

All of these notes are about date from GNU coreutils on Linux. I believe some parameters are diferent on MacOS.

Everything in there is basically a rehash of man date.

Timestamp / Date conversions 🔗

date +"%s"  # Get current date as timestamp

alias ts2date='timestamp2date' # Parse timestamp as date
function timestamp2date {
echo $(date -d @$1)
Bash loop command [bash][loop]

One liner to do loops

Don't forget there is no ; between do and the first command

while true; do ls -lh file; sleep 1; done
Creating zsh completion function [shell][zsh]

A quick trick to have custom completion on the aliases I create.

First create the function and the alias, they can be in ~/.bash_aliases or whatever.

myCoolFunction() {
echo "Here are the args from my function [email protected]"
alias mcf='myCoolFunction'

Then create the completion function (best practice underscore+function name) which can be anywhere too. An interesting list of the helpers provided by zsh for the completion functions is here.

# Suggest words as completion options
_myCoolFunction() {
compadd "[email protected]" foo bar baz

# Or have completion options with additional description
_myCoolFunction() {
local -a options
'arg1:To do something'
'arg2:To do something else'
'arg3:A different argument'
_describe 'mcf' options

Finally in ~/.zshrc we can register _myCoolFunction as the completion function of myCoolFunction:

compdef _myCoolFunction myCoolFunction

Once everything is ready in a new shell typing mcf <TAB> should yield the 3 completions options.

If things don't work one can check the completion function for a particular function (It does not work with the alias) with:

echo $_comps[myCoolFunction]

and the implementation with

functions $_comps[myCoolFunction]

Finally I should check how to make it work with the defined files (~/.oh-my-zsh/completions) and understand why the compdef command only works when put in .zshrc.

Firefox shortcuts [firefox]

Built-in shortcuts 🔗

Move tab left/right (vimium)</>
Focus address bar<C-j>/<C-k>
Backwards/Forward navigation<A-Left>/<A-Right>

Vimium config 🔗

Custom key mappings

map l nextTab
map h previousTab
map d removeTab

Custom search engine 🔗

To use search engines site: filter automatically:

  • Create a bookmark with the the url and %s for the query string like this:
  • Add a keyword to the bookmark e.g. v
  • In the address bar search with v my query
Fixing commits in git [git][commit]

This link is dope
(And someone made a better UI here)

TODO: Extract the ones I need but can never remember

Remove commit from history 🔗

Reminder on zsh escape ^ in HEAD^ with HEAD\^

Remove the last commit from history but keep the working tree

git reset HEAD^

Remove the last commit of the history completely and discard changes

git reset --hard HEAD^ # THIS DISCARDS CHANGES

Sign an older commit in your branch 🔗

This will do an interactive rebase, playing git commit --amend --no-edit -n -S
after each commit

(Check if -n is useful I can't find it in the man)

git rebase --exec 'git commit --amend --no-edit -n -S' -i my-branch
Grep [bash][grep]

Exclude multiple directories 🔗

grep --exclude-dir={node_modules,src}

Have grep colors in less 🔗

grep --color=always | less -r
Scp [bash][scp]

Basically the same syntax as cp (i.e. scp [source] [dest])

# Local to remote
scp ./local/path [email protected]:/home/user

# Remote to local
scp [email protected]:/home/user ./local/path

Useful options 🔗

-rCopy directories
-iProvide identity file
-PSpecify port
Screen [screen]

Basic usage 🔗

Start a new session with session namescreen -S <session_name>
List running sessions / screensscreen -ls
Attach to a running sessionscreen -x
Attach to a running session with namescreen -r <session_name>
Detach a running sessionscreen -d <session_name> or <C-a><C-d>
Shell redirections [bash][shell][output]

Redirect stdout and stderr to a file 🔗

command > /dev/null 2>&1
Tmux [tmux]

List sessions 🔗

tmux list-sessions

Attache session 🔗

tmux a -t [session-id]
Xargs [bash][xargs]

Super useful opinionated guide to xargs

Examples 🔗

# Don't split input
echo "alice bob" | xargs echo hi
# hi alice bob

# Split input by whitespace and specify a number for each command
echo "alice bob" | xargs -n 1 echo hi
# hi alice
# hi bob

echo "alice bob oscar leon" | xargs -n 2 echo hi
# hi alice bob
# hi oscar leon

# Split input by new line
ls | egrep 'pack' | xargs -d $'\n' -- ls -l
# -rw-rw-r-- 1 afabre afabre 1286 août 21 21:05 package.json
# -rw-rw-r-- 1 afabre afabre 162685 août 21 21:05 package-lock.json

# Run in parallel with -P
xargs -P
systemctl & systemd cheatsheet [systemd][systemctl]

I always get confused in the syntax between systemctl and service commands. To be updated with the missing ones next time I one them.

List services 🔗

systemctl list-units
systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed

Service status 🔗

systemctl status sshd

Service action 🔗

systemctl restart sshd