【问题标题】:Opening a new terminal tab in OSX(Snow Leopard) with the opening terminal windows directory path在 OSX(Snow Leopard) 中打开一个新的终端选项卡,打开终端窗口目录路径
【发布时间】:2010-12-08 00:41:29
【问题描述】:

我在谷歌上搜索了一段时间,寻找一种简单的方法来做到这一点,但我找不到。

我设置了一个自定义终端环境 (zsh),其中包含各种别名和功能,以使事情变得更容易。我一直遇到的一件事是,我将快速 APPLE-t 创建一个新选项卡,然后键入一个相对于我刚才所在的终端窗口路径的命令。这总是失败,因为新选项卡的路径是 ~/而不是我刚刚使用的任何东西! 关于将新终端选项卡的目录路径设置为打开选项卡的目录路径的脚本有什么想法吗?

非常感谢任何帮助。

伊恩

【问题讨论】:

  • 解决方案:切换到konsole.kde.org,它确实支持将 CWD 保留到新的选项卡/窗口中。 :)
  • 我想知道是否有一些 AppleScript 解决方案。

标签: bash macos terminal zsh


【解决方案1】:

我有几个我使用的脚本:

dup(带有工作目录的新窗口):

#!/bin/sh
pwd=`pwd`
osascript -e "tell application \"Terminal\" to do script \"cd $pwd; clear\"" > /dev/null

tup(具有相同工作目录的新标签页):

#!/bin/sh

pwd=`pwd`
osascript -e "tell application \"Terminal\"" \
    -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
    -e "do script \"cd $pwd; clear\" in front window" \
    -e "end tell"
    > /dev/null

【讨论】:

  • 这些太棒了,这个答案真的没有足够的+1。谢谢 Gavin,简直太棒了。
  • 不错!我现在正在使用 iTerm,但我在终端中尝试了这些,它们很有效。
  • 太棒了,我稍微修改了它以进行论证。我现在用它来引导我的网络项目环境:gist.github.com/2492064
【解决方案2】:

另一个没有脚本的解决方案是iTerm2,它内置了这个功能。它还有更多值得一试的功能。

【讨论】:

  • iTerm2 真是太棒了。我不知道没有它我会做什么。全屏模式和分屏也是必备功能。当 iTerm2 让这(以及许多其他重要的事情)变得如此简单时,我无法相信人们愿意跳过的 AppleScript 箍!
  • 如何使用 iTerm2 实现这一目标?
  • @mycargus 试试这个:iTerm2 -> Preferences -> Profiles -> General -> Reuse previous session's home directory
【解决方案3】:

好的,我再次回答我自己的问题的方式也是如此(无论如何至少接近回答了)

我发现了一个相对于上述脚本不太冗长的脚本(由Dan Benjamin 提供)似乎可以解决问题,尽管两个脚本在成功完成之前都会输出类似的错误。我已经通过在脚本末尾添加 clear 来解决这个问题,所以这不是什么大问题。

我说我几乎解决了我自己的问题,因为我的目标是找到一种方法来解决这个问题在各种网络浏览器中使用无数小时。我可以使用诸如 Dan 的脚本来管理的最好的方法是 t-return,这不是最大的区别,但足够大,以至于每次发出所述命令时我都会有点恼火。我知道,我应该放手......但我不能,这可能是我一开始就陷入这个烂摊子的原因,无休止地摆弄电脑。我跑题了,这是我正在使用的脚本:

#!/bin/sh

# Make a new OS X Terminal tab with the current working directory.

if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
else
    PATHDIR=$1
fi

/usr/bin/osascript <<EOF
activate application "Terminal"
tell application "System Events"
    keystroke "t" using {command down}
end tell
tell application "Terminal"
    repeat with win in windows
        try
            if get frontmost of win is true then
                do script "cd $PATHDIR; clear" in (selected tab of win)
            end if
        end try
    end repeat
end tell
EOF
clear

为了完整起见,如果省略了尾随清除,则会在请求窗口中吐出错误:

