macos iTerm 2 配置文件

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

iTerm 2 profiles

macositerm

提问by Miles McCrocklin

I have recently switched over to iTerm2 and love it. I am wondering though if there is a way to use profiles to correspond to what environment/specific machine you are on.

我最近切换到 iTerm2 并喜欢它。我想知道是否有办法使用配置文件来对应您所在的环境/特定机器。

Say if I am doing tasks in one window on my mac the profile is displayed as default, but if I ssh into a machine (lets say dev0), the profile on iTerm will update to profile dev0. Once I've finished with dev0 and call exit, the profile will switch to default again.

假设我在 Mac 上的一个窗口中执行任务,配置文件显示为默认值,但如果我通过 ssh 连接到一台机器(假设为 dev0),iTerm 上的配置文件将更新为配置文件 dev0。完成 dev0 并调用 exit 后,配置文件将再次切换为默认值。

I realize one work around is to open up a specific profile whenever I want to ssh into another machine and have a way to distinguish, but if the connection is closed it requires you to notice based off text rather than say the background of the window.

我意识到一种解决方法是每当我想通过 ssh 连接到另一台机器并有办法区分时打开一个特定的配置文件,但是如果连接关闭,它需要您根据文本进行注意,而不是说窗口的背景。

Is this possible? If not how can this feature be added, and is there a way I can contribute?

这可能吗?如果不是,如何添加此功能,我可以做出贡献吗?

回答by George

iTerm2 supports a custom escape code that changes the profile on the fly. Put it in your .bashrc or .bash_profile.

iTerm2 支持自定义转义码,可即时更改配置文件。把它放在你的 .bashrc 或 .bash_profile 中。

<esc>]50;SetProfile=X^G

where X is the profile. For instance, to change the profile to one called "Foo", us this shell script:

其中 X 是配置文件。例如,要将配置文件更改为名为“Foo”的配置文件,请使用以下 shell 脚本:

#!/bin/bash
echo -e "3]50;SetProfile=Foo\a"

To change it back when you log out, put code to change the profile back to default in ~/.bash_logout.

要在注销时将其改回,请在 ~/.bash_logout 中放置代码以将配置文件改回默认值。

回答by lfender6445

for zsh users

对于 zsh 用户

lets say you have 2 profiles, one named mac(for your primary machine) and one for linux(your remote machine)

假设您有 2 个配置文件,一个名为mac(用于您的主机),另一个用于linux(您的远程机器)

when entering the session, we need to tell zsh to load our profile

进入会话时,我们需要告诉 zsh 加载我们的配置文件

  1. connect to remote linux
  2. in ~/.zshrcadd echo -e "\033]50;SetProfile=linux\a"
  3. source your files for immediate effect: source ~/.zshrc
  4. your new theme should be visible within the iterm session.
  1. 连接到远程linux
  2. ~/.zshrcecho -e "\033]50;SetProfile=linux\a"
  3. 获取您的文件以立即生效: source ~/.zshrc
  4. 您的新主题应该在 iterm 会话中可见。

when exiting the session, we need to tell zsh to switch back to our original profile

退出会话时,我们需要告诉 zsh 切换回我们的原始配置文件

  1. connect to remote linux
  2. in linux ~/.zlogoutadd the following
  1. 连接到远程linux
  2. 在linux中~/.zlogout添加以下内容
if [ "$SHLVL" = 1 ]; then
  echo -e "3]50;SetProfile=mac\a"
  clear
fi

now you can swap profiles with ease <3.

现在您可以轻松交换配置文件 <3。

if you are using bash, i believe the steps are similar but you would instead modify ~/.bashrcand ~/.bash_logout

如果您使用的是 bash,我相信步骤是相似的,但您会改为修改~/.bashrc~/.bash_logout

demo

演示

demo of session based profiles in iterm2

iterm2 中基于会话的配置文件的演示

回答by Yoha? Berreby

The latest iTerm2 nightly (Build 2.9.20150329-nightly at the time of writing) allows you to do that easily. You can download it here.

最新的 iTerm2 nightly(在撰写本文时为 Build 2.9.20150329-nightly)让您可以轻松做到这一点。你可以在这里下载。

Once you've installed and opened it:

安装并打开后:

  1. Log in to your remote machine via ssh and click iTerm2(the app menu) > Install Shell Integration. It will download a script with curland install it. Do the same on your local machine.
  2. Go to Preferences> Profiles.
  3. Create a new profile for your local machine. Customize it to fit your needs (change background color, name, etc)
  4. Go to the Advancedtab and scroll to the bottom.
  5. In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOSTon the target machine. It is not always the one you see in your prompt.
  6. Create another profile, this time for your remote machine, and customize it.
  7. Add the hostname of the remote machine in Automatic Profile Switching.
  8. Now, if you ssh into the remote machine, your profile will change, and if you exitout of the ssh session, you will be back to your local profile.
  1. 通过 ssh 登录到您的远程计算机,然后单击iTerm2(应用程序菜单)> Install Shell Integration。它将下载一个脚本curl并安装它。在本地机器上做同样的事情
  2. 转到首选项>配置文件
  3. 为您的本地机器创建一个新的配置文件。自定义它以满足您的需求(更改背景颜色、名称等)
  4. 转到“高级”选项卡并滚动到底部。
  5. Automatic Profile Switching 中,单击“+”并添加本地计算机的主机名。主机名是您echo $HOST在目标机器上运行时获得的主机名。它并不总是您在提示中看到的那个。
  6. 为您的远程机器创建另一个配置文件,并对其进行自定义。
  7. Automatic Profile Switching 中添加远程机器的主机名。
  8. 现在,如果您通过 ssh 进入远程机器,您的配置文件将更改,如果您exit退出 ssh 会话,您将返回到您的本地配置文件。

