try to test paginator

This commit is contained in:
arcan1s 2014-01-15 08:08:07 +04:00
parent 946128bbb6
commit d68c2e83e6
23 changed files with 8539 additions and 69 deletions

View File

@ -1,63 +1,7 @@
source: .
destination: ./_site
plugins: ./_plugins
layouts: ./_layouts
include: ['.htaccess']
exclude: ['.gitignore','Gemfile','Gemfile.lock','README.md','TODO']
keep_files: ['.git','.svn']
gems: []
future: true
limit_posts: 0
pygments: true
relative_permalinks: true
permalink: pretty
paginate_path: 'page:num'
paginate: 1
paginate_path: 'blog/:num'
markdown: rdiscount
markdown_ext: markdown,mkd,mkdn,md
textile_ext: textile
excerpt_separator: "\n\n"
safe: false
host: 0.0.0.0
port: 4000
baseurl: http://arcan1s.github.io
url: http://localhost:4000
lsi: true
maruku:
use_tex: false
use_divs: false
png_engine: blahtex
png_dir: images/latex
png_url: /images/latex
fenced_code_blocks: true
rdiscount:
extensions: []
redcarpet:
extensions: []
kramdown:
auto_ids: true
footnote_nr: 1
entity_output: as_char
toc_levels: 1..6
smart_quotes: lsquo,rsquo,ldquo,rdquo
use_coderay: false
coderay:
coderay_wrap: div
coderay_line_numbers: inline
coderay_line_numbers_start: 1
coderay_tab_width: 4
coderay_bold_every: 10
coderay_css: style
redcloth:
hard_breaks: true
baseurl: /
lsi: false

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -0,0 +1,405 @@
---
layout: paper
date: 14 January 2014
last: 14 January 2014
tags: zshrc, configuration, linux
title: 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 <code>.zshrc</code> and explain what it does and why it is needed. Also any comments or additions are welcome. It is a translated paper from Russian (<a href="http://archlinux.org.ru/forum/topic/12752/">original</a>).
commentIssueId: 5
---
<h3><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
<p align="justify">First install recommended minima:</p>
<pre>pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting</pre>
<p align="justify"><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Alo this command will install shell, additional completion and syntax highlighting.</p>
<h3><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
<p align="justify">All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
<p align="justify">Set history file and number of commands in cache of the current session and in the history file:</p>
<pre># history
HISTFILE=~/.zsh_history
HISTSIZE=500000
SAVEHIST=500000</pre>
<p align="justify">I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
<pre># bindkeys
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
bindkey '^[[B' down-line-or-search # down arrow for fwd-history-search
bindkey '\e[1~' beginning-of-line # home
bindkey '\e[2~' overwrite-mode # insert
bindkey '\e[3~' delete-char # del
bindkey '\e[4~' end-of-line # end
bindkey '\e[5~' up-line-or-history # page-up
bindkey '\e[6~' down-line-or-history # page-down</pre>
<p align="justify">But in this case <code>Up</code>/<code>Down</code> arrows are used to navigate through the history based on <b>already entered part</b> of a command. And <code>PgUp</code>/<code>PgDown</code> <b>will ignore</b> already entered part of a command.</p>
<p align="justify">Command autocomplete:</p>
<pre># autocomplete
autoload -U compinit
compinit
zstyle ':completion:*' insert-tab false
zstyle ':completion:*' max-errors 2</pre>
<p align="justify">Full command autocomplete will be enabled. <code>insert-tab false</code> will enable autocomplete for <b>non-entered</b> commands. <code>max-errors</code> sets maximum number of errors that could be corrected.</p>
<p align="justify">Prompt:</p>
<pre># promptinit
autoload -U promptinit
promptinit</pre>
<p align="justify">Enable colors:</p>
<pre># colors
autoload -U colors
colors</pre>
<p align="justify">Here are some other options.</p>
<p align="justify">Change directory without <code>cd</code>:</p>
<pre># autocd
setopt autocd</pre>
<p align="justify">Correcting of typos (and question template):</p>
<pre># correct
setopt CORRECT_ALL
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "</pre>
<p align="justify">Disable f#$%ing beep:</p>
<pre># disable beeps
unsetopt beep</pre>
<p align="justify">Enable calculator:</p>
<pre># calc
autoload zcalc</pre>
<p align="justify">Append history (<b>do not recreate</b> the history file):</p>
<pre># append history
setopt APPEND_HISTORY</pre>
<p align="justify">Do not save dups to history file:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_ALL_DUPS</pre>
<p align="justify">...and additional spaces:</p>
<pre># ignore dups in history
setopt HIST_IGNORE_SPACE</pre>
<p align="justify">...and blank lines too:</p>
<pre># reduce blanks in history
setopt HIST_REDUCE_BLANKS</pre>
<p align="justify">Enable <code>pkgfile</code>:</p>
<pre># pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh</pre>
<h3><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
<div class="codeblock"># highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-3]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-4]='fg=magenta,bold'
# cursor
#ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'
# main
# default
ZSH_HIGHLIGHT_STYLES[default]='none'
# unknown
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red'
# command
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[alias]='fg=yellow,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[function]='fg=green,bold'
ZSH_HIGHLIGHT_STYLES[command]='fg=green'
ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=green'
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=blue,bold'
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=blue,bold'
# path
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan,bold'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=cyan'
# shell
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=blue'
ZSH_HIGHLIGHT_STYLES[assign]='fg=magenta'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]='fg=blue'
# quotes
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow,underline'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
# pattern example
#ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
# root example
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'</div>
<p align="justify">In first line highlighting is turned on. Next main, brackets and pattern highlighting are turned on. Patterns are set below (<code>rm -rf *</code> in the example). Also <code>root</code> and <code>cursor</code> highlighting may be turned on. Colors syntax is understandable, <code>fg</code> is font color, <code>bg</code> is background color.</p>
<h3><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
<p align="justify">The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
<div class="codeblock"># PROMPT && RPROMPT
if [[ $EUID == 0 ]]; then
# [root@host dir]#
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[red]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[red]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]# %{$reset_color%}"
else
# [user@host dir]$
PROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[green]%}%n%{$reset_color%}\
%{$fg_bold[white]%}@%{$reset_color%}\
%{$fg_no_bold[green]%}%m %{$reset_color%}\
%{$fg_bold[yellow]%}%1/%{$reset_color%}\
%{$fg_bold[white]%}]$ %{$reset_color%}"
fi</div>
<p align="justify"><code>fg</code> is font color, <code>bg</code> is background color. <code>\_bold</code> and <code>\_no_bold</code> regulate the tint. Commands should be in <code>%{ ... %}</code> so they do not appear. Avaible colors are:</p>
<pre>black
red
green
yellow
blue
magenta
cyan
white</pre>
<p align="justify">Avaible variables are:</p>
<div class="codeblock">%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</div>
<p align="justify">RPROMPT (<code>acpi</code> package is necessary):</p>
<div class="codeblock">precmd () {
# battery charge
function batcharge {
bat_perc=`acpi | awk {'print $4;'} | sed -e "s/\s//" -e "s/%.*//"`
if [[ $bat_perc < 15 ]]; then
col="%{$fg_bold[red]%}"
elif [[ $bat_perc < 50 ]]; then
col="%{$fg_bold[yellow]%}"
else
col="%{$fg_bold[green]%}"
fi
echo "%{$fg_bold[white]%}["$col$bat_perc"%{$fg_bold[white]%}%%]%{$reset_color%}"
}
# last command
returncode="%(?.%{$fg[green]%}.%{$fg[red]%})%?%{$resetcolor%}"
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}\
%{$fg_bold[cyan]%}%T%{$reset_color%}\
%{$fg_bold[white]%}] %{$reset_color%}"\
$(batcharge)\
"%{$fg_bold[white]%}[%{$reset_color%}"\
$returncode\
"%{$fg_bold[white]%}]%{$reset_color%}"</div>
<p align="justify">My RPROMPT shows current time, battery change and last returned code. <code>precmd()</code> is necessary for automatic updating. The construct <code>$(if.true.false)</code> is conditional statement in <code>zsh</code>.</p>
<h3><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
<p align="justify"><b>Copy only those aliases that you need.</b> If any alias uses application that is not installed it will leads to fail of loading of configuration file.</p>
<p align="justify">Small useful (or maybe not) function:</p>
<pre>show_which() {
OUTPUT=$(which $1 | cut -d " " -f7-)
echo "Running '$OUTPUT'" 1>&2
}</pre>
<p align="justify">Here is the first group of aliases:</p>
<div class="codeblock">## 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'
# more interactive rm
alias rm='show_which rm && rm -I'</div>
<p align="justify">Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
<pre>alias ls='show_which ls && ls --color=auto'
alias ll='show_which ll && ls --group-directories-first -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'</pre>
<p align="justify">Here are aliases to quick file view from console (just type a file name!):</p>
<pre># 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</pre>
<p align="justify">Here are "sudo" aliases:</p>
<div class="codeblock"># 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</div>
<p align="justify">Here are global aliases. If they are enable the command <code>cat foo g bar</code> will be equivalent the command <code>cat foo | grep bar</code>:</p>
<pre># global alias
alias -g g="| grep"
alias -g l="| less"
alias -g t="| tail"
alias -g h="| head"
alias -g dn="&> /dev/null &"</pre>
<h3><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
<p align="justify">Here is a special function for <code>xrandr</code>:</p>
<div class="codeblock"># function to contorl xrandr
# EXAMPLE: projctl 1024x768
projctl () {
if [ $1 ] ; then
if [ $1 = "-h" ]; then
echo "Usage: projctl [ off/resolution ]"
return
fi
if [ $1 = "off" ]; then
echo "Disable VGA1"
xrandr --output VGA1 --off --output LVDS1 --mode 1366x768
else
echo "Using resolution: $1"
xrandr --output VGA1 --mode $1 --output LVDS1 --mode $1
fi
else
echo "Using default resolution"
xrandr --output VGA1 --mode 1366x768 --output LVDS1 --mode 1366x768
fi
}</div>
<p align="justify">Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
<div class="codeblock"># function to extract archives
# EXAMPLE: unpack file
unpack () {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xf $1 ;;
*.tbz) tar xjvf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'" ;;
esac
else
case $1 in
*help) echo "Usage: unpack ARCHIVE_NAME" ;;
*) echo "'$1' is not a valid file" ;;
esac
fi
}
# function to create archives
# EXAMPLE: pack tar file
pack () {
if [ $1 ]; then
case $1 in
tar.bz2) tar -cjvf $2.tar.bz2 $2 ;;
tar.gz) tar -czvf $2.tar.bz2 $2 ;;
tar.xz) tar -cf - $2 | xz -9 -c - > $2.tar.xz ;;
bz2) bzip $2 ;;
gz) gzip -c -9 -n $2 > $2.gz ;;
tar) tar cpvf $2.tar $2 ;;
tbz) tar cjvf $2.tar.bz2 $2 ;;
tgz) tar czvf $2.tar.gz $2 ;;
zip) zip -r $2.zip $2 ;;
7z) 7z a $2.7z $2 ;;
*help) echo "Usage: pack TYPE FILES" ;;
*) echo "'$1' cannot be packed via pack()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}</div>
<p align="justify">Here is a special function for <code>su</code>:</p>
<div class="codeblock">su () {
checksu=0
for flags in $*; do
if [[ $flags == "-" ]]; then
checksu=1
fi
done
if [[ $checksu == 0 ]]; then
echo "Use 'su -', Luke"
/usr/bin/su - $*
else
/usr/bin/su $*
fi
}</div>
<p align="justify">Functions with automatic rehash after installing/removing packages are:</p>
<div class="codeblock">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
}</div>
<p align="justify">But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
<h3><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
<p align="justify">It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
<p align="justify">Here are path, mask of new files, editor and pager:</p>
<pre># path
export PATH="$PATH:$HOME/.local/bin"
# umask
umask 022
# editor
export EDITOR="vim"
export PAGER="vimpager"</pre>
<p align="justify">Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
<pre># hash
hash -d global=/mnt/global
hash -d windows=/mnt/windows
hash -d iso=/mnt/iso
hash -d u1=/mnt/usbdev1
hash -d u2=/mnt/usbdev2</pre>
<h3><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
<p align="justify"><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
<h3><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
<p align="justify"><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>

