AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

    • 主页
    • 系统&网络
    • Ubuntu
    • Unix
    • DBA
    • Computer
    • Coding
    • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1577
Accepted
Hamish Downer
Hamish Downer
Asked: 2010-08-08 04:40:43 +0800 CST2010-08-08 04:40:43 +0800 CST 2010-08-08 04:40:43 +0800 CST

从 bash 迁移到 zsh [关闭]

  • 772
关闭。这个问题需要更加集中。它目前不接受答案。

想改进这个问题?更新问题,使其仅通过编辑此帖子专注于一个问题。

5年前关闭。

改进这个问题

我正在考虑从 bash 迁移到 zsh,因为我经常看到赞美 zsh 的帖子。我是一位经验丰富的命令行用户,我假设基础知识几乎相同,所以我正在寻找建议以获得移动的好处,以及任何需要注意的问题。

请为每个答案提供一点建议。我正在寻找一口大小的块,在那里我可以回来并以稳定的速度将额外的信息集成到我的 shell 使用中,而不是试图一口气学习所有内容。

command-line bash zsh
  • 10 10 个回答
  • 60883 Views

10 个回答

  • Voted
  1. Best Answer
    loevborg
    2010-08-09T09:50:29+08:002010-08-09T09:50:29+08:00

    正如您所说,zsh在许多方面与bash. 它具有一些您在 中找不到的功能bash,并且可以通过强大的方式进行扩展。不要将移动视为一种革命,而应将其视为有助于您日常工作的一系列进化步骤。以下是我的一些提示.zshrc。尽管您说您更喜欢单条建议,但这篇文章的列表很长。仍然是一个好主意。只需将有趣的部分添加到您的~/.zshrc并重新加载source ~/.zshrc. 最后一个提示:学习zsh默认(“Emacs”)键盘快捷键的击键:^A ^E ^W Alt-F Alt-B Alt-P ^L ^R. 您可以用Alt两个单独的击键替换:Alt-P相当于ESC P.


    这为您提供了更广泛的选项卡完成。

    autoload -U compinit
    compinit
    

    两端制表符补全。

    setopt completeinword
    

    制表符完成应该不区分大小写。

    zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
    

    killall 更好的完成。

    zstyle ':completion:*:killall:*' command 'ps -u $USER -o cmd'
    

    更改“单词”的定义,例如使用 ^W。

    autoload select-word-style
    select-word-style shell
    

    ls的颜色。

    if [[ -x "`whence -p dircolors`" ]]; then
      eval `dircolors`
      alias ls='ls -F --color=auto'
    else
      alias ls='ls -F'
    fi
    

    ls 的快捷方式。

    alias ll='ls -l'
    alias la='ls -a'
    

    所有开壳的历史记录;存储 10,000 个条目。这使它成为有用的记忆辅助工具,可帮助您查找上次使用的命令./configure等。随意使用 Alt-P(查找以这样开头的命令)和 ^R(在历史中搜索)。

    HISTFILE=~/.zhistory
    HISTSIZE=SAVEHIST=10000
    setopt sharehistory
    setopt extendedhistory
    

    启用各种扩展通配符,例如 ls **/*.txt(查找所有文本文件),ls -d *(D)(显示所有文件,包括以“.”开头的文件)。要了解更多信息,请转到man zshexpn“文件名生成”部分。

    # superglobs
    setopt extendedglob
    unsetopt caseglob
    

    这对于记住历史中的命令而不执行它们很有用。

    setopt interactivecomments # pound sign in interactive prompt
    

    键入“..”而不是“cd ..”、“/usr/include”而不是“cd /usr/include”。

    setopt auto_cd
    

    很好的提示。

    PS1='[%T] %n@%m:%~# '
    

    显示耗时超过 10 秒的命令的 CPU 使用情况统计信息

    REPORTTIME=10
    

    您在 Ubuntu 中广泛使用的一些命令。

    alias 'a=sudo aptitude'
    alias 'ai=sudo aptitude install'
    alias 'ar=sudo aptitude remove'
    alias 'au=sudo aptitude update'
    alias 'ag=sudo aptitude safe-upgrade'
    alias 'as=apt-cache search'
    alias 'aw=apt-cache show'
    

    列出按大小排序的包 - 在决定哪些包占用您的磁盘空间时很有用。

    function apt-list-packages {
      dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n' | grep -v deinstall | sort -n | awk '{print $1" "$2}'
    }
    
    • 98
  2. qbi
    2010-08-08T12:36:16+08:002010-08-08T12:36:16+08:00

    我会推荐这本书From bash to Z Shell。它提供了切换 shell 所需的所有建议。它解释了两种 shell 的不同之处,并使新的 zsher 变得容易。

    • 14
  3. LassePoulsen
    2010-08-08T07:42:50+08:002010-08-08T07:42:50+08:00

    这是我的.zshrc,这是最重要的!zsh 有很多选项可供您使用,因此请查看网络上的所有示例或阅读Zsh 主页上的文档。

    除了命令行右侧的时间戳之外,我的 .zshrc 不包含任何非常酷的东西。

    顺便说一句,请记住在此处的几个示例中尝试制表符补全:

    mplayer -a[tab]
    

    将显示如下内容:

    mplayer -a
     -ac                 -- force usage of a specific audio codec
     -af                 -- activate audio filters
     -afm                -- force usage of a specific audio codec family
     -alang              -- select the DVD audio language
     -ao                 -- specify audio driver
     -aop                -- specify audio output filter
    

    如果您使用无密码 ssh-keys 或 ssh-agent,您可能会发现 tabcomplete 远程文件很有用:

    scp apollo:/home/user/[tab]
    Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/
    

    获得列表后,您可以多次按 Tab 键以循环显示不同的可能性。

    但是要注意,这个shell会让你变得懒惰,让你觉得标准的shell是愚蠢和烦人的!

    • 8
  4. poolie
    2010-10-15T17:21:03+08:002010-10-15T17:21:03+08:00

    几个特别有用的扩展 glob:

    1. rmdir *(/^F)- 删除当前目录下所有非空目录

    2. grep traceback /srv/log/**/*(.m-2)- 在过去两天修改的文件中查找此正则表达式

    3. chmod g+w **/*(U^I)- 使我拥有且不可组写的任何文件成为组可写

    是的,当然你可以用它来写,find但这更容易冲掉。公平地说,它确实有两个缺点,都与它们都被扩展到命令行有关:如果它匹配数千个文件,命令行将变得太长并且这将失败,其次所有文件都被找到在文件开始运行之前。

    (setopt extendedglob如果尚未启用,则需要)

    • 5
  5. Pedro
    2010-09-03T11:54:17+08:002010-09-03T11:54:17+08:00

    我对 bash 了解不多,所以我无法与之匹敌。我的 zsh 配置文件中的一些片段。

    一些配置

    HISTFILE=~/.zsh_history
    HISTSIZE=1000
    SAVEHIST=1000
    REPORTTIME=10 # print elapsed time when more than 10 seconds
    setopt NO_HUP
    setopt NO_LIST_BEEP
    setopt LOCAL_OPTIONS # allow functions to have local options
    setopt LOCAL_TRAPS # allow functions to have local traps
    setopt HIST_VERIFY
    setopt SHARE_HISTORY # share history between sessions ???
    setopt EXTENDED_HISTORY # add timestamps to history
    setopt PROMPT_SUBST
    setopt CORRECT
    setopt COMPLETE_IN_WORD
    setopt IGNORE_EOF
    
    setopt APPEND_HISTORY # adds history
    setopt INC_APPEND_HISTORY SHARE_HISTORY  # adds history incrementally and share it across sessions
    setopt HIST_IGNORE_ALL_DUPS  # don't record dupes in history
    setopt HIST_REDUCE_BLANKS
    # Leave some chars out of the out of WORDCHARS so ^W acts more nicely 
    WORDCHARS='*?_-[]~\!#$%^(){}<>|`@#$%^*()+:?'
    

    提示中的 Git

    if [[ -n $SSH_CONNECTION ]]; then
      export PS1='%m:%3~$(git_info_for_prompt)%# '
    else
      export PS1='%3~$(git_info_for_prompt)%# '
    fi
    

    一些热键,在行首插入一些文本。

    insert_sudo     () { zle beginning-of-line; zle -U "sudo "         }
    insert_apt      () { zle beginning-of-line; zle -U "sudo apt-get " }
    insert_gem      () { zle beginning-of-line; zle -U "sudo gem "     }
    insert_install  () { zle -U "install "     }
    
    zle -N insert-sudo      insert_sudo
    zle -N insert-apt       insert_apt
    zle -N insert-gem       insert_gem
    zle -N insert-install   insert_install
    
    bindkey "^B" insert-gem
    bindkey "^N" insert-install
    bindkey "^k" insert-sudo
    bindkey "^a" insert-apt
    

    函数,然后我存储在 ~/.zsh/functions

    git_info_for_prompt

    local g="$(git rev-parse --git-dir 2>/dev/null)"
    if [ -n "$g" ]; then
      local r
      local b
      if [ -d "$g/../.dotest" ]
      then
        if test -f "$g/../.dotest/rebasing"
        then
          r="|REBASE"
        elif test -f "$g/../.dotest/applying"
        then
          r="|AM"
        else
          r="|AM/REBASE"
        fi
        b="$(git symbolic-ref HEAD 2>/dev/null)"
      elif [ -f "$g/.dotest-merge/interactive" ]
      then
        r="|REBASE-i"
        b="$(cat "$g/.dotest-merge/head-name")"
      elif [ -d "$g/.dotest-merge" ]
      then
        r="|REBASE-m"
        b="$(cat "$g/.dotest-merge/head-name")"
      elif [ -f "$g/MERGE_HEAD" ]
      then
        r="|MERGING"
        b="$(git symbolic-ref HEAD 2>/dev/null)"
      else
        if [ -f "$g/BISECT_LOG" ]
        then
          r="|BISECTING"
        fi
        if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
        then
          if ! b="tag: $(git describe --exact-match HEAD 2>/dev/null)"
          then
            b="$(cut -c1-7 "$g/HEAD")..."
          fi
        fi
      fi
    
      if [ -n "$1" ]; then
        printf "$1" "${b##refs/heads/}$r"
      else
        printf "[%s]" "${b##refs/heads/}$r"
      fi
    fi
    

    一些 github 选项

    #compdef github
    
    _github() {
      if (( CURRENT > 2 )); then
        # shift words so _arguments doesn't have to be concerned with second command
        (( CURRENT-- ))
        shift words
        # use _call_function here in case it doesn't exist
        _call_function 1 _github_${words[1]}
      else
        _values "github command" \
         "fetch[Fetch from a remote to a local branch.]" \
         "ignore[Ignore a SHA (from 'github network commits')]" \
         "fetch_all[Fetch all refs from a user]" \
         "info[Info about this project.]" \
         "browse[Open this repo in a web browser.]" \
         "home[Open this repo's master branch in a web browser.]" \
         "clone[Clone a repo.]" \
         "pull-request[Generate the text for a pull request.]" \
         "network[Project network tools.]" \
         "pull[Pull from a remote.]" \
         "track[Track another user's repository.]"
      fi
    }
    
    _github_pull() {
      _arguments \
        "--merge[Automatically merge remote's changes into your master.]"
    }
    _github_clone() {
      _arguments \
        "--ssh[Clone using the git@github.com style url.]"
    }
    
    _github_track() {
      _arguments \
        "--private[Use git@github.com: instead of git://github.com/.]" \
        "--ssh[Equivalent to --private.]"
    }
    
    _github_network() {
      if (( CURRENT > 2 )); then
        # shift words so _arguments doesn't have to be concerned with second command
        (( CURRENT-- ))
        shift words
        # use _call_function here in case it doesn't exist
        _call_function 1 _github_network_${words[1]}
      else
        _values "github network command" \
         "web[Open network in a web browser.]" \
         "list[List networked repositories.]" \
         "fetch[Fetched commits for a given networked repository.]" \
         "commits[List networked commits not pulled into this repo.]"
      fi
    }
    
    _github_network_commits() {
      _arguments \
        "--project[Filter commits on a certain project.]" \
        "--author[Filter commits on a email address of author.]" \
        "--common[Show common branch point.]" \
        "--nocache[Do not use the cached network data.]" \
        "--sort[How to sort : date(*), branch, author.]" \
        "--thisbranch[Look at branches that match the current one]" \
        "--applies[Filter commits to patches that apply cleanly.]" \
        "--limit[Only look through the first X heads - useful for really large projects]" \
        "--before[Only show commits before a certain date.]" \
        "--after[Only show commits after a certain date.]" \
        "--shas[Only show shas.]" \
        "--cache[Use the network data even if it's expired.]" \
        "--noapply[Filter commits to patches that do not apply cleanly.]"
    }
    
    • 4
  6. tutuca
    2010-08-08T06:41:27+08:002010-08-08T06:41:27+08:00

    我在同一次旅行:)

    到目前为止,我发现问题是要有一个好的配置文件(.zshrc)。

    以这个为例http://matt.blissett.me.uk/linux/zsh/zshrc,看看评论并破解你的方式。Stackoverflow 和 severphault 以及搜索的好地方。

    我还没有深入研究http://dotfiles.org/.zshrc,但我没有那么多时间放松 :)

    • 3
  7. Phil P
    2010-09-03T11:21:28+08:002010-09-03T11:21:28+08:00

    了解 zsh 中的扩展 glob 和递归 glob。

    了解一些关于 zstyle 以及各种事物(尤其是补全)如何让您使用 zstyle 调整它们的配置。

    查看关联数组。还有标准数组(注意与 bash 的区别,更好!)

    如果您使用正则表达式,请查看=~(bash 也有)并考虑:setopt rematch_pcre

    避免编写依赖于 zsh 魔法的脚本,因为虽然它使用起来很棒,但 zsh 可能倾向于只写。如果你使用的太多了,考虑一下何时切换到 Python 之类的语言。

    Zsh 很诱人。这是黑暗的一面。欢迎。

    • 3
  8. hariharan022
    2010-11-15T00:39:31+08:002010-11-15T00:39:31+08:00

    很大的好处 - 出色的选项卡完成,带有许多命令的预打包完成脚本。这是一个显示输出的示例apt-get<TAB>:

    apt-get
    action
    autoclean        build-dep        clean            dselect-upgrade  install          remove           update           
    autoremove       check            dist-upgrade     help             purge            source           upgrade          
    
    • 2
  9. Rick
    2010-09-03T17:56:08+08:002010-09-03T17:56:08+08:00

    我已经进行了一些演讲,并将几个人转换为 zsh。我在 github 中保留了我的(有什么优势)笔记的 github 存储库以及我自己的 zsh 配置的启动器和副本。

    http://github.com/mitechie/zshrc

    • 1
  10. qbi
    2010-08-08T12:38:34+08:002010-08-08T12:38:34+08:00

    另一个很棒的资源是zsh 爱好者页面(来自grml zsh 站点)。

    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助
subwaysurfers
my femboy roommate

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve