macos Mac OS X / 使用指定窗口打开终端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1150380/
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-10-21 06:16:56  来源:igfitidea点击:

Mac OS X / Open terminal with specified windows

macosterminal

提问by xpepermint

Is it possible to open a terminal window with 3 tabs. Each tab should have different path.

是否可以打开带有 3 个选项卡的终端窗口。每个选项卡应该有不同的路径。

Example:

例子:

Tab1: /etc
Tab2: /bin
Tab3: /www/ tail -f file.txt

回答by Hyman M.

This is absolutely possible, but it will take some work on your part. The first thing you need is to set up each window/tab you want in your Settings:

这绝对有可能,但您需要做一些工作。您需要做的第一件事是在“设置”中设置您想要的每个窗口/选项卡:

Settings

设置

I have 4 tabs that I open automagically every time I open Terminal. DB Shell, Editor, Server, and Shell. These are all within the Sasquatch(don't ask) project, thus the naming. Each of these should then have a unique command associated with them:

我有 4 个选项卡,每次打开终端时都会自动打开。 DB ShellEditorServer、 和Shell。这些都在Sasquatch(不要问)项目中,因此命名。这些中的每一个都应该有一个与之关联的唯一命令:

startup cmd

启动命令

In this case, I'm executing vim. If you happen to have a specific directory you'd like to start off in, you can use something like vim ~/projects/main/. Really whatever you want to go in there is the command the shell will execute when it opens. Now you need to open all your windows/tabs:

在这种情况下,我正在执行vim. 如果您碰巧有一个想要开始的特定目录,则可以使用类似vim ~/projects/main/. 实际上,无论您想输入什么,都是 shell 在打开时将执行的命令。现在您需要打开所有窗口/标签:

  1. Close everything.
  2. Open a new window for each of your profiles.
    • Go to the Shellmenu => New Tab/New Window=> Select the profile you created above.
    • Repeat for each window or tab you want.
  3. Once you have all of your windows and/or tabs open, save them as a Window Group.
    • Go to the Windowmenu => Save Window As Group....
    • Give your Window Group a name (this is helpful later).
    • If you want this group to open every time you open Terminal, check the box at the bottom of this screen, and then hit save.
  4. Close out of all of your Windows/Tabs (time to test it!).
  5. Open your new Window Group.
    • Go to the Windowmenu => Open Window Group=> Select the group you just made.
  1. 关闭一切。
  2. 为您的每个配置文件打开一个新窗口。
    • 转到Shell菜单 => New Tab/ New Window=> 选择您在上面创建的配置文件。
    • 对您想要的每个窗口或选项卡重复此操作。
  3. 打开所有窗口和/或选项卡后,将它们保存为窗口组。
    • 转到Window菜单 => Save Window As Group...
    • 为您的窗口组命名(这在以后会有所帮助)。
    • 如果您希望每次打开终端时都打开此组,请选中此屏幕底部的框,然后点击保存。
  4. 关闭所有 Windows/选项卡(测试它的时间!)。
  5. 打开您的新窗口组。
    • 转到Window菜单 => Open Window Group=> 选择您刚刚创建的组。

This should pop up all the windows you just had, in the same position. Each of the commands you set up in Settingsshould be launched in their respective tabs.

这应该会在同一位置弹出您刚刚拥有的所有窗口。您设置的每个命令Settings都应在各自的选项卡中启动。

回答by Chris Page

As of Mac OS X Lion 10.7, Terminal makes this much easier to do, without creating new profiles for each command.

从 Mac OS X Lion 10.7 开始,终端使这件事变得更容易,无需为每个命令创建新的配置文件。

  1. By default, Terminal will remember and restore the current working directory for each terminal in a Window Group. (If the working directory has been communicated to Terminal using an escape sequence. The default shell, bash, will do this at every command prompt. For other shells, you'll need to adapt the code in /etc/bashrc.)

  2. If you create a terminal with Shell > New Command, Terminal will automatically run that command when a Window Group is opened. Terminal will automatically run a limited set of "safe" commands?, and when saving a Window Group there's an option to run all commands in the group.

  1. 默认情况下,终端会记住并恢复窗口组中每个终端的当前工作目录。(如果工作目录已使用转义序列与终端通信。默认 shell bash 将在每个命令提示符下执行此操作。对于其他 shell,您需要修改 /etc/bashrc 中的代码。)

  2. 如果您使用Shell > New Command创建终端,则终端将在打开窗口组时自动运行该命令。终端将自动运行一组有限的“安全”命令?,并且在保存窗口组时,有一个选项可以运行组中的所有命令。

Terminal also automatically does these for all windows when restarting Terminal with Resume enabled. So, you may not even have to create a Window Group, depending on your circumstances.

在启用 Resume 的情况下重新启动终端时,终端还会自动为所有窗口执行这些操作。因此,您甚至可能不必创建窗口组,具体取决于您的情况。

For your example case:

对于您的示例案例:

  1. Use Shell > New Commandto run "tail -f /www/file.txt".
  2. Create a new tab and "cd /etc".
  3. Create a new tab and "cd /bin".
  4. Save them with Window > Save Windows as Group. Be sure to check the "Restore all commands" checkbox.
  1. 使用Shell > New Command运行“tail -f /www/file.txt”。
  2. 创建一个新选项卡和“cd /etc”。
  3. 创建一个新选项卡和“cd /bin”。
  4. 使用Window > Save Windows as Group保存它们。请务必选中“恢复所有命令”复选框。

Each time you open that Window Group, it will recreate those windows and run the commands. If you need to run a command andspecify the starting directory, in the New Command dialog check the "Run command inside a shell" checkbox and make the command "cd ; ".

每次打开该窗口组时,它都会重新创建这些窗口并运行命令。如果您需要运行命令指定起始目录,请在“新建命令”对话框中选中“在 shell 内运行命令”复选框并创建命令“cd ;”。

Also note that you can tell Terminal to open your Window Group at startup with Terminal > Preferences > Startup > On startup, open > Window group. There's even a checkbox to set this when saving a new Window Group.

另请注意,您可以通过Terminal > Preferences > Startup > On startup, open > Window group告诉终端在启动时打开您的窗口组。在保存新的窗口组时,甚至还有一个复选框来设置它。

? The "safe" commands include anything listed in /etc/shells, plus: screen, tmux, emacs, vi/vim, nano, pico, and top. You can customize the list with "defaults write com.apple.Terminal RestorableCommands". Set it to an array of strings containing command names or full paths. Some commands have parameters that are "unsafe" to run automatically without user intervention, so by default these commands are only considered "safe" if they do not have any arguments. To make a command safe to run with arguments, add an asterisk, e.g., "top *" is in the default value for this preference.

? “安全”命令包括 /etc/shells 中列出的任何内容,以及:screen、tmux、emacs、vi/vim、nano、pico 和 top。您可以使用“defaults write com.apple.Terminal RestorableCommands”自定义列表。将其设置为包含命令名称或完整路径的字符串数组。有些命令的参数“不安全”,无需用户干预即可自动运行,因此默认情况下,这些命令只有在没有任何参数时才被视为“安全”。要使带参数的命令安全运行,请添加星号,例如,“top *”是此首选项的默认值。

回答by smathy

You can do what you wish from within Terminal.

您可以在终端内做您想做的事。

If in Terminal preferences (Settings tab) you create a new profile (or copy one with Duplicate Settings), you can then set each profile to run a command on startup (the "Shell" subgroup within the profile).

如果在“终端”首选项(“设置”选项卡)中创建了一个新配置文件(或使用重复设置复制一个配置文件),则可以将每个配置文件设置为在启动时运行命令(配置文件中的“外壳”子组)。

Then setup your tabs by using the Shell > New Tab menu to create the new tabs from each of the specific profiles that you created for the three different executables.

然后使用 Shell > New Tab 菜单设置您的选项卡,从您为三个不同的可执行文件创建的每个特定配置文件创建新选项卡。

Then do the Save Window Group to save the group of tabs (and it will save their profiles as well).

然后执行 Save Window Group 以保存选项卡组(它也会保存它们的配置文件)。

回答by John K

I suggest the use if iTerminstead of Terminal. If only because it is more configurable. You can script it, but more important to you is that you can create a bookmark folder (one for each tab) and then "open in tabs" which will give you the behavior you seek.

我建议使用 if iTerm而不是 Terminal。如果仅仅是因为它更具可配置性。您可以编写脚本,但对您来说更重要的是您可以创建一个书签文件夹(每个选项卡一个),然后“在选项卡中打开”,这将为您提供所需的行为。

回答by Jonathan Leffler

I suspect you can control commands to be executed too. One of the programs I use creates a single-tab terminal window and arranges to execute a profile-setting script before continuing to the command prompt - the same should be feasible for a multi-tab terminal. The file is a MacOS X properties XML file.

我怀疑您也可以控制要执行的命令。我使用的其中一个程序创建了一个单选项卡终端窗口,并在继续执行命令提示符之前安排执行配置文件设置脚本 - 这对于多选项卡终端应该是可行的。该文件是一个 MacOS X 属性 XML 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CommandString</key>
    <string>. /Applications/IBM/informix/demo/server/profile_settings</string>
    <key>FontAntialias</key>
    <false/>
    <key>RunCommandAsShell</key>
    <false/>
    <key>ShowShellCommandInTitle</key>
    <true/>
    <key>TerminalType</key>
    <string>xterm</string>
    <key>WindowTitle</key>
    <string>IDS Command Window</string>
    <key>name</key>
    <string>IDS Command Window</string>
    <key>type</key>
    <string>Window Settings</string>
</dict>
</plist>

You can click on it and the terminal window is launched, the profile settings are set, and then you have a command prompt to type at. Presumably, changing the 'dot' command into the 'tail' command of the question would work; it might be that the 'RunCommandAsShell' key set to '<true\>' would replace the normal shell with the command - which is perhaps more appropriate for the question.

您可以单击它并启动终端窗口,设置配置文件设置,然后您可以在命令提示符下键入。据推测,将“点”命令更改为问题的“ tail”命令会起作用;' RunCommandAsShell' 键设置为 ' <true\>' 可能会用命令替换普通 shell - 这可能更适合这个问题。

回答by Achilles

Another way of doing this is by using the Elscripto ruby gem: https://github.com/Achillefs/elscripto. It allows yuo to easily specify terminal tabs using a YAML file

另一种方法是使用 Elscripto ruby​​ gem:https: //github.com/Achillefs/elscripto。它允许您使用 YAML 文件轻松指定终端选项卡

回答by Tamas Kalman

http://www.iterm2.com/#/section/features/split_panes

http://www.iterm2.com/#/section/features/split_panes

"Divide a tab up into multiple panes, each one of which shows a different session. You can slice vertically and horizontally and create any number of panes in any imaginable arrangement."

“将一个选项卡分成多个窗格,每个窗格显示不同的会话。您可以垂直和水平切片,并以任何可以想象的排列创建任意数量的窗格。”

回答by Panayiotis Georgiou

How to Create Custom iTerm2 Window Arrangements

如何创建自定义 iTerm2 窗口排列

Create a custom keyboard shortcut to automatically spawn a set of windows and splits with processes running.

创建自定义键盘快捷键以自动生成一组窗口并在进程运行时拆分。

With a shortcut cmd+shift+w transform split your window arrangement into 3 panels

使用快捷键 cmd+shift+w 变换将您的窗口排列分成 3 个面板

https://www.youtube.com/watch?v=Rg8AT-nds1Q

https://www.youtube.com/watch?v=Rg8AT-nds1Q

回答by Gav

Open the tabs you want and set each one up as you wish, i.e. in tab 1, cd /etc, tab 2 cd /binand so on. Now go to Window > Save Windows as Group. Click the checkbox 'Use window group when Terminal starts' and hey presto!

打开您想要的选项卡并根据需要设置每个选项卡,即在选项卡 1 cd /etc、选项卡 2 中cd /bin等等。现在转到“窗口”>“将 Windows 保存为组”。单击“终端启动时使用窗口组”复选框,然后嘿嘿!