zsh

Functions

Default arguments

Example of a function with default arguments in zsh

function e() {
    if [ "$1" != "" ]
    then
        subl $1
    else
        subl .
fi
}

File name without the extension

If you want the full path without the extension:

$ myfile=/path/to/story.txt
$ echo ${myfile:r}
/path/to/story
$ myfile=story.txt
$ echo ${myfile:r}
story

If you want just the file name minus the path:

$ myfile=/path/to/story.txt
$ echo ${myfile:t}
story.txt

Check this out you can combine those two symbols!

$ myfile=/path/to/story.txt
$ echo ${myfile:t:r}
story

Hooks

  1. Automatic ls after cd: A function is created to replace the cd command. Whenever cd is used to change directories, the function automatically runs ls to display the contents of the new directory. zsh function cd { builtin cd "$@" && ls } This method applies to bash and zsh, combining the cd and ls commands.

  2. Using zsh Hooks for ls after cd: A more refined approach specific to zsh that uses hooks. A function named ls_after_cd is defined to run ls, and a zsh hook is added to execute this function every time the current working directory changes. ```zsh autoload -U add-zsh-hook

    ls_after_cd() { ls }

    add-zsh-hook chpwd ls_after_cd ``This approach is more aligned withzshpractices, allowing for the automatic listing of directory contents upon navigation without altering the originalcd` command.

Performance

Profiling