Difference between revisions of "Zsh (简体中文)"

From ArchWiki
Jump to navigation Jump to search
Line 378: Line 378:
 
改为:
 
改为:
 
  username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
 
  username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
 +
 +
==konsole中配置zsh==
 +
待写
  
 
==更多信息可见==
 
==更多信息可见==

Revision as of 06:54, 15 November 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Zsh是一款强大的虚拟终端,既是一个系统的虚拟终端,也可以作为一个脚本语言的交互解析器。它相对Bash (not by default, only if you issue "emulate sh")有如下优点:

  • 更快
  • 优化了的自动补全
  • 优化的模式识别
  • Improved array handling
  • 全面可定制

Zsh的常见问题解答有更详细的解释

安装

未免重复安装,可以先看看目前所使用的终端是什么:

$ echo $SHELL

Template:Package Official官方维护仓库中,可以和其他类似软件一样安装 .

初始化配置文件

先运行一下,看看软件是否已经正确安装:

$ zsh

将会看到 zsh-newuser-install,新手向导将可以帮你完成一些最基本的配置。若想跳过,按Template:Codeline键退出.

把Zsh变成默认终端

Template:Filename文件中已列有zsh,则可以使用 Template:Codeline 来改变默认的终端——此命令不需要以根账户使用,但使用过程中需要使用根账户密码。 如果是从官方维护的仓库中安装Zsh,则Template:Filename已列有zsh。

让目前使用的用户改变默认虚拟终端:

$ chsh -s $(which zsh)
Note: 修改完后要重新登录才能见效

重新登录之后,会在终端中看到Zsh的提示符,和Bash是不一样的。如果还有疑惑,可输入下面命令看看默认终端是那个:

$ echo $SHELL
Tip: If you are replacing Template:Package Official, you may want to move some code from Template:Filename to Template:Filename (e.g. the prompt and the aliases) and from Template:Filename to Template:Filename (e.g. the code that starts your X Window System).

配置文件介绍

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Zsh (简体中文)#)

At login, Zsh sources the following files in this order:

