avatarkazu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5175

Abstract

word">the</span> <span class="hljs-built_in">shell</span> easier.</pre></div><div id="270e"><pre><span class="hljs-attribute">You can</span><span class="hljs-punctuation">:</span></pre></div><div id="36f4"><pre>(q) Quit <span class="hljs-keyword">and</span> <span class="hljs-keyword">do</span> <span class="hljs-keyword">nothing</span>. The <span class="hljs-keyword">function</span> will be run again next <span class="hljs-type">time</span>.</pre></div><div id="63fd"><pre>(<span class="hljs-number">0</span>) Exit, creating <span class="hljs-keyword">the</span> <span class="hljs-built_in">file</span> /.zshrc containing just <span class="hljs-keyword">a</span> comment. That will prevent this <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">being</span> <span class="hljs-title">run</span> <span class="hljs-title">again</span>.</span></pre></div><div id="8403"><pre>(<span class="hljs-number">1</span>) Continue <span class="hljs-selector-tag">to</span> the <span class="hljs-selector-tag">main</span> <span class="hljs-selector-tag">menu</span>.</pre></div><div id="08ad"><pre>(<span class="hljs-number">2</span>) Populate your /.zshrc with the configuration recommended by the system administrator and <span class="hljs-keyword">exit</span> (you will need to edit the file by hand, <span class="hljs-keyword">if</span> so desired).</pre></div><div id="4e3e"><pre><span class="hljs-comment">--- Type one of the keys in parentheses ---</span></pre></div><p id="2c2d">Of course, you can choose what you like, but here, let’s Type “q” to quit the screen and create the configuration file by yourself.</p><h1 id="0520">Creating .zshrc</h1><p id="d5e7">The next step is to configure the <code>zsh</code> . You can do it by creating <code>/.zshrc</code>. This file is equivalent to <code>/.bashrc</code> in <code>bash</code>. You can define the way of completion, where to store command history, and so on in the file. You can see the detailed document in the links below.</p><div id="5236" class="link-block"> <a href="http://zsh.sourceforge.net/Doc/Release/Options.html"> <div> <div> <h2>16 Options</h2> <div><h3>zsh: 16 Options Options are primarily referred to by name. These names are case insensitive and underscores are…</h3></div> <div><p>zsh.sourceforge.net</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><div id="356f" class="link-block"> <a href="http://zsh.sourceforge.net/Doc/Release/Completion-System.html"> <div> <div> <h2>20 Completion System</h2> <div><h3>This describes the shell code for the 'new' completion system, referred to as compsys. It is written in shell functions…</h3></div> <div><p>zsh.sourceforge.net</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="d2d9">I know it is hard to write your own <code>zshrc</code> file from scratch. So I recommend you copy-and-pasting first, and then, modify it as you like.</p><p id="bcec">Here is the part of my <code>.zshrc</code>. Let’s copy and paste it to make your initial <code>.zshrc</code>. Save the <code>.zshrc</code> file in your home directory.</p><div id="ca54"><pre>################################# HISTORY #################################

history

HISTFILE=$HOME/.zsh-history # file to save history HISTSIZE=<span class="hljs-number">100000</span> # history size <span class="hljs-keyword">in</span>-memory SAVEHIST=<span class="hljs-number">1000000</span> # history size <span class="hljs-keyword">in</span> the file</pre></div><div id="0fac"><pre><span class="hljs-comment"># share .zshhistory</span> <span class="hljs-built_in">setopt</span> inc_append_history <span class="hljs-comment"># save the command to history when running</span> <span class="hljs-built_in">setopt</span> share_history <span class="hljs-comment"># share history with other session</span></pre></div><div id="6f10"><pre>################################# COMPLEMENT #################################

enable completion

autoload -Uz compinit && compinit</pre></div><div id="966b"><pre><span class="hljs-built_in">zstyle</span> <span class="hljs-string">':completion:'</span> matcher-list <span class="hljs-string">''</span> <span class="hljs-string">'m:{[:lower:]}={[:upper:]}'</span> <span class="hljs-string">'+m:{[:upper:]}={[:lower:]}'</span> <span class="hljs-built_in">zstyle</span> <span class="hljs-string">':completion:'</span> format <span class="hljs-string">'%B%F{blue}%d%f%b'</span> <span class="hljs-built_in">zstyle</span> <span class="hljs-string">':completion:*'</span> group-name <span class="hljs-string">''</span></pre></div><div id="03e9"><pre>zs

Options

tyle <span class="hljs-string">':completion:*:default'</span> menu select=<span class="hljs-number">2</span> ################################# OTHERS #################################

automatically change directory when dir name is typed

