代码之家  ›  专栏  ›  技术社区  ›  user1164937

Override cd命令保存bash zsh history的绝对路径[关闭]

  •  0
  • user1164937  · 技术社区  · 6 年前

    有什么简单的方法吗?我在试着覆盖 cd 所以在bash历史中,它显示为 cd <absolute_path>

    1 回复  |  直到 6 年前
        1
  •  1
  •   cdarke    6 年前

    我想这就是你想要的效果:

    之前:

    /Users/fred> cd ..
    /Users> cd
    /Users/fred> history
    ...
    438  cd ..
    439  cd
    440  history
    

    之后:

    /Users/fred> source gash.sh      #   Magic happens here
    /Users/fred> cd ..
    /Users> cd
    /Users/fred> history
    ...
    501  source gash.sh
    502  cd /Users
    503  cd /Users/fred
    504  history
    

    bash ,此处定义为 伤口.sh:

    # Must be 'sourced'
    
    cd() {
        if (( $# == 0 )); then
            builtin cd                # This calls the shell version of cd
        else
            builtin cd "$@"           # This calls the shell version of cd
        fi
        msg="cd $(pwd)"
        history -s "$msg"             # This writes to the history 'file'
    }
    

    zsh history -s (哪个 没有)与 print -s fc -p fc -P ,但这似乎是一个相当大的开销来绕过这一点。