bash 使用打开的终端窗口目录路径在 OSX(Snow Leopard) 中打开一个新的终端选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1589114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:17:40  来源:igfitidea点击:

Opening a new terminal tab in OSX(Snow Leopard) with the opening terminal windows directory path

bashmacosterminalzsh

提问by i0n

I've been Googling for a while looking for a simple way to do this, and I can't find one.

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

I have a custom terminal environment set up (zsh) with various aliases and functions to make things easier. One thing I keep running into is that I will quickly APPLE-t to create a new tab and then type a command relative to the path of the terminal window I was just in. This invariably fails because the path of the new tab is ~/ instead of whatever I was just using! Any ideas for a script to set the directory path of the new terminal tabs to the directory path of the opening tab?

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

Any help most appreciated.

任何帮助最受赞赏。

Ian

伊恩

采纳答案by i0n

OK, so as is my way I am answering my own question again (well at least getting close to answering it anyway)

好的,就像我的方式一样,我再次回答我自己的问题(至少无论如何都接近回答了)

I have found a less verbose script to the one above (courtesy of Dan Benjamin) that seems to do the trick, although both scripts output a similar error before successfully completing. I have dealt with that by adding clearto the end of the script so that's no big problem.

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

I say that I have nearly solved my own problem because my objective was to find a way to accomplish this with the Apple-t key command that has been burnt into my muscle memory as the shortcut for a new tab in anything, thanks to countless hours in various web browsers. The best I can manage with a script such as Dan's is t-return which isn't the biggest difference, but big enough that I will be slightly irked every time I issue said command. I know, I should let it go..... But I can't, which is probably how I got into this mess in the first place, endlessly fiddling with computers. I digress, here is the script I am using:

我说我几乎已经解决了我自己的问题,因为我的目标是找到一种方法来实现这一点,Apple-t 键盘命令已经烙印在我的肌肉记忆中,作为任何新标签的快捷方式,这要归功于无数小时在各种网络浏览器中。我可以用 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=
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

For completeness here is the error that gets spat out on the soliciting window if the trailing clear is omitted:

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

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

回答by Gavin Brock

I have a couple of scripts I use:

我有几个我使用的脚本:

dup(New window with the working dir):

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

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

and tup(New tab with the same working dir):

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

回答by xer0x

One other solution without scripting is iTerm2, which has this feature built in. It has even more features that make it worth checking out too.

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

回答by Pinochle

You can get what you want by modifying the BASH script found at http://www.entropy.ch/blog/Mac+OS+X/2008/06/27/Terminal-Tricks-“term”-revisited-with-tabs. Here is the script, taken from Marc Linyage's site www.entropy.ch/blog.

您可以通过修改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 "" ]]; then WD=$(cd ""; 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

回答by Paused until further notice.

In my answer here, I provided a function and an alias:

在我的答案在这里,我提供的功能和别名:

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

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

You should be able to put a (possibly conditional) statement at the end of your ~/.bashrcor ~/.zshrcto execute that alias.

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