setopt auto_cd</pre></div><div id="17e7"><pre><span class="hljs-comment"># disable ctrl+s, ctrl+q</span> <span class="hljs-built_in">setopt</span> no_flow_control</pre></div><p id="c68b">After saving the file, you can load it by the following command.</p><div id="4d1c"><pre><span class="hljs-built_in">source</span> ~/.zshrc</pre></div><p id="5022">If the setting has done correctly, the tab completion will be work as shown below.</p><figure id="3d04"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7GE1TC_oi9tBS8-qGHKm1g.png"><figcaption>Tab completion for ‘ls’</figcaption></figure><p id="a024">OK! Now you learn how to customize your <code>zsh</code>!</p><h1 id="b270">Dotfiles</h1><p id="9cc7">The next topic is “Dotfiles”.</p><p id="703a">In the previous section, you made your original <code>.zshrc</code>. I know you are already a fan of <code>zsh</code>, so you will edit your setting file again and again. In such a case, you’d like to put it under the version control system.</p><p id="9376">The word “Dotfiles” means the idea of using git (or other VCS) to organize setting files like <code>.zshrc</code>. You can organize not only <code>.zshrc</code> but also like</p><ul><li><code>.vimrc</code></li><li><code>.tmux.conf</code></li></ul><p id="ecef">and so on. Using dotfiles is very easy. All you have to do is,</p><ul><li>1. put the config files in a directory.</li><li>2. <code>git init</code></li><li>3. Create the file to deploy these setting files.</li></ul><p id="b304">Let’s do it together!</p><h2 id="3ee2">1. put the config files in a directory</h2><p id="2831">First, you have to create a directory to store the setting files.</p><div id="34b9"><pre><span class="hljs-built_in">mkdir</span> ~/dotfiles</pre></div><p id="f971">Now you have the directory named “dotfiles” in your home directory. Then, move your <code>.zshrc</code> to the directory.</p><div id="2f08"><pre><span class="hljs-built_in">mv</span> ~/.zshrc /dotfiles</pre></div><div id="cdfa"><pre><span class="hljs-comment"># confirm</span> <span class="hljs-built_in">ls</span> -la /dotfiles <span class="hljs-comment"># -> you could see .zshrc</span></pre></div><h2 id="bd2d">2. git init</h2><p id="deb3">The second task is to make it a Git directory.</p><div id="0c76"><pre><span class="hljs-keyword">cd</span> <span class="hljs-string">/dotfiles</span> git init git add <span class="hljs-string">.zshrc</span> git commit -m <span class="hljs-string">"init"</span></pre></div><p id="497b">That it! Now you can try any setting you want and revert it easily thanks to Git. I recommend you make a GitHub repository for a remote one.</p><h2 id="f272">3. Create the file to deploy these setting files.</h2><p id="2cd0">Now your <code>.zshrc</code> is in <code>/dotfiles</code>, so your <code>zsh</code> can not find your setting file as <code>/.zshrc</code> anymore. Let’s resolve the problem by creating a symbolic link to <code>/.zshrc</code>. You can make it by using <code>ln</code> command.</p><div id="e6bd"><pre><span class="hljs-built_in">ln</span> -sv ~/dotfiles/.zshrc ~/ <span class="hljs-comment"># Confirm</span> <span class="hljs-built_in">ls</span> -la ~/ <span class="hljs-comment"># -> you could see the symlink</span></pre></div><p id="b8e1">You can make the file to deploy your setting file like below.</p><div id="b014"><pre><span class="hljs-meta">#!/bin/bash</span> dotfiles = (.zshrc)</pre></div><div id="24db"><pre><span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">dotfiles</span>[@]"</span>; <span class="hljs-keyword">do</span> <span class="hljs-built_in">ln</span> -svf <span class="hljs-variable">file</span> / <span class="hljs-keyword">done</span></pre></div><p id="c503">Make the file with upper content and name it <code>/dotfiles/deploy.sh</code>. By running this script, you can make the symbolic link automatically. Don’t forget to add it to Git!</p><h1 id="6a0c">Conclusion</h1><p id="39c4">In this article, you learned</p><ul><li>How to install <code>zsh</code></li><li>How to setup</li><li>What’s the “dotfiles”</li></ul><p id="f2e0">Now you can customize your <code>zsh</code>!</p><p id="9e8a">And here is my dotfiles repository.</p><div id="306e" class="link-block"> <a href="https://github.com/kazu914/dotfiles"> <div> <div> <h2>kazu914/dotfiles</h2> <div><h3>My dotfiles repository. Contribute to kazu914/dotfiles development by creating an account on GitHub.</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*2p-ZwpXxjiy8w3ZT)"></div> </div> </div> </a> </div><p id="61a6">Let’s enjoy your terminal life!</p></article></body>

The Definitive Guide: Zsh and Dotfiles.

In this article, I will show you these contents.

  • How to install zsh
  • How to setup
  • What’s the “dotfiles”

Who should read this article?

The main targets of the article are

  • Who willing to customize the terminal
  • Who wants to get familiar with the terminal
  • Who’d like to be more effective in the terminal
  • Who doesn’t know about zsh or dotfiles

If you are already well known about zsh and terminal, this article is not for you.

About zsh

First of all, let’s take a brief look at what is zsh. zsh is defined as below.

“Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being compatible with the POSIX sh (not by default, only if issuing emulate sh), it offers advantages such as improved tab completion and globbing.”

from https://wiki.archlinux.org/title/zsh

Simply put, zsh is a kind of interactive shell, like

  • bash
  • fish
  • csh
  • Nu Shell

