【问题标题】:Switching to zsh shell -- `ls` ends terminal tab切换到 zsh shell -- `ls` 结束终端选项卡
【发布时间】:2015-03-27 23:26:30
【问题描述】:

我正在从 Fish Shell 切换到 Zsh。

我刚刚经历了一个漫长的过程,将我的一堆Fish函数转换成Zsh,大多数东西都可以工作,但我只是注意到当我输入ls时,它不仅不起作用,而且实际上结束了终端标签会话:

➜  ~  cd code
➜  code  ls

[Process completed]

超级令人困惑,因为我没有(我不认为)做任何事情来弄乱那个命令。大多数其他基本命令似乎都可以正常工作。任何想法可能导致这种混乱?我已将我的新 Zsh 函数和我的 .zshrc 文件放在下面。

这些函数,主要是一堆 Git/Zeus(一个 Rails 工具)函数,让我的生活更轻松:

~/.oh-my-zsh/custom/plugins/functions/functions.plugin.zsh

##############################
######### ZEUS BASED #########
##############################

z () zeus start

rmz () rm .zeus.sock

rr () {
  if [ "$#" -gt 0 ]; then
    r routes | grep "$@" 
  else
    r routes
  fi
}

zeus_on () {
  if ps aux | grep -v grep | grep 'zeus slave: development_environment'; then
    true
  else
    false
  fi
}

mg () r db:migrate "$@"

tprep () {
  if zeus_on; then
    zeus rake db:test:prepare "$@"
  else
    echo "Zeus is not running"
    rake db:test:prepare "$@"
  fi
}

s () { 
  if zeus_on; then
    zeus s
  else
    echo "Zeus is not running"
    rails s
  fi
}

t () {
  if zeus_on; then
    if [ '$#' -gt 0 ]; then
      zeus test "$@"
    else
      zeus test spec
    fi
  else
    echo "Zeus is not running"
    if [ "$#" -gt 0 ]; then
      rspec "$@"
    else
      rspec spec
    fi
  fi
}

tt () zeus rspec --tag $1 spec

r () {
  if zeus_on; then
    zeus rake "$@"
  else
    echo "Zeus is not running"
    rake "$@"
  fi
}

c () {
  if zeus_on; then
    zeus c "$@"
  else
    echo "Zeus is not running"
    rails c "$@"
  fi
}

jt () {
  if zeus_on; then
    if [ "$#" -gt 0 ]; then
      zeus tr spec:javascript SPEC="$@"
    else
      zeus tr spec:javascript
    fi
  else
    echo "Zeus is not running"
    if [ "$#" -gt 0 ]; then
      rake spec:javascript RAILS_ENV=test SPEC="$@"
    else
      rake spec:javascript RAILS_ENV=test
    fi
  fi
}

# ##############################
# ############ GIT #############
# ##############################

gcurrent () git rev-parse --abbrev-ref HEAD

gclean! () git clean -f -d

gd () git diff "$@"

gds () git diff --staged "$@"

gdh () git diff HEAD^

gr () git rebase "$@"

gri () gr -i "$@"

grc () gr --continue

gback () git reset HEAD^

gh () hub browse

gac () {
  ga
  gc "$@"
}

gacm () gac -m "$@"

ga () {
  if [ "$#" -gt 0 ]; then
    git add "$@"
  else
    git add .
  fi
}

gp () git pull "$@"

gs () git status "$@"

gsp () git stash pop

gss () git stash save

gl () git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit "$@"

gco () git checkout "$@"

gcom () git checkout master

gpush () git push "$@"

gb () git branch "$@"

gbd () git branch -d "$@"

gc () git commit "$@"

gca () gc --amend

grb () git rebase "$@"

g () git "$@"

gcpick () git cherry-pick "$@"

grh () git reset --hard "$@"

gbdelete () git push origin --delete "$@"


# ###############################
# ######## CD SHORTCUTS #########
# ###############################

fish_dir () cd ~/.config/fish/

# Define code_directory in .zshrc
code () cd /$code_directory/"$@"

f () code "$@"

# ##############################
# ########### OTHER ############
# ##############################


tasks () ps aux | grep $@

b () bundle $@

ll () ls -lh $@

fs () foreman start $@

hcon () heroku run rails console

dtest () tail -f diagnostic.txt

