mirror of
https://github.com/arcan1s/arcanis.me.git
synced 2025-07-17 14:59:55 +00:00
updated papers
Refactoring of an English part is done
This commit is contained in:
@ -6,19 +6,22 @@ hasTr: true
|
||||
tags: zshrc, configuration, linux
|
||||
title: About zshrc
|
||||
short: about-zshrc
|
||||
description: It is first paper in my blog (I think I need something here for tests =)). There are many similar articles, and I'll not be an exception. I just want to show my <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>).
|
||||
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/" title="Forum thread">original</a>).
|
||||
---
|
||||
<h2><a name="prepare" class="anchor" href="#prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
|
||||
<h2><a href="#prepare" class="anchor" name="prepare"><span class="octicon octicon-link"></span></a>Prepare</h2>
|
||||
<p>First install recommended minima:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
pacman -Sy pkgfile zsh zsh-completions zsh-syntax-highlighting
|
||||
{% endhighlight %}
|
||||
<p><a href="https://www.archlinux.org/packages/pkgfile/">pkgfile</a> is a very useful utility. Also this command will install shell, additional completion and syntax highlighting.</p>
|
||||
|
||||
<h2><a name="configuration" class="anchor" href="#configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
|
||||
<p>All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html">here</a>.</p>
|
||||
<p><a href="https://www.archlinux.org/packages/pkgfile/" title="Archlinux package">pkgfile</a> is a very useful utility. Also this command will install shell, additional completion and syntax highlighting.</p>
|
||||
|
||||
<h2><a href="#configuration" class="anchor" name="configuration"><span class="octicon octicon-link"></span></a>Shell configuration</h2>
|
||||
<p>All options are avaible <a href="http://zsh.sourceforge.net/Doc/Release/Options.html" title="zsh documentation">here</a>.</p>
|
||||
|
||||
<p>Set history file and number of commands in cache of the current session and in the history file:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# history
|
||||
HISTFILE=~/.zsh_history
|
||||
@ -27,6 +30,7 @@ SAVEHIST=500000
|
||||
{% endhighlight %}
|
||||
|
||||
<p>I can not remember all <code>Ctrl+</code> combinations so I bind keys to its default usages:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# bindkeys
|
||||
bindkey '^[[A' up-line-or-search # up arrow for back-history-search
|
||||
@ -38,9 +42,11 @@ 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
|
||||
{% endhighlight %}
|
||||
|
||||
<p>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>Command autocomplete:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# autocomplete
|
||||
autoload -U compinit
|
||||
@ -48,9 +54,11 @@ compinit
|
||||
zstyle ':completion:*' insert-tab false
|
||||
zstyle ':completion:*' max-errors 2
|
||||
{% endhighlight %}
|
||||
|
||||
<p>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>Prompt:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# promptinit
|
||||
autoload -U promptinit
|
||||
@ -58,6 +66,7 @@ promptinit
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Enable colors:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# colors
|
||||
autoload -U colors
|
||||
@ -66,53 +75,71 @@ colors
|
||||
|
||||
<p>Here are some other options.</p>
|
||||
<p>Change directory without <code>cd</code>:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# autocd
|
||||
setopt autocd
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Correcting of typos (and question template):</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# correct
|
||||
setopt CORRECT_ALL
|
||||
SPROMPT="Correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) "
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Disable f#$%ing beep:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# disable beeps
|
||||
unsetopt beep
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Enable calculator:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# calc
|
||||
autoload zcalc
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Append history (<b>do not recreate</b> the history file):</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# append history
|
||||
setopt APPEND_HISTORY
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Do not save dups to history file:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# ignore spaces in history
|
||||
setopt HIST_IGNORE_ALL_DUPS
|
||||
{% endhighlight %}
|
||||
|
||||
<p>...and additional spaces:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# ignore dups in history
|
||||
setopt HIST_IGNORE_SPACE
|
||||
{% endhighlight %}
|
||||
|
||||
<p>...and blank lines too:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# reduce blanks in history
|
||||
setopt HIST_REDUCE_BLANKS
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Enable <code>pkgfile</code>:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# pkgfile
|
||||
source /usr/share/doc/pkgfile/command-not-found.zsh
|
||||
{% endhighlight %}
|
||||
|
||||
<h2><a name="highlighting" class="anchor" href="#highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
|
||||
<h2><a href="#highlighting" class="anchor" name="highlighting"><span class="octicon octicon-link"></span></a>Syntax highlighting</h2>
|
||||
|
||||
{% highlight bash %}
|
||||
# highlighting
|
||||
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
|
||||
@ -159,10 +186,12 @@ ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
|
||||
# root example
|
||||
#ZSH_HIGHLIGHT_STYLES[root]='bg=red'
|
||||
{% endhighlight %}
|
||||
|
||||
<p>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>
|
||||
|
||||
<h2><a name="prompt" class="anchor" href="#prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
|
||||
<h2><a href="#prompt" class="anchor" name="prompt"><span class="octicon octicon-link"></span></a>$PROMPT and $RPROMPT</h2>
|
||||
<p>The general idea is the use single <code>.zshrc</code> for root and normal user:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# PROMPT && RPROMPT
|
||||
if [[ $EUID == 0 ]]; then
|
||||
@ -185,6 +214,7 @@ fi
|
||||
{% endhighlight %}
|
||||
|
||||
<p><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>
|
||||
|
||||
{% highlight bash %}
|
||||
black
|
||||
red
|
||||
@ -197,6 +227,7 @@ white
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Avaible variables are:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
%n - the username
|
||||
%m - the computer's hostname (truncated to the first period)
|
||||
@ -213,6 +244,7 @@ white
|
||||
{% endhighlight %}
|
||||
|
||||
<p>RPROMPT (<code>acpi</code> package is necessary):</p>
|
||||
|
||||
{% highlight bash %}
|
||||
precmd () {
|
||||
# battery charge
|
||||
@ -237,12 +269,14 @@ $(batcharge)\
|
||||
$returncode\
|
||||
"%{$fg_bold[white]%}]%{$reset_color%}"
|
||||
{% endhighlight %}
|
||||
|
||||
<p>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>
|
||||
|
||||
<h2><a name="aliases" class="anchor" href="#aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
|
||||
<h2><a href="#aliases" class="anchor" name="aliases"><span class="octicon octicon-link"></span></a>Aliases</h2>
|
||||
<p><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>Small useful (or maybe not) function:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
show_which() {
|
||||
OUTPUT=$(which $1 | cut -d " " -f7-)
|
||||
@ -251,6 +285,7 @@ show_which() {
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here is the first group of aliases:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
## alias
|
||||
# colored grep
|
||||
@ -269,7 +304,8 @@ alias less='vimpager'
|
||||
alias zless='vimpager'
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls">man ls</a>):</p>
|
||||
<p>Here are ls aliases (see <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ls" title="Man page">man ls</a>):</p>
|
||||
|
||||
{% highlight bash %}
|
||||
alias ls='show_which ls && ls --color=auto --group-directories-first'
|
||||
alias ll='show_which ll && ls -l --human-readable'
|
||||
@ -282,6 +318,7 @@ alias lm='show_which lm && la | more'
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here are aliases to quick file view from console (just type a file name!):</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# alias -s
|
||||
alias -s {avi,mpeg,mpg,mov,m2v,mkv}=mpv
|
||||
@ -293,6 +330,7 @@ alias -s {html,htm}=opera
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here are "sudo" aliases:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# sudo alias
|
||||
if [[ $EUID == 0 ]]; then
|
||||
@ -316,6 +354,7 @@ fi
|
||||
{% endhighlight %}
|
||||
|
||||
<p>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>
|
||||
|
||||
{% highlight bash %}
|
||||
# global alias
|
||||
alias -g g="| grep"
|
||||
@ -325,7 +364,7 @@ alias -g h="| head"
|
||||
alias -g dn="&> /dev/null &"
|
||||
{% endhighlight %}
|
||||
|
||||
<h2><a name="functions" class="anchor" href="#functions"><span class="octicon octicon-link"></span></a>Functions</h2>
|
||||
<h2><a href="#functions" class="anchor" name="functions"><span class="octicon octicon-link"></span></a>Functions</h2>
|
||||
<p>Here is a special function for <code>xrandr</code>:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
@ -352,6 +391,7 @@ projctl () {
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Unfortunately I can not remember <code>tar</code> flags thus I use special functions:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# function to extract archives
|
||||
# EXAMPLE: unpack file
|
||||
@ -405,6 +445,7 @@ pack () {
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here is a special function for <code>su</code>:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
su () {
|
||||
CHECKSU=0
|
||||
@ -423,6 +464,7 @@ su () {
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Function that replaces original <code>rm</code> command. If you type <code>rm</code> it will be equivalent moving to trash an you can easily restore a file:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
rm () {
|
||||
# error check
|
||||
@ -466,6 +508,7 @@ rm () {
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Functions with automatic rehash after installing/removing packages are:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
pacman () {
|
||||
/usr/bin/sudo /usr/bin/pacman $* && echo "$*" | grep -q "S\|R\|U" && rehash
|
||||
@ -478,12 +521,14 @@ yatest () {
|
||||
/usr/bin/yaourt --config /etc/pactest.conf $* && echo "$*" | grep -q "S\|R\|U" && rehash
|
||||
}
|
||||
{% endhighlight %}
|
||||
<p>But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205">will require</a> root privileges.</p>
|
||||
|
||||
<h2><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Variables</h2>
|
||||
<p>But autocomplete for <code>yaourt -Ss</code> <a href="https://github.com/zsh-users/zsh-completions/pull/205" title="Issue">will require</a> root privileges.</p>
|
||||
|
||||
<h2><a href="#variables" class="anchor" name="variables"><span class="octicon octicon-link"></span></a>Variables</h2>
|
||||
<p>It is recommended to set own variables in <code>~/.zshenv</code>. But I have everything stored in the single file.</p>
|
||||
|
||||
<p>Here are path, mask of new files, editor and pager:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# path
|
||||
export PATH="$PATH:$HOME/.local/bin"
|
||||
@ -495,6 +540,7 @@ export PAGER="vimpager"
|
||||
{% endhighlight %}
|
||||
|
||||
<p>Here is hashes. If they are enable the command <code>~global</code> will be equivalent the command <code>/mnt/global</code>:</p>
|
||||
|
||||
{% highlight bash %}
|
||||
# hash
|
||||
hash -d global=/mnt/global
|
||||
@ -504,8 +550,12 @@ hash -d u1=/mnt/usbdev1
|
||||
hash -d u2=/mnt/usbdev
|
||||
{% endhighlight %}
|
||||
|
||||
<h2><a name="screenshot" class="anchor" href="#screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
|
||||
<p><a href="/resources/screenshots/zshrc_demo.png"><img src="/resources/preview/zshrc_demo_prev.jpg"></a></p>
|
||||
<h2><a href="#screenshot" class="anchor" name="screenshot"><span class="octicon octicon-link"></span></a>Screenshot</h2>
|
||||
<p>
|
||||
{% assign scrdesc = "Zsh demonstation" %}
|
||||
{% assign scrname = "zshrc_demo" %}
|
||||
{% include prj_scr.html %}
|
||||
</p>
|
||||
|
||||
<h2><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>File</h2>
|
||||
<p><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc">Here is</a> my <code>.zshrc</code>.</p>
|
||||
<h2><a href="#file" class="anchor" name="file"><span class="octicon octicon-link"></span></a>File</h2>
|
||||
<p><a href="https://raw.github.com/arcan1s/dotfiles/master/zshrc" title="GitHub">Here is</a> my <code>.zshrc</code>.</p>
|
||||
|
Reference in New Issue
Block a user