You can combine this solution with @esod's answerseamlessly.
Note: it didn't work for me until I created a profile specifically for the desktop instead of using the default profile.

您可以将此解决方案与@esod 的答案无缝结合。
注意:在我专门为桌面创建配置文件而不是使用默认配置文件之前,它对我不起作用。

See the documentationfor more info.

有关更多信息,请参阅文档

回答by Mark Simon

step 1:

步骤1:

custom your iterm profile, e.g. dark, light

自定义您的 iterm 配置文件,例如darklight

step 2:

第2步:

add code before to your shell profile, e.g .bashrc or .zshrc

在您的外壳配置文件之前添加代码,例如 .bashrc or .zshrc

# Change iterm2 profile. Usage it2prof ProfileName (case sensitive)
it2prof() { echo -e "3]50;SetProfile=\a" }

step 3:

第 3 步:

make sense your profile

使您的个人资料有意义

exec $SHELL -l

step 4:

步骤4:

toggle your iterm theme profile

切换您的 iterm 主题配置文件

it2prof dark
it2prof light

回答by CK.Nguyen

For Fishelluser: 1. Create a fishfunction:

对于Fishell用户: 1. 创建一个Fish函数:

functions it2prof > ~/.config/fish/functions/it2prof.fish

functions it2prof > ~/.config/fish/functions/it2prof.fish

  1. Add this content to ~/.config/fish/functions/it2prof.fishfile and save:
  1. 将此内容添加到~/.config/fish/functions/it2prof.fish文件并保存:
function it2prof
  echo -e "3]50;SetProfile=$argv\a"
end
  1. enjoy your command:
  1. 享受你的命令:

it2prof whatever_profile_you_define

it2prof whatever_profile_you_define

回答by karl li

In my case, I need to combine Yoha? Berreby's answerwith my hosts' setting to implement this feature.

就我而言,我需要结合Yoha?Berreby 对我的主机设置的回答以实现此功能。

  1. In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOST on the target machine. It is not always the one you see in your prompt.
  1. 在自动配置文件切换中,单击“+”并添加本地计算机的主机名。主机名是您在目标机器上运行 echo $HOST 时获得的主机名。它并不总是您在提示中看到的那个。

My staging server doesn't set $HOST and host name is :

我的登台服务器没有设置 $HOST 并且主机名是:

[devel@alveo-staging ~]$ hostname
alveo-staging

But I can't set the rule with hostname as alveo-staging. Because in fact alveo-stagingis just an alias of the real hostname (alveo-staging.xxx), which can be set in /etc/hosts.

但我无法将主机名设置为alveo-staging. 因为实际上alveo-staging只是真实主机名(alveo-staging.xxx)的别名,可以在/etc/hosts.

So the quick solution to this is to use * wildcards, to set the rule as *staging*.

因此,对此的快速解决方案是使用 * 通配符,将规则设置为*staging*.

Then it works.

然后它起作用了。

回答by esod

I had this same wish and found this can be accomplished in iTerm 2 (Build 1.0.0.20130319) in the application's preferences.

我有同样的愿望,发现这可以在应用程序首选项中的 iTerm 2 (Build 1.0.0.20130319) 中完成。

You can assign a profile (say a remote profile) with a different preset than your default preset by going to:

您可以通过以下方式分配具有与默认预设不同的预设的配置文件(例如远程配置文件):

Profiles -> Open Profiles select the profile and click Edit Profiles... Go to the Colors Tab and choose a preset for this profile from the list in Load Presets...

配置文件 -> 打开配置文件 选择配置文件并单击编辑配置文件... 转到颜色选项卡并从加载预设...的列表中为该配置文件选择一个预设。

Further, I've set up Keys shortcut for different profiles so I can have one iTerm window look different than another window. I did this by:

此外,我为不同的配置文件设置了 Keys 快捷方式,因此我可以让一个 iTerm 窗口看起来与另一个窗口不同。我这样做了:

  1. Creating a new a Profile in Preferences
  2. Creating a new Profile Shortcut Key in Preferences-> Keys whose action is New Window with Profile
  1. 在首选项中创建一个新的配置文件
  2. 在 Preferences-> Keys 中创建一个新的 Profile Shortcut Key,其动作是 New Window with Profile

My Default profile has a black background but sometimes it helps me to have a white background. I duplicate my Default profile and name the new profile DefaultLight. On my Default profile I go to the Keys tab where I create a new Profile Shortcut Key whose Keyboard Shortcut is ^+cmd+n, whose action is New Window with Profile, and whose Profile is DefaultLight.

我的默认配置文件有黑色背景,但有时它有助于我拥有白色背景。我复制我的默认配置文件并将新配置文件命名为 DefaultLight。在我的默认配置文件中,我转到“键”选项卡,在其中创建一个新的配置文件快捷键,其键盘快捷键是 ^+cmd+n,其操作是带有配置文件的新窗口,其配置文件是 DefaultLight。

After saving the prefrences, cmd+n opens a new window with a black background and ^+cmd+n opens a new window with a white background.

保存首选项后,cmd+n 打开一个黑色背景的新窗口,^+cmd+n 打开一个白色背景的新窗口。

There's also a New Tab with Profile action in the Keyboard Shortcut Keys Preference if you're interested in taking this even further.

如果您有兴趣更进一步,在键盘快捷键首选项中还有一个带有配置文件操作的新标签。