---
category: en
type: paper
layout: paper
hastr: true
tags: zshrc, configuration, linux
title: About zshrc
short: about-zshrc
description: It is first paper in my blog (I think I need something here for tests =)). There are many similar articles, and I'll not be an exception. I just want to show my .zshrc
and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (original).
---
First install recommended minima:
{% highlight bash %} pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting {% endhighlight %}pkgfile is a very useful utility. Also this command will install shell, additional completion and syntax highlighting.
All options are avaible here.
Set history file and number of commands in cache of the current session and in the history file:
{% highlight bash %} # history HISTFILE=~/.zsh_history HISTSIZE=500000 SAVEHIST=500000 {% endhighlight %}I can not remember all Ctrl+
combinations so I bind keys to its default usages:
But in this case Up
/Down
arrows are used to navigate through the history based on already entered part of a command. And PgUp
/PgDown
will ignore already entered part of a command.
Command autocomplete:
{% highlight bash %} # autocomplete autoload -U compinit compinit zstyle ':completion:*' insert-tab false zstyle ':completion:*' max-errors 2 {% endhighlight %}Full command autocomplete will be enabled. insert-tab false
will enable autocomplete for non-entered commands. max-errors
sets maximum number of errors that could be corrected.
Prompt:
{% highlight bash %} # promptinit autoload -U promptinit promptinit {% endhighlight %}Enable colors:
{% highlight bash %} # colors autoload -U colors colors {% endhighlight %}Here are some other options.
Change directory without cd
:
Correcting of typos (and question template):
{% highlight bash %} # correct setopt CORRECT_ALL SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) " {% endhighlight %}Disable f#$%ing beep:
{% highlight bash %} # disable beeps unsetopt beep {% endhighlight %}Enable calculator:
{% highlight bash %} # calc autoload zcalc {% endhighlight %}Append history (do not recreate the history file):
{% highlight bash %} # append history setopt APPEND_HISTORY {% endhighlight %}Do not save dups to history file:
{% highlight bash %} # ignore spaces in history setopt HIST_IGNORE_ALL_DUPS {% endhighlight %}...and additional spaces:
{% highlight bash %} # ignore dups in history setopt HIST_IGNORE_SPACE {% endhighlight %}...and blank lines too:
{% highlight bash %} # reduce blanks in history setopt HIST_REDUCE_BLANKS {% endhighlight %}Enable pkgfile
:
In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (rm -rf *
in the example). Also root
and cursor
highlighting may be turned on. Colors syntax is understandable, fg
is font color, bg
is background color.
The general idea is the use single .zshrc
for root and normal user:
fg
is font color, bg
is background color. _bold
and _no_bold
regulate the tint. Commands should be in %{ ... %}
so they do not appear. Avaible colors are:
Avaible variables are:
{% highlight bash %} %n - the username %m - the computer's hostname (truncated to the first period) %M - the computer's hostname %l - the current tty %? - the return code of the last-run application. %# - the prompt based on user privileges (# for root and % for the rest) %T - system time(HH:MM) %* - system time(HH:MM:SS) %D - system date(YY-MM-DD) %d - the current working directory %~ - the same as %d but if in $HOME, this will be replaced by ~ %1/ - the same as %d but only last directory {% endhighlight %}RPROMPT (acpi
package is necessary):
My RPROMPT shows current time, battery change and last returned code. precmd()
is necessary for automatic updating. The construct $(if.true.false)
is conditional statement in zsh
.
Copy only those aliases that you need. If any alias uses application that is not installed it will leads to fail of loading of configuration file.
Small useful (or maybe not) function:
{% highlight bash %} show_which() { OUTPUT=$(which $1 | cut -d " " -f7-) echo "Running '$OUTPUT'" 1>&2 } {% endhighlight %}Here is the first group of aliases:
{% highlight bash %} ## alias # colored grep alias grep='grep --colour=auto' # change top to htop alias top='show_which top && htop' # chromium with different proxy servers (i2p and tor included) alias chrommsu='show_which chrommsu && chromium --proxy-server=cache.msu:3128' alias chromtor='show_which chromtor && chromium --proxy-server="socks://localhost:9050" --incognito' alias chromi2p='show_which chromi2p && chromium --proxy-server="http=127.0.0.1:4444;https=127.0.0.1:4445" --incognito' # human-readable df and du alias df='show_which df && df -k --print-type --human-readable' alias du='show_which du && du -k --total --human-readable' # change less and zless to vimpager alias less='vimpager' alias zless='vimpager' {% endhighlight %}Here are ls aliases (see man ls):
{% highlight bash %} alias ls='show_which ls && ls --color=auto --group-directories-first' alias ll='show_which ll && ls -l --human-readable' alias lr='show_which lr && ls --recursive' alias la='show_which la && ll --almost-all' alias lx='show_which lx && ll -X --ignore-backups' alias lz='show_which lz && ll -S --reverse' alias lt='show_which lt && ll -t --reverse' alias lm='show_which lm && la | more' {% endhighlight %}Here are aliases to quick file view from console (just type a file name!):
{% highlight bash %} # alias -s alias -s {avi,mpeg,mpg,mov,m2v,mkv}=mpv alias -s {mp3,flac}=qmmp alias -s {odt,doc,xls,ppt,docx,xlsx,pptx,csv}=libreoffice alias -s {pdf}=okular autoload -U pick-web-browser alias -s {html,htm}=opera {% endhighlight %}Here are "sudo" aliases:
{% highlight bash %} # sudo alias if [[ $EUID == 0 ]]; then alias fat32mnt='show_which fat32mnt && mount -t vfat -o codepage=866,iocharset=utf8,umask=000' alias synctime='show_which synctime && { ntpd -qg; hwclock -w; date; }' else alias fat32mnt='show_which fat32mnt && sudo mount -t vfat -o codepage=866,iocharset=utf8,umask=000' alias umount='show_which umount && sudo umount' alias mount='show_which mount && sudo mount' alias netctl='show_which netctl && sudo netctl' alias synctime='show_which synctime && { sudo ntpd -qg; sudo hwclock -w; date; }' alias wifi-menu='show_which wifi-menu && sudo wifi-menu' alias dhcpcd='show_which dhcpcd && sudo dhcpcd' alias journalctl='show_which journalctl && sudo journalctl' alias systemctl='show_which systemctl && sudo systemctl' alias modprobe='show_which modprobe && sudo modprobe' alias rmmod='show_which rmmod && sudo rmmod' alias staging-i686-build='show_which staging-i686-build && sudo staging-i686-build' alias staging-x86_64-build='show_which staging-x86_64-build && sudo staging-x86_64-build' fi {% endhighlight %}Here are global aliases. If they are enable the command cat foo g bar
will be equivalent the command cat foo | grep bar
:
Here is a special function for xrandr
:
Unfortunately I can not remember tar
flags thus I use special functions:
Here is a special function for su
:
Function that replaces original rm
command. If you type rm
it will be equivalent moving to trash an you can easily restore a file:
Functions with automatic rehash after installing/removing packages are:
{% highlight bash %} pacman () { /usr/bin/sudo /usr/bin/pacman $* && echo "$*" | grep -q "S\|R\|U" && rehash } yaourt () { /usr/bin/yaourt $* && echo "$*" | grep -q "S\|R\|U" && rehash } # for testing repo yatest () { /usr/bin/yaourt --config /etc/pactest.conf $* && echo "$*" | grep -q "S\|R\|U" && rehash } {% endhighlight %}It is recommended to set own variables in ~/.zshenv
. But I have everything stored in the single file.
Here are path, mask of new files, editor and pager:
{% highlight bash %} # path export PATH="$PATH:$HOME/.local/bin" # umask umask 022 # editor export EDITOR="vim" export PAGER="vimpager" {% endhighlight %}Here is hashes. If they are enable the command ~global
will be equivalent the command /mnt/global
:
Here is my .zshrc
.