- There are plenty of plugins and themes to enhance the bash experience (see below)
- Spell correct: type a command like
cd myFolder
(an existing one) but make a typo likecd myfolder
orcd myFoder
or anything else. Zsh will detect it and will suggest something like Did you mean cd myFolder?. To make this work, runnano ~/.zshrc
and make sure to enableENABLE_CORRECTION
to true (uncomment the line) and write a new line likeexport SPROMPT="You've typed %R, did you mean %r? [Yes, No, Abort, Edit] "
to customize the question as you want. Runsource ~/.zshrc
to read the new configuration file if you've modified it. - There is a special utility called
zmv
that will allow mass actions on files or folders like renaming.tmp
files to.bak
f.i.zmv -n '(*).tmp' '${1}.bak'
. - Completion is provided with the
-
(minus sign) and after pressing the tab key. For instance, typegit commit -
ordocker-compose logs -
then press - Chain commands with:
;
like inclear ; ls
(run the first then the second)||
like inrm -rf node_modules/ || rm package-lock.json || yarn install
(||
will exec the next command only if the previous one was successful)|
to pipe the output like incat longfile.log | less
(similar to thePAUSE
DOS command)
- Special search patterns like:
ls *(.x)
display the list of all executable files in the current folderls */*(.x)
display the list of all executable files in a first-level subfolderls **/*(.x)
display the list of all executable files, whatever the deep in the structurels -d */**(/)
list of all folders and subfolders (not deeper)
- The autopushd command helps you do
popd
after you use cd to go back to your previous directory: type f.i.cd /usr/local/bin
to go there then typepopd
and you're back in your origin folder.-
(the minus sign) is an alias ofpopd
- Run
mkdir
andcd
in only one action:take test/any/sub/folders
will create the folder and jump in it (take
is a function defined in~/.oh-my-zsh/lib/functions.zsh
; we can create our owns) - You've a subfolder called
vendor
? No need to typecd vendor
,cd
is implicit; just typevendor
and press Enter to jump inside - Inline glob expansion: type f.i.
cat */**/settings.json
AND PRESS TAB (not enter!), zsh will replace the pattern with all matched filenames. The cat instruction will then be something likecat settings.json vendor/laravel/tinker/settings.json vendor/vlucas/phpdotenv/settings.json
(and many more perhaps). Before pressing Enter, you can navigate in the command and suppress a few filenames f.i. - Prompt completion: type
cd
followed by enter and you'll get the list of folders; you can select a name by navigating with the tab key - Type
cd ven
and press tab, Zsh will complete the folder name - Type
cd /u/lo/b
and press tab, the path will be extended tocd /usr/local/bin
because there is no doubt on the desired path. This is called the fuzzy match feature - Thanks to global aliases, type
cd ...
to go up two levels (the parent folder of the parent folder),cd .....
for three levels up (equivalent ofcd ../../..
)
- Type any command like
mkdir
followed by a space and press the UP or DOWN key to loop across the history. - Type
cd sto
and Zsh will display a command you've already used (likecd storage/app/public/pictures
); just press the right arrow key and hop, the command will be completed so you just need to press enter to run it - Shared history: open more than one Zsh sessions, the history will be shared across all sessions
- ALT-/ to output the full CLI history
- CTRL-R to show the list of commands in the history with navigation feature so we can select a previous command and press Enter to fire it
- Press CTRL-L to clear the screen (note:
clear
will clear the output history i.e. if we scroll to the first line, we'll not have previous commands while this is untrue with CTRL-L)
- First, install zsh like this:
sudo apt install zsh
. - Then set zsh as your default shell:
chsh -s /usr/bin/zsh
- This done, install oh-my-zsh:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Note: each time a plugin has to be activated, you'll need to edit the ~/.zshrc
file and add his name to the list of plugins to load.
Edit the file and search for the plugins=( ... )
array. Add the plugin name to the list. Save the file and the plugin will be loaded the next time you'll create a session.
With core plugins, no installation are required since they're already there. We just need to activate them by adding their name in the plugins=( ... )
array.
https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/composer
Add aliases for composer
. For instance type cu
instead of composer update
.
https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/docker-compose
Add aliases for docker-compose
like f.i. dce
instead of docker-compose exec
.
Add aliases for git
like f.i. gst
instead of git status
.
https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/globalias
This one is a special one: you'll type any alias like dce
then press the space bar and that alias will be expanded so you'll see what is behind (here, docker-compose exec
). I like this because expanding an alias will allow you to always remember the real command behind, see it and don't have any doubt about "Uh oh, I don't remember well what was the real command".
Example below with la
followed by a space and expanded to ls -lAh
:
https://github.com/zsh-users/zsh-autosuggestions/blob/master/INSTALL.md#oh-my-zsh
This plugin will predict your command based on your history (a command you've already typed in) or, for instance, you're typing cd
and the plugin will automatically suggest the list of folders in the current directory. You're typing cat
and you'll get this time a list of file names.
Absolutely essential.
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/INSTALL.md
Provide highlighting in the prompt. Practical
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
To install fzf:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ; ~/.fzf/install
Much more tips? Take a look to the video tutorial: https://www.youtube.com/watch?v=qgG5Jhi_Els
Press CTRL-R and get the list of all commands you've already fired. Start to type some letters to filter the list and once you've retrieve the desired command, select that one and press ENTER to select that command.
Imagine you want to display the content of a file and you don't want to type the full path. CTRL-T is, here, the solution. By pressing these keys, you'll get the list of files in any child-folders. You can start to type and the list will be filtered. Press ENTER and the filename will be selected and displayed in the prompt.
So, how to use it? Imagine you want to display the content of the file. Just start to type cat
, press CTRL-T, select your file, press ENTER to select it and press once more ENTER so you'll run your command.
The same result can be obtained with cat $(fzf)
or even cat **
followed by TAB. **
is thus a shortcut for $(fzf)
.
Here is a real world sample, you want to show the content of a file that is under a given folder but you don't know anymore where the file is and is exact name. Type **
followed by TAB in the command below.
cat reponame/**
Better below: fzf -m
will allow you to select multiple files (by using TAB) then, once your choice is done, press ENTER. The list of file name will be passed to the next pipe item which is xargs code
here. So, Visual Studio code will be opened and each selected files too, each in his own tab.
fzf -m | xargs cat
Note: if you known the file name (you want to open every composer.json
file), just use find . -maxdepth 2 -name composer.json | xargs code
. The -maxdepth 2
option is to limit to the current folder and his first level child ones.
How to learn which alias are already supported on your system? You always type commands like git status
, docker-compose exec
or any other ones but is there an alias for it?
In fact, the zsh-you-should-use
make the opposite of globalias
and this is fine! zsh-you-should-use
will teach you about existing aliases and globalias
will always remember you the real command behind an alias.
Installation is made of four steps.
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
This done, edit the ~/.zshrc
file and update the ZSH_THEME
like this:
ZSH_THEME="powerlevel10k/powerlevel10k"
Download the four fonts from https://github.com/romkatv/powerlevel10k#manual-font-installation: download one by one and click on the download file. Windows will open it and you'll get a windows with an Install
button.
This done, open the Windows Terminal settings and open the settings.json
file. Add the MesloLGS NF
font in the defaults
section for profiles
, like this:
{
"profiles": {
"defaults": {
"fontFace": "MesloLGS NF"
}
}
}
Once the ~/.zshrc
file has been updated and fonts installed, just create a new Linux session. The powerlevel10k
configuration script will be started and if everything is going fine, you'll see the correct symbol (if not, fonts were incorrectly installed).
Follow the wizard and make your choice in the different options.
If you want to display the username in the prompt, follow this guide.