- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以从当前打开的选项卡中的命令行在 Mac OS X 终端中打开新选项卡?
我知道在终端中打开新选项卡的键盘快捷键是“CMD+t”,但我正在寻找在命令行中执行的基于脚本的解决方案。
最佳答案
更新:这个答案基于下面发布的 shell 函数而受到欢迎,该函数在 OSX 10.10 中仍然有效(-g
选项除外)。< br/>不过,功能更齐全、更强大、经过测试的脚本版本现已在 npm registry 上提供。作为 CLI ttab
,它还支持 iTerm2 :
如果您有Node.js安装完毕,只需运行:
npm install -g ttab
(根据您安装 Node.js 的方式,您可能需要在前面添加 sudo
)。
否则,请关注these instructions .
安装后,运行 ttab -h
获取简洁的使用信息,或运行 man ttab
查看手册。
基于已接受的答案,下面是一个 bash 便利函数,用于在当前终端窗口中打开一个新选项卡并可选择执行命令(作为奖励,有一个用于创建新选项卡的变体函数) 窗口代替)。
如果指定了命令,则其第一个标记将用作新选项卡的标题。
# Get command-line help.
newtab -h
# Simpy open new tab.
newtab
# Open new tab and execute command (quoted parameters are supported).
newtab ls -l "$Home/Library/Application Support"
# Open a new tab with a given working directory and execute a command;
# Double-quote the command passed to `eval` and use backslash-escaping inside.
newtab eval "cd ~/Library/Application\ Support; ls"
# Open new tab, execute commands, close tab.
newtab eval "ls \$HOME/Library/Application\ Support; echo Press a key to exit.; read -s -n 1; exit"
# Open new tab and execute script.
newtab /path/to/someScript
# Open new tab, execute script, close tab.
newtab exec /path/to/someScript
# Open new tab and execute script, but don't activate the new tab.
newtab -G /path/to/someScript
警告:当您从脚本运行 newtab
(或 newwin
)时,脚本的初始工作文件夹将是新选项卡/窗口中的工作文件夹,即使您在调用 newtab
/newwin
之前更改脚本内的工作文件夹 - 通过使用 cd
命令作为解决方法的 eval
(参见上面的示例)。
# Opens a new tab in the current Terminal window and optionally executes a command.
# When invoked via a function named 'newwin', opens a new Terminal *window* instead.
function newtab {
# If this function was invoked directly by a function named 'newwin', we open a new *window* instead
# of a new tab in the existing window.
local funcName=$FUNCNAME
local targetType='tab'
local targetDesc='new tab in the active Terminal window'
local makeTab=1
case "${FUNCNAME[1]}" in
newwin)
makeTab=0
funcName=${FUNCNAME[1]}
targetType='window'
targetDesc='new Terminal window'
;;
esac
# Command-line help.
if [[ "$1" == '--help' || "$1" == '-h' ]]; then
cat <<EOF
Synopsis:
$funcName [-g|-G] [command [param1 ...]]
Description:
Opens a $targetDesc and optionally executes a command.
The new $targetType will run a login shell (i.e., load the user's shell profile) and inherit
the working folder from this shell (the active Terminal tab).
IMPORTANT: In scripts, \`$funcName\` *statically* inherits the working folder from the
*invoking Terminal tab* at the time of script *invocation*, even if you change the
working folder *inside* the script before invoking \`$funcName\`.
-g (back*g*round) causes Terminal not to activate, but within Terminal, the new tab/window
will become the active element.
-G causes Terminal not to activate *and* the active element within Terminal not to change;
i.e., the previously active window and tab stay active.
NOTE: With -g or -G specified, for technical reasons, Terminal will still activate *briefly* when
you create a new tab (creating a new window is not affected).
When a command is specified, its first token will become the new ${targetType}'s title.
Quoted parameters are handled properly.
To specify multiple commands, use 'eval' followed by a single, *double*-quoted string
in which the commands are separated by ';' Do NOT use backslash-escaped double quotes inside
this string; rather, use backslash-escaping as needed.
Use 'exit' as the last command to automatically close the tab when the command
terminates; precede it with 'read -s -n 1' to wait for a keystroke first.
Alternatively, pass a script name or path; prefix with 'exec' to automatically
close the $targetType when the script terminates.
Examples:
$funcName ls -l "\$Home/Library/Application Support"
$funcName eval "ls \\\$HOME/Library/Application\ Support; echo Press a key to exit.; read -s -n 1; exit"
$funcName /path/to/someScript
$funcName exec /path/to/someScript
EOF
return 0
fi
# Option-parameters loop.
inBackground=0
while (( $# )); do
case "$1" in
-g)
inBackground=1
;;
-G)
inBackground=2
;;
--) # Explicit end-of-options marker.
shift # Move to next param and proceed with data-parameter analysis below.
break
;;
-*) # An unrecognized switch.
echo "$FUNCNAME: PARAMETER ERROR: Unrecognized option: '$1'. To force interpretation as non-option, precede with '--'. Use -h or --h for help." 1>&2 && return 2
;;
*) # 1st argument reached; proceed with argument-parameter analysis below.
break
;;
esac
shift
done
# All remaining parameters, if any, make up the command to execute in the new tab/window.
local CMD_PREFIX='tell application "Terminal" to do script'
# Command for opening a new Terminal window (with a single, new tab).
local CMD_NEWWIN=$CMD_PREFIX # Curiously, simply executing 'do script' with no further arguments opens a new *window*.
# Commands for opening a new tab in the current Terminal window.
# Sadly, there is no direct way to open a new tab in an existing window, so we must activate Terminal first, then send a keyboard shortcut.
local CMD_ACTIVATE='tell application "Terminal" to activate'
local CMD_NEWTAB='tell application "System Events" to keystroke "t" using {command down}'
# For use with -g: commands for saving and restoring the previous application
local CMD_SAVE_ACTIVE_APPNAME='tell application "System Events" to set prevAppName to displayed name of first process whose frontmost is true'
local CMD_REACTIVATE_PREV_APP='activate application prevAppName'
# For use with -G: commands for saving and restoring the previous state within Terminal
local CMD_SAVE_ACTIVE_WIN='tell application "Terminal" to set prevWin to front window'
local CMD_REACTIVATE_PREV_WIN='set frontmost of prevWin to true'
local CMD_SAVE_ACTIVE_TAB='tell application "Terminal" to set prevTab to (selected tab of front window)'
local CMD_REACTIVATE_PREV_TAB='tell application "Terminal" to set selected of prevTab to true'
if (( $# )); then # Command specified; open a new tab or window, then execute command.
# Use the command's first token as the tab title.
local tabTitle=$1
case "$tabTitle" in
exec|eval) # Use following token instead, if the 1st one is 'eval' or 'exec'.
tabTitle=$(echo "$2" | awk '{ print $1 }')
;;
cd) # Use last path component of following token instead, if the 1st one is 'cd'
tabTitle=$(basename "$2")
;;
esac
local CMD_SETTITLE="tell application \"Terminal\" to set custom title of front window to \"$tabTitle\""
# The tricky part is to quote the command tokens properly when passing them to AppleScript:
# Step 1: Quote all parameters (as needed) using printf '%q' - this will perform backslash-escaping.
local quotedArgs=$(printf '%q ' "$@")
# Step 2: Escape all backslashes again (by doubling them), because AppleScript expects that.
local cmd="$CMD_PREFIX \"${quotedArgs//\\/\\\\}\""
# Open new tab or window, execute command, and assign tab title.
# '>/dev/null' suppresses AppleScript's output when it creates a new tab.
if (( makeTab )); then
if (( inBackground )); then
# !! Sadly, because we must create a new tab by sending a keystroke to Terminal, we must briefly activate it, then reactivate the previously active application.
if (( inBackground == 2 )); then # Restore the previously active tab after creating the new one.
osascript -e "$CMD_SAVE_ACTIVE_APPNAME" -e "$CMD_SAVE_ACTIVE_TAB" -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" -e "$cmd in front window" -e "$CMD_SETTITLE" -e "$CMD_REACTIVATE_PREV_APP" -e "$CMD_REACTIVATE_PREV_TAB" >/dev/null
else
osascript -e "$CMD_SAVE_ACTIVE_APPNAME" -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" -e "$cmd in front window" -e "$CMD_SETTITLE" -e "$CMD_REACTIVATE_PREV_APP" >/dev/null
fi
else
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" -e "$cmd in front window" -e "$CMD_SETTITLE" >/dev/null
fi
else # make *window*
# Note: $CMD_NEWWIN is not needed, as $cmd implicitly creates a new window.
if (( inBackground )); then
# !! Sadly, because we must create a new tab by sending a keystroke to Terminal, we must briefly activate it, then reactivate the previously active application.
if (( inBackground == 2 )); then # Restore the previously active window after creating the new one.
osascript -e "$CMD_SAVE_ACTIVE_WIN" -e "$cmd" -e "$CMD_SETTITLE" -e "$CMD_REACTIVATE_PREV_WIN" >/dev/null
else
osascript -e "$cmd" -e "$CMD_SETTITLE" >/dev/null
fi
else
# Note: Even though we do not strictly need to activate Terminal first, we do it, as assigning the custom title to the 'front window' would otherwise sometimes target the wrong window.
osascript -e "$CMD_ACTIVATE" -e "$cmd" -e "$CMD_SETTITLE" >/dev/null
fi
fi
else # No command specified; simply open a new tab or window.
if (( makeTab )); then
if (( inBackground )); then
# !! Sadly, because we must create a new tab by sending a keystroke to Terminal, we must briefly activate it, then reactivate the previously active application.
if (( inBackground == 2 )); then # Restore the previously active tab after creating the new one.
osascript -e "$CMD_SAVE_ACTIVE_APPNAME" -e "$CMD_SAVE_ACTIVE_TAB" -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" -e "$CMD_REACTIVATE_PREV_APP" -e "$CMD_REACTIVATE_PREV_TAB" >/dev/null
else
osascript -e "$CMD_SAVE_ACTIVE_APPNAME" -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" -e "$CMD_REACTIVATE_PREV_APP" >/dev/null
fi
else
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB" >/dev/null
fi
else # make *window*
if (( inBackground )); then
# !! Sadly, because we must create a new tab by sending a keystroke to Terminal, we must briefly activate it, then reactivate the previously active application.
if (( inBackground == 2 )); then # Restore the previously active window after creating the new one.
osascript -e "$CMD_SAVE_ACTIVE_WIN" -e "$CMD_NEWWIN" -e "$CMD_REACTIVATE_PREV_WIN" >/dev/null
else
osascript -e "$CMD_NEWWIN" >/dev/null
fi
else
# Note: Even though we do not strictly need to activate Terminal first, we do it so as to better visualize what is happening (the new window will appear stacked on top of an existing one).
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWWIN" >/dev/null
fi
fi
fi
}
# Opens a new Terminal window and optionally executes a command.
function newwin {
newtab "$@" # Simply pass through to 'newtab', which will examine the call stack to see how it was invoked.
}
关于macos - 从命令行打开新的终端选项卡 (Mac OS X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171725/
在 Android 的 API > 19 中是否有任何方法可以获取可移动 SD 卡的路径? 与外部 SD 卡一样,我们有 Environment.getExternalStorageDirectory
一些 Android 设备有 microSD(或其他存储卡)插槽,通常安装为 /storage/sdcard1 据我所知,自 Android 4.4 起 Google 限制了对此内存的访问,并在 An
我使用 Java Card 2.1.2 SDK 和 GPShell 作为与设备通信的方式在 Java Card 上构建一个项目。我从 GpShell 测试了 helloworld 示例,并成功发送了
我开发了一个应用程序,它有一个来电接收器,它适用于所有手机。一位用户有一部双 SIM 卡安卓手机。该应用程序适用于第一张 SIM 卡。但是当有人调用他的第二张 SIM 卡时,我们的应用程序不会被调用。
我有一个带预览的文件输入。 这是笔 Codepen 我想强制高度,我无法理解我该怎么做。我想将此组件的高度固定为 300px(示例),我还需要保持加载图像的正确纵横比,用灰色背景填充空白。现在我保持宽
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我正在使用此代码访问 SD card : import os from os.path import join from jnius import autoclass #from android.pe
我正在为数据记录设备编写固件。它以 20 Hz 的频率从传感器读取数据并将数据写入 SD 卡。但是,向SD卡写入数据的时间并不一致(大约200-300 ms)。因此,一种解决方案是以一致的速率将数据写
我正在使用以下代码将视频放到网站上,但是在垂直方向上,手机屏幕上只能看到视频的左半部分 我不是网络开发人员。有人可以告诉我确切的内容吗,如何使其正确放置在手机屏幕上? 是在youtube iframe
我正在使用 Vuetify 1.5 和 Vuetify 网格系统来设置我的布局。现在我有一个组件 HelloWorld我将其导入到我的 Parent 中成分。我已经在我的 HelloWorld 中设置
我使用 python 制作了一个简单的二十一点游戏。我制作了游戏的其余部分,但我正在努力放入 ASCII 卡,所以这只是代码的一小部分。我尝试将 * len(phand) 放在附加行的末尾。虽然这确实
我正在使用玩家卡设置 Twitter 卡。它可以在预览工具中运行,但文档说它需要在“twitter.com 现代桌面浏览器? native iOs 和 Android Twitter 应用程序?mob
任何旧的 GSM 兼容 SIM 卡(3G USIM 的奖励)。 我想我需要一些硬件?谁能为业余爱好者推荐一些便宜的东西,以及一些更专业的东西? 我认为会有一个带有硬件的 API 的完整文档,所以也许这
我使用 python 制作了一个简单的二十一点游戏。我制作了游戏的其余部分,但我正在努力放入 ASCII 卡,所以这只是代码的一小部分。我尝试将 * len(phand) 放在附加行的末尾。虽然这确实
我记得前一段时间读到有 cpu 卡供系统添加额外的处理能力来进行大规模并行化。任何人都有这方面的经验和任何资源来研究项目的硬件和软件方面吗?这项技术是否不如传统集群?它更注重功率吗? 最佳答案 有两个
我检查外部存储是否已安装并且可用于读/写,然后从中读取。我使用的是确切的官方 Android 示例代码 ( from here )。 它说外部存储未安装。 getExternalFilesDir(nu
在 Android 2.1 及更低版本中,Android 应用程序可以请求下载到 SD 卡上吗?另外我想知道应用程序是否可以请求一些包含视频的文件夹下载到 SD 卡上?以及如何做到这一点? 提前致谢。
我们编写了一个 Windows 设备驱动程序来访问我们的自定义 PCI 卡。驱动程序使用 CreateFile 获取卡的句柄。 我们最近在一次安装中遇到了问题,卡似乎停止工作了。我们尝试更换卡(更换似
有些新设备(例如 Samsung Galaxy)带有两个 SD 卡。我想知道是否有任何方法可以确定设备是否有两张 SD 卡或一张 SD 卡。谢谢 最佳答案 我认为唯一的方法是使用 检查可用根的列表 F
我正在尝试将文件读/写到 SD 卡。我已经尝试在我的真实手机和 Eclipse 中的模拟器上执行此操作。在这两种设备上,对/mnt/sdcard/或/sdcard 的权限仅为“d--------”,我
我是一名优秀的程序员,十分优秀!