View File

@ -4,14 +4,6 @@ title: arcanis' blog
comment: false
share: false
back: 1
posts:
- title: About zshrc
date: 14 January 2014
last: 14 January 2014
tags:
- zshrc
- configuration
- linux
---
<body>
<div class="wrapper">
@ -27,4 +19,33 @@ posts:
<p align="justify"><b>Tags</b>: {{ post.tags }}</p>
{% endfor %}
{% if paginator.total_pages > 1 %}
<nav><ol>
{% if paginator.previous_page > 1 %}
{% capture page_url %}page{{ page }}{% endcapture%}
{% endif %}
{% if paginator.previous_page %}
<li><a href="/{{ page_url }}" title="Go to previous page">&laquo;</a></li>
{% endif %}
<li>
{% for page in (1..paginator.total_pages) %}
<li>
{% if page > 1 %}
{% capture page_url %}page{{ page }}{% endcapture%}
{% endif %}
{% if page == paginator.page %}
{{ page }}
{% else %}
<a href="/{{ page_url }}">{{ page }}</a>
{% endif %}
</li>
{% endfor %}
{% if paginator.next_page %}
<li>
<a href="/page{{ paginator.next_page }}" title="Go to next page">&raquo;</a>
</li>
{% endif %}
</ol></nav>
{% endif %}
<script type="text/javascript" src="http://datejs.googlecode.com/svn/trunk/build/date-en-US.js"></script>