gnome-terminal:在新选项卡中跟踪目录


37

我在Arch Linux上,当我打开新的终端标签时,它总是转到$HOME。如何做到这一点,以便在打开新选项卡时可以在我以前所在的目录中打开外壳程序?

Answers:


49

有一个与此问题有关的错误

您需要做的就是将以下行添加到您的.bashrc或中.zshrc

. /etc/profile.d/vte.sh

至少在Arch上,脚本会检查您是否正在运行bash或zsh,否则将退出。


1
应该注意的是export PROMPT_COMMAND=...,如果您的中已经存在这样的东西,除非在之后添加它,否则可能没有效果.bashrc
swalog 2014年

2
/etc/profile.d/vte.sh覆盖PROMPT_COMMAND变量。要解决此问题,您可以修改vte.sh,并将零件更改PROMPT_COMMAND="__vte_prompt_command"PROMPT_COMMAND="${PROMPT_COMMAND};__vte_prompt_command"
swalog

2
我一直小心地将其放置在正确的位置,检查脚本是否存在,等等,但效果不理想。我正在使用Gnome Terminal 3.18.1运行4.2.3-1-ARCH。有什么建议么?
杰克·塞尼察

当将zsh添加到我的时,这对我来说不起作用.zshrc。我正在使用oh-my-zsh,不确定是否相关。
Andreas Mueller

1
我正在使用oh-my-zsh。到目前为止,我最多只能进行几周的更新。查看我的dotfiles回购进行比较:github.com/korylprince/dotfiles
korylprince

7

超级用户最好也可以交叉发布这个骇人的解决方案:

[此]在执行每个命令后(将IMO设置得不太麻烦)将当前文件夹保存在文件中,并在已保存的当前文件夹中打开一个新的终端。

将以下内容添加到.zshrc [或.bashrc ]

# emulate bash PROMPT_COMMAND (only for zsh)
precmd() { eval "$PROMPT_COMMAND" }
# open new terminal in same dir
PROMPT_COMMAND='pwd > "${HOME}/.cwd"'
[[ -f "${HOME}/.cwd" ]] && cd "$(< ${HOME}/.cwd)"

请注意,这还会在打开新窗口时将您放在最后使用的目录中。


对我来说很棒。Solus Linux,gnome-terminal和zshell。
psparrow

太适合我了,我使用默认的Elementary OS 0.4.1 Loki终端,谢谢。
拉斐尔·贝罗

0

@swalog在他的评论中启发了我,删除了所有不必要的部分,vte.sh同时不修改提示或终端标题。请注意,我不使用zsh,因此删除了zsh相关代码。

# Copyright © 2006 Shaun McCance <shaunm@gnome.org>
# Copyright © 2013 Peter De Wachter <pdewacht@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 28 Sep 2019: Tukusej’s Sirs modified this by stripping down all unnecessary parts for his usage
# (src: https://unix.stackexchange.com/questions/93476/gnome-terminal-keep-track-of-directory-in-new-tab#comment219157_93477)

# Not an interactive shell?
[[ $- == *i* ]] || return 0

# Not running under vte?
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0

__vte_urlencode() (
    # This is important to make sure string manipulation is handled
    # byte-by-byte.
    LC_ALL=C
    str="$1"
    while [ -n "$str" ]; do
        safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
        printf "%s" "$safe"
        str="${str#"$safe"}"
        if [ -n "$str" ]; then
            printf "%%%02X" "'$str"
            str="${str#?}"
        fi
    done
)

__vte_prompt_command() {
    local command=$(HISTTIMEFORMAT= history 1 | sed 's/^ *[0-9]\+ *//')
    command="${command//;/ }"
    local pwd='~'
    printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "$(__vte_urlencode "${PWD}")"
}

case "$TERM" in
    xterm*|vte*)
        [ -n "$BASH_VERSION" ] && PROMPT_COMMAND="${PROMPT_COMMAND};__vte_prompt_command"
        ;;
esac
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.