and so on.

I guess almost all of you, the target of this article, are currently using bash or zsh now, because these two are (or were) used as the default shells for Linux and Mac OS X. Here, the difference between bash and zsh is that zsh is something like extended-bash. So, if you are using bash without custom settings, you can move on to zsh easily. If you’d like to know the exact differences, you can google it.

How to install zsh

Ok, now you know what is zsh , let’s move on to how to install it.

If you are a mac user, you can do it with brew .

brew install zsh

If you are using Linux, package managers will do it.

sudo apt install zsh
# or
sudo yum install zsh

Confirm the installation

If the installation is done, you can check it by using which command.

which zsh
# -> /usr/bin/zsh

Did it!! You are almost there!

Set zsh as the login shell

Next, let’s set zsh as your login shell. To do so, first, let’s confirm that your /etc/shells has the path of zsh .

# checking /etc/shells
cat /etc/shells
# If there are no line related zsh, you should add it
sudo sh -c "echo $(which zsh) >> /etc/shells"

If you’d like to use some shell as a login shell, you have to add the path of the shell to /etc/shells .

Next, you can set the shell by using chsh (change shell) command.

chsh -s $(which zsh)

Done! Now you can see these outputs below after log-out and log-in and open terminal.

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.
You can:
(q)  Quit and do nothing.  The function will be run again next time.
(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.
(1)  Continue to the main menu.
(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).
--- Type one of the keys in parentheses ---

Of course, you can choose what you like, but here, let’s Type “q” to quit the screen and create the configuration file by yourself.

Creating .zshrc

The next step is to configure the zsh . You can do it by creating ~/.zshrc. This file is equivalent to ~/.bashrc in bash. You can define the way of completion, where to store command history, and so on in the file. You can see the detailed document in the links below.

I know it is hard to write your own zshrc file from scratch. So I recommend you copy-and-pasting first, and then, modify it as you like.

Here is the part of my .zshrc. Let’s copy and paste it to make your initial .zshrc. Save the .zshrc file in your home directory.

#################################  HISTORY  #################################
# history
HISTFILE=$HOME/.zsh-history # file to save history
HISTSIZE=100000             # history size in-memory
SAVEHIST=1000000            # history size in the file
# share .zshhistory
setopt inc_append_history   # save the command to history when running
setopt share_history        # share history with other session
#################################  COMPLEMENT  #################################
# enable completion
autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' '+m:{[:upper:]}={[:lower:]}'
zstyle ':completion:*' format '%B%F{blue}%d%f%b'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:default' menu select=2
#################################  OTHERS  #################################
# automatically change directory when dir name is typed
setopt auto_cd
# disable ctrl+s, ctrl+q
setopt no_flow_control

After saving the file, you can load it by the following command.

source ~/.zshrc

If the setting has done correctly, the tab completion will be work as shown below.

Tab completion for ‘ls’

OK! Now you learn how to customize your zsh!

Dotfiles

The next topic is “Dotfiles”.

In the previous section, you made your original .zshrc. I know you are already a fan of zsh, so you will edit your setting file again and again. In such a case, you’d like to put it under the version control system.

The word “Dotfiles” means the idea of using git (or other VCS) to organize setting files like .zshrc. You can organize not only .zshrc but also like

  • .vimrc
  • .tmux.conf

and so on. Using dotfiles is very easy. All you have to do is,

  • 1. put the config files in a directory.
  • 2. git init
  • 3. Create the file to deploy these setting files.

Let’s do it together!

1. put the config files in a directory

First, you have to create a directory to store the setting files.

mkdir ~/dotfiles

Now you have the directory named “dotfiles” in your home directory. Then, move your .zshrc to the directory.

mv ~/.zshrc ~/dotfiles
# confirm
ls -la ~/dotfiles
# -> you could see .zshrc

2. git init

The second task is to make it a Git directory.

cd ~/dotfiles
git init
git add .zshrc
git commit -m "init"

That it! Now you can try any setting you want and revert it easily thanks to Git. I recommend you make a GitHub repository for a remote one.

3. Create the file to deploy these setting files.

Now your .zshrc is in ~/dotfiles, so your zsh can not find your setting file as ~/.zshrc anymore. Let’s resolve the problem by creating a symbolic link to ~/.zshrc. You can make it by using ln command.

ln -sv ~/dotfiles/.zshrc ~/
# Confirm
ls -la ~/
# -> you could see the symlink

You can make the file to deploy your setting file like below.

#!/bin/bash
dotfiles = (.zshrc)
for file in "$dotfiles[@]"; do
    ln -svf $file ~/
done

Make the file with upper content and name it ~/dotfiles/deploy.sh. By running this script, you can make the symbolic link automatically. Don’t forget to add it to Git!

Conclusion

In this article, you learned

  • How to install zsh
  • How to setup
  • What’s the “dotfiles”

Now you can customize your zsh!

And here is my dotfiles repository.

Let’s enjoy your terminal life!

Shell
Programming
Beginners Guide
Zsh
Dotfiles
Recommended from ReadMedium