sb () {
  if [ "$#" -gt 0 ]; then
    sublime "$@"
  else
    sublime .
  fi
}

fish_edit () {
  sb ~/.config/fish/config.fish
}

可能不相关,但也让我感到困惑。当我打开与该文件中 grep 的使用相关的新 shell 选项卡时,我收到此警告。这在 Fish 之前没有发生过:

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
  [-e pattern] [-f file] [--binary-files=value] [--color=when]
  [--context[=num]] [--directories=action] [--label] [--line-buffered]
  [--null] [pattern] [file ...]

最后是导入这些插件的.zshrc 文件。 This answer(尽管行为不同)表明这可能是 PATH 定义问题,但我认为我在 PATH 中包含了所有必要的路径:

# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh

export code_directory=$HOME/code/

ZSH_THEME="robbyrussell"

plugins=(functions github)

# User configuration

export PATH="/Users/sasha/.rvm/gems/ruby-2.2.0/bin:/Users/sasha/.rvm/gems/ruby-2.2.0@global/bin:/Users/sasha/.rvm/rubies/ruby-2.2.0/bin:/Users/sasha/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin"
# export MANPATH="/usr/local/man:$MANPATH"

source $ZSH/oh-my-zsh.sh

回答以下问题:

➜  .oh-my-zsh git:(master) ✗ which ls
ls: aliased to ls -G
➜  .oh-my-zsh git:(master) ✗ typeset -f ls
ls () {
    ls -G -lh $@
}
➜  .oh-my-zsh git:(master) ✗ type ls
ls is an alias for ls -G

更新

我已经弄清楚 是什么 导致它,但不知道 为什么。就是这一行:

ll () ls -lh $@

在我的函数文件中。当我输入ll 时,我认为它会运行ls -lh whatever-arguments-follow。任何想法为什么在我运行ls 时会引发错误?

【问题讨论】:

  • which lstypeset -f lstype ls 说什么?
  • 上面已经回答了——看起来它在某处有别名?我会四处寻找,但我不明白为什么ls -G 会破坏终端选项卡。
  • 这可能是由ls 的递归(无限)定义引起的吗?如果将 ls 别名为 /bin/ls -G 会发生什么?
  • 我在函数文件中添加了alias ls=/bin/ls -G,打开了一个新选项卡并尝试了它,不幸的是,它再次杀死了该选项卡。
  • 尝试在子shell中输入ls,即运行zsh,然后在zsh提示符下输入ls。如果zsh 在死机时打印错误消息,那应该让您在选项卡关闭之前看到它(并带您返回父 shell)。

标签: bash shell command-line zsh fish


【解决方案1】:

好的,所以ls 既是别名又是函数。那是行不通的,因为它们会递归。在我的终端上,当我运行时, ls 是一个别名和一个函数

ls

它思考了两秒钟,然后我明白了

ls:1: maximum nested function level reached

使用函数或别名,但不能同时使用。

编辑哦,看起来可以,但问题是ls 函数是递归的。它应该是

ls () {
    command ls -G -lh $@
}

command 内置确保您执行实际命令,既不是别名也不是函数。

【讨论】:

  • 我看不到我在哪里将其设为别名或函数。我已经定义了一个ll 函数。这是隐式别名ls 还是什么?
  • 其他人这样做了。也许您的 zsh 配置为源 /etc/profile/etc/zsh/etc/zshrc 或 ...您是否将 ENV 变量设置为?
  • 超级怪。我根本没有ls 函数。只是ll,应该直接调用ls。但是当我添加command 时,它a)都有效,b)用相同的功能替换了llls(基本上是我创建的ll)。再次。我没有在任何地方重新定义ls,但看起来ll 以某种方式递归地重新定义它!?! (我唯一的代码在上面的问题中)。任何想法为什么会这样?
  • @Sasha 如果您想详细了解 zsh 作为登录 shell 启动时的作用,请运行 zsh -l -x。这最终应该会提供别名和函数来自何处的线索。
  • 请注意,type ll 可以看到正在使用的 ll(例如,某事物的别名或 shell 函数),whence -f ll 显示其定义。这可能会给你一些关于它在哪里定义的线索。
猜你喜欢
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2011-02-21
  • 2023-03-05
  • 1970-01-01
  • 2013-08-04
相关资源
最近更新 更多