2009-10-20 01:30:38.714 osascript[20862:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
    /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
tab 2 of window id 13942

【讨论】:

  • 这对我来说在 Snow Leopard (10.6.8) 上失败:第 25 行:1371 分段错误 /usr/bin/osascript
  • @JohnKeyes & i0n,如果你想要一个在 Snow Leopard 上工作的不那么冗长的解决方案,你应该给 Gavin's 回答看看。
【解决方案4】:

您可以通过修改位于http://www.entropy.ch/blog/Mac+OS+X/2008/06/27/Terminal-Tricks-“term”-revisited-with-tabs 的 BASH 脚本来获得所需的内容。这是脚本,取自 Marc Linyage 的网站www.entropy.ch/blog

#!/bin/sh
#
# Open a new Mac OS X terminal window or tab in the current or another
# directory and optionally run a command in the new window or tab.
#
# - Without any arguments, the new terminal window opens in
#   the current directory, i.e. the executed command is "cd $PWD".
# - If the first argument is a directory, the new terminal will "cd" into
#   that directory before executing the remaining arguments as command.
# - The optional "-t" flag executes the command in a new tab 
#   instead of a new window.
# - The optional "-x" flag closes the new window or tab
#   after the executed command finishes.
# - The optional "-p" flag takes an argument of the form x,y (e.g. 40,50) and
#   positions the terminal window to the indicated location on the screen
# - The optional "-s" flag takes an argument of the form w,h (e.g. 800,400) and
#   resizes the terminal window to the indicated width and height in pixels.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 2.1
#

set -e

while getopts xtp:s: OPTION; do
    [ $OPTION = "x" ] && { EXIT='; exit'; }
    [ $OPTION = "t" ] && { TAB=1; }
    [ $OPTION = "p" ] && { POSITION="set position of window 1 to {$OPTARG}"; }
    [ $OPTION = "s" ] && { SIZE="set size of window 1 to {$OPTARG}"; }
done

for (( $OPTIND; $OPTIND-1; OPTIND=$OPTIND-1 )); do shift; done

if [[ -d "$1" ]]; then WD=$(cd "$1"; pwd); shift; else WD=$PWD; fi


COMMAND="cd '$WD' && echo -n \$'\\\\ec';"
for i in "$@"; do
COMMAND="$COMMAND '$i'"
done

if [ $TAB ]; then

osascript 2>/dev/null <<EOF
tell application "System Events"
    tell process "Terminal" to keystroke "t" using command down
end
tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT" in window 1
    $POSITION
    $SIZE
end tell
EOF

else

osascript <<EOF
tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
    $POSITION
    $SIZE
end tell
EOF

fi

【讨论】:

  • 嗯。我想知道那个愚蠢的for (( ... )) 循环是干什么用的; shift $OPTIND 会好很多。此外,为了安全起见,COMMAND=... 绝对应该更像COMMAND="cd $(printf '%q' "$WD") ..."。嗯,无论如何,它似乎相当聪明。
【解决方案5】:

在我的回答 here 中,我提供了一个函数和一个别名:

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }
export cd

alias cdp='cd $(cat /tmp/CWD)'

您应该能够在 ~/.bashrc~/.zshrc 的末尾添加一个(可能有条件的)语句来执行该别名。

【讨论】:

  • 虽然不是很完美。假设您打开了两个选项卡,您的最后一个 cd 已在选项卡 1 中完成,但您当前正在选项卡 2 中工作。现在您生成一个新选项卡 - 它转到选项卡 1 的 CWD,而不是选项卡的 CWD 2.
  • 这会破坏我终端中的 cd 命令。不知道为什么,但我想我最好为了其他人的利益提及它。移除后一切恢复正常。不过感谢您的努力!
  • @ephemient:$PROMPT_COMMAND 变量可以设置为'echo "$PWD" &gt; /tmp/CWD',因此每次发出提示时都会写入。 @i0n:“休息”到底是什么意思。错误信息?具体行为?
  • 是的,本来可以更具描述性,不是吗! cd 命令完全停止执行任何操作。没有任何类型的错误消息。 cd .. cd DIR_NAME cd ~ 与 cd 相关的一切都停止工作。我所有终端相关文件的完整和最新副本都在 GitHub 上:github.com/i0n/dotfiles
  • 您可以尝试添加export cd
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2019-02-17
  • 2021-10-05
  • 2013-10-28
  • 1970-01-01
  • 2018-06-28
相关资源
最近更新 更多