Skip to content

robturtle/.zsh

Repository files navigation

This is my collection of zsh configurations. And since dozens of my friends are not familiar with the shell. So I make many newbie tips along this document.

Prerequisites

Meta Key

Many great shell key bindings are inheritant from Emacs and hence the Meta key is of good use. To enbale this, configure your iTerm2 at:

iTerm > Preferences > Profile > Keys
both options to +Esc. Now option keys act like Meta keys.

https://www.dropbox.com/s/rbhpkiwlo8wx09f/iterm-meta-key.png?raw=1

Installation

In the .zsh directory, call the ./INSTALL.sh to install it. The best practice is install .zsh folder under your home directory. Here is an example:

cd ~
git clone https://github.com/robturtle/.zsh.git
cd .zsh
chmod +x INSTALL.sh
./INSTALL.sh

NOTE: If you haven’t install oh-my-zsh, the oh-my-zsh installer will run a new zsh shell after the installation finished. Please press Ctrl-D to quit this shell to make the INSTALL.sh continue.

https://www.dropbox.com/s/jvay2wa8jhzt4eg/install-howto.png?raw=1

This is based on Oh-my-zsh, if you didn’t installed it or ~/.oh-my-zsh/ is not found, the install script will install it automatically.

After the installation, you can simply restart your terminal to see the changes.

Custom settings

By default, you can store your custom configs in ~/.zsh.my.sh. Typically there are few variable you may be interested to change:

DEFAULT_USER
tell zsh your default user’s name so that you can log in without prompting current username
INSTALLER
the default is “brew install” for Mac users. If you’re a Linux user you must change this to your own package install command to make sure the auto-install facility works.
my_zsh_mod
specify plugins you want to load. You can view all available plugins in plugins folder.
MY_JAVA_CP
used in java plugin. indicates where you preferred to store downloaded jar files. default value is “~/include/java/”
ZSH_THEME
my favorite is “agnoster” but it need fonts installed. read the Powerline Theme below for details.

The examples are:

#!/bin/zsh
# file: ~/.zsh.my.sh
DEFAULT_USER=jack
INSTALLER='sudo apt-get install'
my_zsh_mod=(python java)
MY_JAVA_CP='~/include/java/'
ZSH_THEME="agnoster" # make sure you've installed the specail fonts before setting it

During the installation process, the script will ask you some common settings. You can type in the value, or, just press enter to apply the default value.

Added Functions

I’ve collects several good functions from the web. Amount them I think there are 2 functions totally changed my life in the shell.

The first one is ranger – a file system manager in command line. Its key bindings are simulating the vim. Its common used keys are listed at the ranger section.

The second one is a shell history searching utility which make benifits from an interactive querying libpercol. With this you type some partial contents, separates keywords with whitespaces. And then the percol will filter the results for you.

namefunctionrank
Ctrl+Rsearch shell historyKiller
Ctrl-Sfile managerKiller
fffile partial searchingHelper
pclipstdin into clipboardHelper

Powerline Theme

Special fonts

The special fonts is to support the pertty display of powerline theme like:

https://www.dropbox.com/s/bty5zanck06jacj/zsh-powerline.png?raw=1

Install the powerline fonts following instructions below:

  1. First, Download and install fonts from here
  2. Then, configure your iTerm2 fonts settings
iTerm > Preferences > Profile > Text
change both Regular Font and Non-ASCII Font.

https://www.dropbox.com/s/7pbuhvb8m9a6zf0/iterm-fonts-settings.png?raw=1

Change the theme

Now you can switch your theme into this pretty powrline theme. In your ~/.zsh.my.rc, add this line:

# file: ~/.zsh.my.sh
... # all other variables
ZSH_THEME="agnoster"

Then restart your shell.

iTerm2 Color theme

I use solarized-light and solarized-dark theme in the iTerm2. They are automatically downloaded into your ~/Download/ directory when you run the INSTALL.sh. Double click them you can import this color theme into the iTerm2.

https://www.dropbox.com/s/felgtfctujkuafu/iterm-import-colors.png?raw=1

https://www.dropbox.com/s/jv9jyuadw5p2j6l/iterm-import-colors-success.png?raw=1

To switch to this theme, go to iTerm2’s preference:

iTerm > Preferences > Profile > Colors > Load Presets
And choose “Solarized Light” or “Solarized Dark”

https://www.dropbox.com/s/8nshb4cy0ekjbfz/iterm-colors.png?raw=1

As an advise, I suggest you only use the dark color theme when the environment is dark or you have a monitor with bad quality.

Keyboard shortcuts

For newbies

Common Emacs shortcuts in shell

Most of the shell supports Emacs key bindings by default.

Let ‘C’ stands for Ctrl, ‘M’ stands for ‘Meta’.

keyfunctionmutationfunction
C-Abeginning of line
C-EE-nd of line
C-KK-ill to end of line
C-UU-ntype words
C-DD-elete forwardM-Done word
BACKSPACE/C-Hdelete backwardM-BACKSPACEone word
C-Fmove F-orward one charM-Fone word
C-Bmove B-ackward one charM-Bone word
C-PP-revious command
C-NN-ext command
C-Lclear screen

Being familiar with the Meta shortcuts will makes your shell using more effective.

Common vim shorcuts in shell

in man, less, these vim style key bindings are also usable.

keyfunction
jdown
kup
hleft
lright
C-Dpage D-own
C-Upage U-p
ggfirst line
Glast line
slashsearch

ranger

keyfunction
SPACEmark/unmark
madd bookmark
`goto bookmark
j/k/h/lnavigates
yycopy marked files
ddcut marked files
pppaste
:deletedelete marked files
:mkdirmake directory
zhtoggle show hidden files
qquit

git

namefunctionrank
gitkallshow all branches in gitkHelper

Troubleshooting

js module: npm ERROR: permission denied

In my js mod file I prefer use npm install -g without sudo. Hence you may probably either remove “js” from my_zsh_mod in ~/.zsh.my.sh or fix the permission of the installation destination.

For Mac users, you may fix it by the command like chown -R ${USER} /usr/local regarding to brew’s convention.

python module: Everytime I open a new shell, it tries to install some python packages with pip

I use `python -c “import ${package}”` to check if the package is installed. If you encounter this situtation, that means you didn’t configure your Python import path properly. Please remove “python” from my_zsh_mod in ~/.zsh.my.sh until you read about the Python’s sys.path and configure it right.