Template:Filename
This file is sourced by all Bourne-compatible shells upon login: it sets up an environment upon login and application-specific (/etc/profile.d/*.sh) settings.
Template:Filename
This file should contain commands to set the command search path, plus other important environment variables; it should not contain commands that produce output or assume the shell is attached to a tty.
Template:Filename
This file is generally used for automatic execution of user's scripts.
Template:Filename
这是Zsh的主配置文件。
Template:Filename
This file is generally used for automatic execution of user's scripts.

At logout it sources Template:Filename, which is used for automatic execution of user's scripts.

Note:

用于定制的配置文件 ~/.zshrc

Although Zsh is usable out of the box, it is almost certainly not set up the way you would like to use it, but due to the sheer amount of customisation available in Zsh, configuring Zsh can be a daunting and time-consuming experience.

Included below is a sample configuration file, it provides a decent set of default options as well as giving examples of many ways that Zsh can be customised. In order to use this configuration save it as a file named Template:Filename. You can then apply the changes without needing to logout and then back in by running:

$ source ~/.zshrc

简单的 .zshrc

下面是一个简单的Template:Filename配置文件,足够用于起步:

Template:File

命令提示和补全

自动补全是Zsh的王牌功能,不可不好好利用。加入下列行到配置文件Template:Filename中,开启此功能:

Template:File

The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work you will need to prevent ssh from hashing hosts names in ~/.ssh/known_hosts (Warning: be aware that this makes your computer vulnerable to "Island-hopping" attacks). In that intention, comment the following line or set the value to "no": Template:File And move your ~/.ssh/known_hosts somewhere else so that ssh creates a new one with with un-hashed hostnames (warning: previously known hosts will thus be lost).

For autocompletion with an arrow-key driven interface, add the following to: Template:File

For autocompletion of command line switches for aliases, add the following to: Template:File

快捷键绑定

Zsh does not use readline, instead it uses its own and more powerful zle. It does not read Template:Filename or Template:Filename. zle has an emacs mode and a vi mode. By default, it tries to guess whether you want emacs or vi keys from the $EDITOR environment variable. If it is empty, it will default to emacs. You can change this with bindkey -v or bindkey -e.

To get some special keys working: Template:File

Alternatively, you can convert /etc/inputrc for Zsh: Template:File

Note: To get the proper sequences for certain key combinations, start Template:Codeline or Template:Codeline without any parameters and press them; they should then be printed in the terminal. Both can be closed again via Template:Keypress+Template:Keypress.

历史类似操作查看

把下面的文字加入配置文件.zshrc中:

Template:File

这可以使开头和目前已输入的部分相同的历史操作会被显示出来。

Prompts

There is a quick and easy way to set up a colored prompt in Zsh. Make sure that prompt is set to autload in your Template:Filename. This can be done by adding these lines to:

Template:File

You can now see available prompts by running the command:

$ prompt -l

To try one of the commands that is listed, use the command prompt followed by the name of the prompt you like. For example, to use the "walters" prompt, you would enter:

$ prompt walters

Customizing your prompt

In case you are dissatisfied with the prompts mentioned above(or want to expand their usefulness), zsh offers the possibility to build your own custom prompt. Zsh supports a left- and right-sided prompt additional to the single, left-sided prompt that is common to all shells. To customize it, the following variables can be used:

Prompt variables

常用
%n 
用户名
%m 
The computer's hostname(truncated to the first period)
%M 
The computer's hostname
%l 
The current tty
 %? 
The return code of the last-run application.
 %# 
The prompt based on user privileges (Template:Codeline for root and Template:Codeline for the rest)
时间
%T 
系统时间(格式为HH:MM)
 %* 
系统时间(格式为HH:MM:SS)
%D 
系统日期(格式为YY-MM-DD)
路径
 %~ 
表示目前所在的工作路径。若在你的家目录$HOME,则会被替换为"~"。
%d 
表示目前所在的工作路径。

For the options mentioned above: You can prefix an integer to show only certain parts of your working path. If you entered Template:Codeline and found yourself in Template:Codeline it would show Template:Codeline. This can also be done with negative integers: Template:Codeline using the same directory as above would show Template:Codeline.

格式化
%U [...] %u 
给某段话下划线的开始和结束标识
%B [...] %b 
给某段话加粗的开始和结束标识
 %{ [...] %} 
让某段话不显示在屏幕上的开始和结束标识。这是有用的,比如用于颜色设置。
配色

Zsh has a different approach to setting colors on the terminal than the one depicted here. First you write in your Template:Codeline:

autoload -U colors && colors

Following commands would now produce the color escape sequence needed to set the requested color when the prompt is printed:

$fg[color] 
will set the textcolor(red,green,blue, etc)
$reset_color 
will reset the textcolor to white

It is useful to put these commands in Template:Codeline}.

示例

To have a two-sided prompt you could write:

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%#"
RPROMPT="[%{$fg[yellow]%}%?%{$reset_color%}]"

It would equal(without colors):

username@host ~ %                                                         [0]

Advanced .zshrc files

One could also find a great way to manage its Zsh configuration by using Oh-my-zsh created by Robby Russel which contains a lot of user contributed plugins and themes but most importantly a great community of more than 900 forks on github.

This is an example of a more advanced Template:Filename.

Template:File

There are many more ways that you can customise Zsh, obviously far too many to list here, see the Zsh manual for more information.

.zshrc文件实例

这是一些用户的Template:Filename配置文件,如果你认为你的配置也很有特色,欢迎加进这个列表里:


全局配置

Occasionally you might want to have some settings applied globally to all zsh users. The zsh wiki tells us that there are some global configuration files, for example Template:Filename. This however is slightly different on ArchLinux, since it has been compiled with flags specifically to target Template:Filename instead.

So, for global configuration use Template:Filename, not Template:Filename. The same goes for Template:Filename, Template:Filename and Template:Filename. Note that these files are not installed by default, so you need to create them yourself if you want to use them.

The only exception is zprofile, use Template:Filename instead.

自动开启应用

Zsh always executes Template:Filename and Template:Filename so do not bloat these files.

If the shell is a login shell, commands are read from Template:Filename and then Template:Filename. Then, if the shell is interactive, commands are read from Template:Filename and then Template:Filename. Finally, if the shell is a login shell, Template:Filename and Template:Filename are read.

卸载

若想卸载,先要改变默认虚拟终端,比如,先恢复成Bash。

按照Zsh#Making Zsh your default shell的方法改变默认虚拟终端,把zsh替换为bash即可恢复默认虚拟终端为bash。

之后,就可以安全地卸载Zsh软件包了。

Warning: 若上述步骤失败,将引发众多问题,所以请务必严格执行之。

若没按照上面所述去做,还可以用另外的方式改变默认虚拟终端,方法是以根用户身份修改 /etc/passwd 例如:

把:

username:x:1000:1000:Full Name,,,:/home/username:/bin/zsh

改为:

username:x:1000:1000:Full Name,,,:/home/username:/bin/bash

konsole中配置zsh

待写

更多信息可见

  • IRC channel: #zsh at irc.freenode.org