Skip to content

Instantly share code, notes, and snippets.

@ClementNerma
Last active October 27, 2025 11:08
Show Gist options
  • Save ClementNerma/1dd94cb0f1884b9c20d1ba0037bdcde2 to your computer and use it in GitHub Desktop.
Save ClementNerma/1dd94cb0f1884b9c20d1ba0037bdcde2 to your computer and use it in GitHub Desktop.

Revisions

  1. ClementNerma revised this gist Nov 13, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,8 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(ie)value]}` |
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(Ie)value]}` |
    | Get index of a value in an array (`${#VARNAME} + 1` if not found) | `${VARNAME[(ie)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( $VARNAME[(Ie)value] ));` |
  2. ClementNerma revised this gist Aug 24, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(ie)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( $VARNAME[(ie)value] ));` |
    | Check if a value is contained in an array | `if (( $VARNAME[(Ie)value] ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |
    | Remove an element from an array | `VARNAME[index]=()` |
  3. ClementNerma revised this gist Mar 10, 2021. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,8 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Replace all occurrences in a string | `${VARNAME//toreplace/replacement}` |
    | Cut a string after a model | `${VARNAME%%model*}` |
    | Check if a string starts by a specific substring | `if [[ $VARNAME = "startstr"* ]]` |
    | Check if a string contains a substring | `if [[ $VARNAME = *"substring"* ]]` |
    | Check if a string ends by a specific substring | `if [[ $VARNAME = *"substring" ]]` |

    ## Arrays

    @@ -27,12 +29,13 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(I)value]}` |
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(ie)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( $VARNAME[(Ie)value] ));` |
    | Check if a value is contained in an array | `if (( $VARNAME[(ie)value] ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |
    | Remove an element from an array | `VARNAME[index]=()` |

    ## Associative arrays (= maps / dictionaries)

  4. ClementNerma revised this gist Feb 23, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr

    | Description | Syntax |
    | ------------------------------------------------ | ----------------------------------- |
    | Get the length of a string | `${#VARNAME}` |
    | Get a single character | `${VARNAME[index]}` |
    | Get the string from a specific index | `${VARNAME[index,-1]}` |
    | Get a substring | `${VARNAME[from,to]}` |
  5. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -16,22 +16,22 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr

    ## Arrays

    | Description | Syntax |
    | ---------------------------------------------- | ----------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+=(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |
    | Description | Syntax |
    | --------------------------------------------------- | -------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+=(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array (`0` if not found) | `${VARNAME[(I)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( $VARNAME[(Ie)value] ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |

    ## Associative arrays (= maps / dictionaries)

    @@ -43,7 +43,7 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p
    | Create an associative array with initial values | `typeset -A VARNAME=( [key1]=value1 [key2]=value2 )` |
    | Add a new key to the array | `VARNAME[key]=value` |
    | Access the array's elements | `$VARNAME[key]` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Remove a key from the array | `unset 'VARNAME[key]'` |
    | Get the array's number of elements | `${#VARNAME}` |
    | Iterate over the array's values | `for value in $VARNAME;` |
    | Iterate over the array's keys | `for key in ${(k)VARNAME};` |
    @@ -130,7 +130,7 @@ fi
    | Description | Syntax |
    | --------------------------------------------------------------- | ---------------------------- |
    | Check if a string is empty or not defined | `if [[ -z $VARNAME ]];` |
    | Check if a string is defined and not empty | `if [[ ! -n $VARNAME ]];` |
    | Check if a string is defined and not empty | `if [[ -n $VARNAME ]];` |
    | Check if a file exists | `if [[ -f "filepath" ]];` |
    | Check if a directory exists | `if [[ -d "dirpath" ]]; ` |
    | Check if a symbolic link exists | `if [[ -L "symlinkpath" ]];` |
  6. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p
    | Remove the first parameter from `$@` | `shift` |
    | Remove the last parameter from `$@` | `shift -p` |
    | Exit the function with a status code (behaves like for a command) | `return 1` (or any other code) |
    | Get the list of all functions, as an array | `${(k()functions}` |
    | Get the list of all functions, as an array | `${(k)functions}` |
    | Delete a function | `unset -f func_name` |

    ## Aliases
  7. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -142,7 +142,7 @@ fi
    | Check if a command exits successfully (exit code `0`) | `if command arg1 arg2 ...` |
    | Check if a command doesn't exit successfully (exit code != `0`) | `if ! command arg1 arg2 ...` |

    NOte that the `$` symbol preceding variables' names in arithmetic expression (`((...))`) are purely optional, so you can perfectly write `if (( VAR1 < VAR2 ));` for instance.
    Note that the `$` symbol preceding variables' names in arithmetic expression (`((...))`) are purely optional, so you can perfectly write `if (( VAR1 < VAR2 ));` for instance.

    You can read all dash `-` options in ZSH's manual, as there are many different ones: http://zsh.sourceforge.net/Doc/Release/Conditional-Expressions.html

  8. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -142,6 +142,8 @@ fi
    | Check if a command exits successfully (exit code `0`) | `if command arg1 arg2 ...` |
    | Check if a command doesn't exit successfully (exit code != `0`) | `if ! command arg1 arg2 ...` |

    NOte that the `$` symbol preceding variables' names in arithmetic expression (`((...))`) are purely optional, so you can perfectly write `if (( VAR1 < VAR2 ));` for instance.

    You can read all dash `-` options in ZSH's manual, as there are many different ones: http://zsh.sourceforge.net/Doc/Release/Conditional-Expressions.html

    ## Loops
  9. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -205,5 +205,5 @@ If we want to display the message only if the condition is falsey:

    ```zsh
    [[ -z $VARNAME ]] || echo "VARNAME is not empty!"
    [[ -f $FILEPATH ]] || echo "File does not exists!"
    [[ -f $FILEPATH ]] || echo "File does not exist!"
    ```
  10. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -142,6 +142,8 @@ fi
    | Check if a command exits successfully (exit code `0`) | `if command arg1 arg2 ...` |
    | Check if a command doesn't exit successfully (exit code != `0`) | `if ! command arg1 arg2 ...` |

    You can read all dash `-` options in ZSH's manual, as there are many different ones: http://zsh.sourceforge.net/Doc/Release/Conditional-Expressions.html

    ## Loops

    Syntaxes:
  11. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -130,7 +130,7 @@ fi
    | Description | Syntax |
    | --------------------------------------------------------------- | ---------------------------- |
    | Check if a string is empty or not defined | `if [[ -z $VARNAME ]];` |
    | Check if a string is defined and not empty | `if [[ ! -z $VARNAME ]];` |
    | Check if a string is defined and not empty | `if [[ ! -n $VARNAME ]];` |
    | Check if a file exists | `if [[ -f "filepath" ]];` |
    | Check if a directory exists | `if [[ -d "dirpath" ]]; ` |
    | Check if a symbolic link exists | `if [[ -L "symlinkpath" ]];` |
    @@ -193,7 +193,7 @@ echo $(add_twice 2 3) # 10
    Conditionals use expressions, such as in `if [[ -z $VARNAME ]];` the expression is `[[ -z $VARNAME ]]`. These can also be used in `while` loops, as well as be used outside of blocks:

    ```zsh
    [[ -z $VARNAME ]] && echo "VARNAME is defined!"
    [[ -z $VARNAME ]] && echo "VARNAME is not defined or empty!"
    [[ -f $FILEPATH ]] && echo "File exists!"
    ```

    @@ -202,6 +202,6 @@ This works because conditional expressions (`[[ ... ]]` and `(( ... ))`) don't a
    If we want to display the message only if the condition is falsey:

    ```zsh
    [[ -z $VARNAME ]] || echo "VARNAME is not defined or empty!"
    [[ -z $VARNAME ]] || echo "VARNAME is not empty!"
    [[ -f $FILEPATH ]] || echo "File does not exists!"
    ```
  12. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -39,15 +39,15 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p

    | Description | Syntax |
    | ----------------------------------------------- | ---------------------------------------------------- |
    | Create an associative array | `declare -A VARNAME=()` |
    | Create an associative array with initial values | `declare -A VARNAME=( [key1]=value1 [key2]=value2 )` |
    | Create an associative array | `typeset -A VARNAME=()` |
    | Create an associative array with initial values | `typeset -A VARNAME=( [key1]=value1 [key2]=value2 )` |
    | Add a new key to the array | `VARNAME[key]=value` |
    | Access the array's elements | `$VARNAME[key]` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Get the array's number of elements | `${#VARNAME}` |
    | Iterate over the array's values | `for value in $VARNAME;` |
    | Iterate over the array's keys | `for key in ${(@k)VARNAME};` |
    | Iterate over the array's key-value pairs | `for key value in ${(@kv)VARNAME};` |
    | Iterate over the array's keys | `for key in ${(k)VARNAME};` |
    | Iterate over the array's key-value pairs | `for key value in ${(kv)VARNAME};` |

    ## Arithmetics

    @@ -72,6 +72,7 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p
    | Get a parameter | `$1` (second is `$2`, etc.) |
    | Expand all parameters | `$*` |
    | Expand all parameters but keep them quoted if needed | `$@` (tip: it's an array!) |
    | Get the number of parameters (so not counting `$0`) | `$#` |
    | Remove the first parameter from `$@` | `shift` |
    | Remove the last parameter from `$@` | `shift -p` |
    | Exit the function with a status code (behaves like for a command) | `return 1` (or any other code) |
  13. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 18 additions and 17 deletions.
    35 changes: 18 additions & 17 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -16,22 +16,22 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr

    ## Arrays

    | Description | Syntax |
    | ---------------------------------------------- | -------------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+=(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME[@]}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME[@]} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |
    | Description | Syntax |
    | ---------------------------------------------- | ----------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+=(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME}` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |

    ## Associative arrays (= maps / dictionaries)

    @@ -42,8 +42,9 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p
    | Create an associative array | `declare -A VARNAME=()` |
    | Create an associative array with initial values | `declare -A VARNAME=( [key1]=value1 [key2]=value2 )` |
    | Add a new key to the array | `VARNAME[key]=value` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Access the array's elements | `$VARNAME[key]` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Get the array's number of elements | `${#VARNAME}` |
    | Iterate over the array's values | `for value in $VARNAME;` |
    | Iterate over the array's keys | `for key in ${(@k)VARNAME};` |
    | Iterate over the array's key-value pairs | `for key value in ${(@kv)VARNAME};` |
  14. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Get the string from a specific index | `${VARNAME[index,-1]}` |
    | Get a substring | `${VARNAME[from,to]}` |
    | Replace the first occurrence in a string | `${VARNAME/toreplace/replacement}` |
    | Replace the last occurrence in a string | `${VARNAME//toreplace/replacement}` |
    | Replace all occurrences in a string | `${VARNAME//toreplace/replacement}` |
    | Cut a string after a model | `${VARNAME%%model*}` |
    | Check if a string starts by a specific substring | `if [[ $VARNAME = "startstr"* ]]` |

    @@ -20,7 +20,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | ---------------------------------------------- | -------------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+(value)` |
    | Push to an array | `VARNAME+=(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
  15. ClementNerma revised this gist Feb 18, 2021. No changes.
  16. ClementNerma revised this gist Feb 18, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -200,6 +200,6 @@ This works because conditional expressions (`[[ ... ]]` and `(( ... ))`) don't a
    If we want to display the message only if the condition is falsey:

    ```zsh
    [[ -z $VARNAME ]] || echo "VARNAME is defined!"
    [[ -f $FILEPATH ]] || echo "File exists!"
    [[ -z $VARNAME ]] || echo "VARNAME is not defined or empty!"
    [[ -f $FILEPATH ]] || echo "File does not exists!"
    ```
  17. ClementNerma revised this gist Feb 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ Associate arrays are the equivalent of hash maps or dictionaries in many other p
    | Add a new key to the array | `VARNAME[key]=value` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Access the array's elements | `$VARNAME[key]` |
    | Iterate over the array's value | `for value in $VARNAME;` |
    | Iterate over the array's values | `for value in $VARNAME;` |
    | Iterate over the array's keys | `for key in ${(@k)VARNAME};` |
    | Iterate over the array's key-value pairs | `for key value in ${(@kv)VARNAME};` |

  18. ClementNerma revised this gist Feb 17, 2021. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -19,19 +19,35 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Description | Syntax |
    | ---------------------------------------------- | -------------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Create an array with initial values | `VARNAME=(value1 value2 value3)` |
    | Push to an array | `VARNAME+(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME[@]}` |
    | Iterate over an array's values | `for value in VARNAME;` |
    | Iterate over an array's values | `for value in $VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME[@]} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |

    ## Associative arrays (= maps / dictionaries)

    Associate arrays are the equivalent of hash maps or dictionaries in many other programming languages: unlike arrays, they can use string keys, and these don't necessary have an order.

    | Description | Syntax |
    | ----------------------------------------------- | ---------------------------------------------------- |
    | Create an associative array | `declare -A VARNAME=()` |
    | Create an associative array with initial values | `declare -A VARNAME=( [key1]=value1 [key2]=value2 )` |
    | Add a new key to the array | `VARNAME[key]=value` |
    | Remove a key from the array | `unset 'VARNAME[key]` |
    | Access the array's elements | `$VARNAME[key]` |
    | Iterate over the array's value | `for value in $VARNAME;` |
    | Iterate over the array's keys | `for key in ${(@k)VARNAME};` |
    | Iterate over the array's key-value pairs | `for key value in ${(@kv)VARNAME};` |

    ## Arithmetics

    | Description | Syntax |
  19. ClementNerma revised this gist Feb 17, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Iterate over an array's values | `for value in VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index:length}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME[@]} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |
    @@ -68,6 +69,7 @@ This is a cheat sheet for how to perform various actions to ZSH, which can be tr
    | Get the list of all defined aliases, as an array | `${(k)aliases}` |
    | Define an alias | `alias name="command arg1 arg2 arg3 ..."` |
    | Remove an alias | `unalias name` |
    | Get the arguments, with escaped spaces | `${@:q}` |

    ## Conditionals

  20. ClementNerma created this gist Feb 17, 2021.
    187 changes: 187 additions & 0 deletions zsh-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,187 @@
    # ZSH CheatSheet

    This is a cheat sheet for how to perform various actions to ZSH, which can be tricky to find on the web as the syntax is not intuitive and it is generally not very well-documented.

    ## Strings

    | Description | Syntax |
    | ------------------------------------------------ | ----------------------------------- |
    | Get a single character | `${VARNAME[index]}` |
    | Get the string from a specific index | `${VARNAME[index,-1]}` |
    | Get a substring | `${VARNAME[from,to]}` |
    | Replace the first occurrence in a string | `${VARNAME/toreplace/replacement}` |
    | Replace the last occurrence in a string | `${VARNAME//toreplace/replacement}` |
    | Cut a string after a model | `${VARNAME%%model*}` |
    | Check if a string starts by a specific substring | `if [[ $VARNAME = "startstr"* ]]` |

    ## Arrays

    | Description | Syntax |
    | ---------------------------------------------- | -------------------------------------------------- |
    | Create an array | `VARNAME=()` |
    | Push to an array | `VARNAME+(value)` |
    | Access an array's element | `VARNAME[index]` |
    | Remove first element from an array (shift) | `shift VARNAME` |
    | Remove last element from an array (pop) | `shift -p VARNAME` |
    | Get an array's length | `${#VARNAME[@]}` |
    | Iterate over an array's values | `for value in VARNAME;` |
    | Get index of a value in an array | `${VARNAME[(i)value]}` |
    | Get an array slice _after_ the specified index | `${VARNAME:index}` |
    | Check if a value is contained in an array | `if (( ${VARNAME[(i)value]} <= ${#VARNAME[@]} ));` |
    | Check if an array is empty | `if [[ -z $VARNAME ]]` |
    | Check if an array is not empty | `if [[ ! -z $VARNAME ]]` |

    ## Arithmetics

    | Description | Syntax |
    | -------------------------------------------------------------------------------------- | ----------------- |
    | Compute a mathematical expression (variables don't need to be prefixed with `$` in it) | `$((expression))` |

    ## Variables

    | Description | Syntax |
    | ------------------------------------------------------------- | ------------------ |
    | Get the value of a variable whose name is in another variable | `${(P)NAMEVAR}` |
    | Get the list of all defined variables, as an array | `${(k)parameters}` |
    | Delete a variable | `unset VARNAME` |

    ## Functions

    | Description | Syntax |
    | ----------------------------------------------------------------- | ------------------------------ |
    | Declare a local variable (not accessible outside the function) | `local varname=...` |
    | Get the original executable name | `$0` |
    | Get a parameter | `$1` (second is `$2`, etc.) |
    | Expand all parameters | `$*` |
    | Expand all parameters but keep them quoted if needed | `$@` (tip: it's an array!) |
    | Remove the first parameter from `$@` | `shift` |
    | Remove the last parameter from `$@` | `shift -p` |
    | Exit the function with a status code (behaves like for a command) | `return 1` (or any other code) |
    | Get the list of all functions, as an array | `${(k()functions}` |
    | Delete a function | `unset -f func_name` |

    ## Aliases

    | Description | Syntax |
    | ------------------------------------------------ | ----------------------------------------- |
    | Display the list of all defined aliases | `alias` |
    | Get the list of all defined aliases, as an array | `${(k)aliases}` |
    | Define an alias | `alias name="command arg1 arg2 arg3 ..."` |
    | Remove an alias | `unalias name` |

    ## Conditionals

    [A word on conditionals](#a-word-on-conditionals)

    Syntaxes:

    ```zsh
    # 1)
    if expression
    then
    # instructions
    fi

    # 2)
    if expression; then
    # instructions
    fi

    # 3)
    if expression; then ...; fi

    # 4)
    if expression; then
    # instructions
    else
    # instructions
    fi

    # 5)
    if expression; then
    # instructions
    elif expression
    # instructions
    else
    # instructions
    fi
    ```

    | Description | Syntax |
    | --------------------------------------------------------------- | ---------------------------- |
    | Check if a string is empty or not defined | `if [[ -z $VARNAME ]];` |
    | Check if a string is defined and not empty | `if [[ ! -z $VARNAME ]];` |
    | Check if a file exists | `if [[ -f "filepath" ]];` |
    | Check if a directory exists | `if [[ -d "dirpath" ]]; ` |
    | Check if a symbolic link exists | `if [[ -L "symlinkpath" ]];` |
    | Check if a shell option is set | `if [[ -o OPTION_NAME ]];` |
    | Check if two values are equal | `if [[ $VAR1 = $VAR2 ]];` |
    | Check if two values are different | `if [[ $VAR1 != $VAR2 ]];` |
    | Check if a number is greater than another | `if (( $VAR1 > $VAR2 ));` |
    | Check if a number is smaller than another | `if (( $VAR1 < $VAR2 ));` |
    | Check if a command exits successfully (exit code `0`) | `if command arg1 arg2 ...` |
    | Check if a command doesn't exit successfully (exit code != `0`) | `if ! command arg1 arg2 ...` |

    ## Loops

    Syntaxes:

    ```zsh
    # 1)
    for itervarname in iterable
    do
    # instructions
    done

    # 2)
    for itervarname in iterable; do
    # instructions
    done

    # 3)
    for itervaname in iterable; do ...; done
    ```

    | Description | Syntax |
    | ------------------------------------------------------------------------ | -------------------------- |
    | Iterate over a range (inclusive) | `for i in {from..to};` |
    | Iterate over a list of filesystem items | `for i in globpattern;` |
    | Iterate over a list of filesystem items, fail silently if no match found | `for i in globpattern(N);` |

    ## Examples cheat sheet

    Return a value from within a function:

    ```zsh
    function add() {
    local sum=$(($1 + $2))
    echo $sum
    }

    function add_twice() {
    local sum=$(add $1 $2) # get the callee's STDOUT
    local sum_twice=$(add $sum $sum)
    echo $sum_twice
    }

    echo $(add 2 3) # 5
    echo $(add_twice 2 3) # 10
    ```

    ## A word on conditionals

    Conditionals use expressions, such as in `if [[ -z $VARNAME ]];` the expression is `[[ -z $VARNAME ]]`. These can also be used in `while` loops, as well as be used outside of blocks:

    ```zsh
    [[ -z $VARNAME ]] && echo "VARNAME is defined!"
    [[ -f $FILEPATH ]] && echo "File exists!"
    ```

    This works because conditional expressions (`[[ ... ]]` and `(( ... ))`) don't actually return a value; they behave like commands and as such set the status code to `0` if the condition is true, or `1` else.

    If we want to display the message only if the condition is falsey:

    ```zsh
    [[ -z $VARNAME ]] || echo "VARNAME is defined!"
    [[ -f $FILEPATH ]] || echo "File exists!"
    ```