Zsh and oh-my-zsh

Source: Internet
Author: User
Tags git commands

Before starting mactalk today, let's ask two questions:

1. What are the main advantages of Mac over other systems?
2. What shell do you usually use?
......

The first child boots can sit down. The biggest advantage of Mac is the perfect combination of GUI and command line. Don't put all your attention on Mac's sexy waist and bright display. Okay, it's not my sister paper! You can go out to the wall with the second child boots. After talking about mactalk for so many times, you tell me that Windows CMD is still in use. How can you make mac feel? Even if you say you are using Linux Bash, I will forgive you and kill!

The last time I mentioned shell in "How to Learn a programming language", some readers asked questions about shell, so this time I will tell you about shell.

I introduced a little shell in "fun talk about personal website building". I borrowed some of my things and kept them as follows:

Shell is a shell of Linux/Unix. You can understand it as a dress. It is responsible for external interaction with the Linux kernel, receiving commands from users or other applications, and then converting these commands into languages that the kernel can understand and passing them to the kernel, then, return the result to the user or application.

Linux/Unix provides many types of shell. Is it necessary for so many shell? Is it used for fried food? Then I ask you, how are there so many clothes of the same type? Different colors and textures. Writing a program is much more complicated than buying clothes, and programmers are often responsible for making complicated things simple and simple. When a programmer sees an uncomfortable shell, he will write a new set by himself and gradually form some standards. There are several common shell types, such as SH, Bash, and CSH, you can run the following command to check whether your system has several shells:

cat /etc/shells

Shown as follows:

/bin/bash/bin/csh/bin/ksh/bin/sh/bin/tcsh/bin/zsh

Running this command in Linux is slightly different from Mac. You will find that there is a zsh in Mac, that is, the OS X system has a zsh preinstalled. What about this shell?

Currently, the common Linux System and the default shell of OS X are Bash, but the real powerful shell is the hidden zsh, which is definitely a sports car in a carriage, a flying car in a sports car, the "ultimate shell" is called in history. However, the configuration is too complicated, so no one cares about it in the early stage. Many people come to see the zsh Configuration Guide and leave without saying anything. One day, a very boring foreign programmer developed a zsh project called "Oh my zsh". The GitHub website is https://github.com/robbyrussell/oh-my-zsh. This is like the series "X days tell you to learn C ++", which can make your work more efficient and true.

Now let's take a look at how to install, configure, and use zsh.

Install zsh

If you use Mac, you can view the next section.
If you use RedHat Linux, run: sudo Yum install zsh
If you use Ubuntu Linux, run: sudo apt-Get install zsh.
If you use windows ...... Go to wash and sleep.

After the installation is complete, set the current user to use zsh: CHSH-S/bin/zsh, and enter the current user's password as prompted.

Install oh my zsh

Install git first. The installation method is the same as above. Replace zsh with git.

Install "Oh my zsh" automatically or manually.

Automatic installation:

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

Manual installation:

git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zshcp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

It is not complex. After the installation is complete, exit the current session and open a terminal window again. Then you can see the color prompt:


Configuration

Zsh configurations are mainly concentrated in the. zshrc file in the current directory of the user. Open. zshrc in vim or other editors you like, and you will find such a line at the bottom:

# Customize to your needs...

You can define your own environment variables and aliases here. Of course, oh my zsh has automatically read and set the current environment variables during installation. You can continue to append other environment variables.

Next, set the alias. My own configuration is as follows:

Alias CLS = 'clear' alias LL = 'LS-l' alias LA = 'LS-a' alias Vi = 'vim 'alias javac = "javac-J-Dfile.encoding = utf8" alias grep = "grep -- color = auto" alias-s html = mate # enter the file name suffixed with HTML in the command line, alias-s RB = mate # Open the ruby file in textmate. Alias-s py = VI # Open the python file in textmate, it will be opened in Vim, the following is similar to alias-s JS = vialias-s c = vialias-s Java = vialias-s TXT = vialias-s Gz = 'tar-xzvf 'alias-s tgz = 'tar- xzvf 'Alias-s zip = 'unzip' alias-s bz2 = 'tar-xjvf'

Zsh's cow dung is that it can not only set a general alias, but also set the corresponding open program for the file type, such:

Alias-s html = mate, which means you enter hello.html in the command line. zsh will automatically open textmat for you and read hello.html; alias-s Gz = 'tar-xzvf ', automatically decompress the zip package suffixed with GZ.

In short, you can't do it without thinking of it.

After setting environment variables and aliases, you can basically use them. If you are a theme controller, you can also play with zsh themes. Find zsh_theme in. zshrc to set the topic. The default topic is:

Zsh_theme = "robbyrussell"

Oh my zsh provides dozens of themes ~ /. Under the oh-my-zsh/themes directory, you can select or edit the topic to meet your abnormal needs. I used the default topic robbyrussell, but made a small change:

PROMPT=‘%{$fg_bold[red]%}? %{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$reset_color%}>‘#PROMPT=‘%{$fg_bold[red]%}? %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}‘

In contrast to the original version, I changed C to D, C to the current directory, and D to the absolute path. In addition, I added a ">" at the end. The effect after the change is as follows:

You can try to make changes.

Finally, let's talk about plug-ins.

Plug-ins

The Oh my zsh project provides a complete plug-in system ~ /. By default, the oh-my-zsh/Plugins directory provides more than 100 types. You can use it based on your actual learning and working environment. To learn about the functions of each plug-in, you just need to open the zsh file in the relevant directory and check it. The plug-in is also configured in. zshrc. Find the Plugins keyword and you will be able to load your plug-in. By default, the system loads git. You can append the following content:

plugins=(git textmate ruby autojump osx mvn gradle)

The following is a brief introduction:

1. Git: when you are in a git-controlled directory, the shell will clearly display "Git" and branch, as shown in. In addition, many git commands are simplified, for example, GCO = 'git checkout', GD = 'git diff', GST = 'git status', G = 'git ', and so on. Skilled use can greatly reduce the GIT command length, for more information about the command content, see ~ /. Oh-my-zsh/plugins/git. plugin. zsh

2. textmate: Mr can create Ruby framework projects, and TM finename can use textmate to open the specified file.

3. OSX: tab enhancement. quick-look filename can be used to preview files directly. Man-preview grep can be used to generate a pdf version of The grep manual.

4. autojump: the combination of zsh and autojump forms the most powerful plug-in zsh. Today we will mainly talk about this product.

First install autojump. If you use Mac, use brew to install it:

brew install autojump

For Linux, download the latest version of autojump, for example:

wget https://github.com/downloads/joelthelion/autojump/autojump_v21.1.2.tar.gz

Decompress the package and enter the directory. Execute

./install.sh

Add the following code to. zshrc:

[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh

So far, the three-in-one installation, configuration, and plug-in have fully appeared in the ultimate shell. Log out of the terminal session and log on again to start to feel the zsh training!

Use zsh

1. It is compatible with Bash. It is easy to switch from a bash brother.

2. Powerful history function. Enter grep and use the up and down arrows to view all the grep commands you run.

3. Enter gtep mactalk *-R and the system will prompt zsh: Correct 'gtep' to 'grep' [Nyae]? It's more intimate than sister paper. They always make you guess ......

4. Various completions: path completion, command completion, command parameter completion, and plug-in content completion. To trigger completion, you only need to press the tab key one or two times. You can use Ctrl + N/P/F/B to switch between top, bottom, and left. For example, if you want to kill a Java Process, you only need to enter the kill Java + Tab key. If there is only one Java Process, zsh will be automatically replaced with the PID of the process, if there are multiple options, you can select them. SSH + space + two tab keys, zsh will list all accessed hosts and usernames for completion

5. Smart jump. After autojump is installed, zsh automatically records the directories you have accessed. You can directly perform directory jump using the J + directory name, and the directory name supports fuzzy match and Automatic completion, for example, you have accessed the hadoop-1.0.0 directory, enter J Hado to jump correctly. J-stat can be used to view your historical path library.

6. directory browsing and navigation: enter D to list the directories you have accessed in this session. Enter the sequence number before the list to directly jump.

7. Enter... in the current directory... , Or directly enter the current directory name to jump, you do not even need to enter the CD command.

8. wildcard search: ls-l **/*. Sh. You can recursively display the shell files in the current directory. If there are few files, you can replace find. If there are too many files, you can stop.

9. Stronger aliases: see the configuration section.

10. Plug-in support: see the plug-in section.

......

After reading this article, you will know that as soon as zsh is published, no one will compete with us! The Ultimate 2 character is not covered.

If you are a shell programmer and you are still using bash, go to the wall and confess, let alone you have subscribed to mactalk!

Thanks to the boring programmer who developed oh my zsh, he may not reap material benefits, but his code has improved the efficiency of countless programmers and saved a lot of time, we say that programmers change the world!

Mactalk writes a lot, and friends and family do not quite understand it. It cannot make money and cannot compete for a name. How can it be used? I think it's always better to learn, code, write, and do something that helps others ......

 

 

 

 

The default terminal in Mac is Bash. Is it better? Is zsh.

Install zsh
brew install zsh
Set zsh to the default shell
mate /etc/shells

Add at the end of the article:

/usr/local/bin/zsh

Set zsh to the default shell:

chsh -s /usr/local/bin/zsh

Impress iterm2 again.

Configure zsh

Currently, there are two popular installation configuration files: grml-zsh-config on the official website and oh-my-zsh.

Rails developers generally recommend the latter. The installation method is as follows:

Cd ~ /. # Enter the root directory # automatically install the script wget -- no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh-o-| sh

Modify. zshrc and import the custom content in. bash_profile and. profile. For example, to support rvm, add the following content in. zshrc:

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

And customize the. zhsrc file, such as replacing theme. The complete theme solution list is here

export ZSH_THEME="robbyrussell"  
Integrate common rails3 development plug-ins

Install common plug-ins such as git, rails, Ruby, and rvm for rails development. Note that the Plugins directory of Oh-my-zsh contains rails and rails3. If you want to support rails3, yes:

plugins=(rails3 rails git textmate ruby rvm gem git github brew bundler textmate pow)   

For the usage of these plug-ins, refer to the Wiki of Oh-my-zsh.

To check whether plugins have been installed or installed, here:

cd ~/.oh-my-zsh/plugins

After modification, reload:

source ~/.zshrc
More references

Oh-my-zsh
Grmlzshrc
Zsh-terminal for different MAC experiences!

Zsh and oh-my-zsh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.