DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Notes about zsh


Install zsh (apt-get install zsh) and oh-my-zsh. Set zsh as default shell: chsh -s /bin/zsh;

The way plugins of oh-my-zsh work is like vim-pathogen. To use a plugin of zsh, clone a git repo into the plugin folder ($ZSH_CUSTOM, the default value is ~/.oh-my-zsh/custom), and add its name into the plugin list in ~/.zshrc, and restart the zshell.

So you needn't add many source /usr/share/autojump/autojump.zsh, source $HOME/.asdf/asdf.sh, etc into ~/.zshrc manually.

Useful plugins

Install the following packages:

apt install fonts-powerline autojump

Change color and prompt style by changing the value of "ZSH_THEME" in .zshrc. Or add your own theme file (say lichao.zsh-theme) in $ZSH_CUSTOM.

Finally add the following lines into ~/.zshrc:

export ZSH=$HOME/.oh-my-zsh
ZSH_THEME="lichao"
plugins=(asdf git gitfast httpie web-search vi-mode autojump copyfile
         colored-man-pages zsh-autosuggestions zsh-syntax-highlighting)

For installation details see:

  • zsh-syntax-highlighting

  • zsh-autosuggestions

  • oh-my-zsh plugin list

Usage

path navigation

The tab-completion function of bash can only auto-complete one path name at a time. While that of zsh auto complete multi-levels path at a time. So if you want go to ~/dir1/SubDir2/SubSubdir3/Target, instead of type "cd blah blah blah", just type "~/d/s/s/t" and you got there. Note that path recognition in zsh is not case sensitive, so no need to care about capital letters. This make path navigation really fast and meaningful.

You can ommit "cd" command before change current directory (make sure option AUTO_CD is turned on). So if you want go to parent folder, ".." is ok. If you want come back, "-" or "1". If you want go to ./apps/eclipse-java-juno-SR2-linux-gtk/p2/org.eclipse.equinox.p2.core/cache/binary, just type: "apps" then a list will be displayed and you can use to select among them.

Use "Alt+Backspace" to delete a path name (not a character).

Use d to list all visited folders. Use 1~9 to navigate to folders in the recent visited list. Because 0 is always the current folder, you can just use "1" to switch between two folders.

autojump

With autojump, you can use j folder_name_part1 part2 ... to jump to a directory you have previously visited. For example: "j gaia" will jump to "/home/bvt/.jenkins/jobs/GAIA", or "j gaia/works" to "/home/bvt/.jenkins/jobs/GAIA/workspace".

Use autojump -s or j -s to show all entries in autojump database with their weight. Use "j" to jump to the item with highest weight. So you can have a "favorite list" based on the weight.

If you want change a item to another folder, say the old destination of j work is to ~/docs/jshome/workspace, and now you want it to ~/docs/haskell/workspace.

First find out their weight respectively: j -s|grep workspace. For now the weight of jshome is 122.3, and that of the haskell is 90.4. So this can be achieved by decrease the weight of jshome less than that of haskell:

j work
j -d 50

This makes the weight of jshome to 72.3. Use j -s|grep ... above, you can find now haskell has the highest weight. And j work take you to ~/docs/haskell/workspace now.

If you have 2 "newfairs" folder, one for source codes and one for devops: "~/docs/website/v3/codes/newfairs" and "~/docs/website/v3/devOps/ansible/newfairs", use j co new to jump to the former, and j ops new to jump to latter.

Use j --purege to remove invalid path due to path changes.

See j -h or autojump --help for details.

find files

use ls **/*.zip instead of find . -name *.zip;

edit files

use "vi /gaia//build.xml" instead of giving the actual path of the file;

command history search

use "Ctrl + R" to activate command search mode;

other type-saving tricks

"less REMDME" -> "<README"

open file in zsh

alias -s pdf=exo-open will use exo-open to open pdf files. Now you can use ./book.pdf to open it in Document Viewer as if it's a executable command.

alias -s pdf=evince has the same effect with previous "exo-open" method on Ubuntu, except that pdf application (Document Viewer, or evince) runs in foreground, which means zsh hangs when evince runs. So "exo-open" is a better choice for it acts like double click file in GUI file manager (like Thunar).

But on Ubuntu, evince is builtin app, while exo-open not.

If you only want this in a session, define it in command line. If you want system remember this, add it into .zshrc.

To open all file associations already defined in system, put the following lines in .zshrc:

autoload -U zsh-mime-setup

zsh-mime-setup

Ref: Replacing the File Manager in Zsh

Use alias -s to list all file extension associations defined.

Use unalias -s pdf to remove the association (or remove alias command from .zshrc).

Cheatsheet

  • take: "take myfolder" equals to "mkdir myfolder;cd myfolder";

  • x: extract some file, including zip, tar, gz, bz2, tgz, 7z, etc file;

  • zsh_stats: print top 20 frequently used commands under zsh;

  • upgrade_oh_my_zsh: upgrade Oh-my-zsh;

  • uninstall_oh_my_zsh: uninstall Oh-my-zsh;



Published

Jul 6, 2013

Last Updated

Jun 27, 2019

Category

Tech

Tags

  • autojump 3
  • shell 46
  • zsh 5

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor