ZSH:输入行为

时间:2015-05-11 13:33:59

标签: linux shell unix zsh

我意识到,当我在我的终端时,我希望在空输入时按Enter以便在我使用git repos时生成lsgit status

我怎样才能实现这一目标?我的意思是,在zsh中的Empty input -> Enter上有自定义行为吗?

编辑:谢谢你的帮助。这是我对preexec ...

的看法
precmd() {
  echo $0;
  if ["${0}" -eq ""]; then
    if [ -d .git ]; then
      git status
    else
      ls
    fi;
  else
    $1
  fi;
}

2 个答案:

答案 0 :(得分:1)

在我的JerseyClient client = new JerseyClientBuilder().build(); JerseyWebTarget target = client.target("http://192.168.122.156:8089/tomcatembedded/rest/auth/login"); Response response = target.request() .header("Content-Type", "application/json;charset=UTF-8") .accept(MediaType.APPLICATION_JSON) .post(Entity.entity(credentials, MediaType.APPLICATION_JSON)); String output = response.readEntity(String.class); System.out.println(output); 中,我使用了在这里找到的precmd和preexec的组合:

http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions

我还发现git-prompt非常有用:

https://github.com/olivierverdier/zsh-git-prompt

答案 1 :(得分:0)

On 输入 zsh 调用accept-line小部件,这会导致缓冲区作为命令执行。

您可以编写自己的小部件以实现所需的行为并重新绑定 Enter

my-accept-line () {
    # check if the buffer does not contain any words
    if [ ${#${(z)BUFFER}} -eq 0 ]; then
        # put newline so that the output does not start next
        # to the prompt
        echo
        # check if inside git repository
        if git rev-parse --git-dir > /dev/null 2>&1 ; then
            # if so, execute `git status'
            git status
        else
            # else run `ls'
            ls
        fi
    fi
    # in any case run the `accept-line' widget
    zle accept-line
}
# create a widget from `my-accept-line' with the same name
zle -N my-accept-line
# rebind Enter, usually this is `^M'
bindkey '^M' my-accept-line

虽然仅在实际存在命令的情况下运行zle accept-line就足够了,但 zsh 在输出后不会发出新提示。虽然可以使用zle redisplay重绘提示,但如果您使用多行提示,则可能会覆盖输出的最后一行。 (当然也有解决方法,但没有像使用zle accept-line那么简单。

警告:这会重新定义shell中最重要的部分。虽然这本身没有任何问题(否则我不会在这里发布),如果my-accept-line没有完美运行,它就有可能使你的shell无法使用。例如,如果缺少zle accept-line,则无法使用 Enter 来确认任何命令(例如,重新定义my-accept-line或启动编辑器)。所以,请先将它测试,然后再将其放入~/.zshrc

此外,默认情况下accept-line也绑定到 Ctrl + J 。我建议保留这种方式,以便轻松运行默认的accept-line