macos OS X 终端颜色

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

OS X Terminal Colors

macosshellterminalcolors

提问by sa125

I'm new to OS X, having just gotten a Mac after working with Ubuntu Linux for some time. Among the many things I'm trying to figure out is the absence of colors in my terminal window - like the ones that are shown (on Linux) when running ls -laor git status...

我是 OS X 的新手,在使用 Ubuntu Linux 一段时间后刚刚获得了一台 Mac。我试图弄清楚的许多事情之一是我的终端窗口中没有颜色 - 就像在运行ls -la或运行时(在 Linux 上)显示的颜色一样git status......

I just can't figure out how to activate colors in my shell.

我只是不知道如何在我的外壳中激活颜色。

回答by LuckyMalaka

Here is a solution I've found to enable the global terminal colors.

这是我找到的启用全局终端颜色的解决方案 。

Edit your .bash_profile(since OS X 10.8) — or (for 10.7 and earlier): .profileor .bashrcor /etc/profile(depending on availability) — in your home directory and add following code:

在您的主目录中编辑您的.bash_profile(自 OS X 10.8 起)—或(对于 10.7 及更早版本):.profile.bashrc/etc/profile(取决于可用性)并添加以下代码:

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

CLICOLOR=1simply enables coloring of your terminal.

CLICOLOR=1只需启用终端的着色。

LSCOLORS=...specifies how to color specific items.

LSCOLORS=...指定如何为特定项目着色。

After editing .bash_profile, start a Terminal and force the changes to take place by executing:

编辑后.bash_profile,启动终端并通过执行强制更改发生:

source ~/.bash_profile

source ~/.bash_profile

Then go to Terminal > Preferences, click on the Profilestab and then the Textsubtab and check Display ANSI Colors.

然后转到Terminal > Preferences,单击Profiles选项卡,然后单击Text子选项卡并检查Display ANSI Colors

Verified on Sierra (May 2017).

在 Sierra 上验证(2017 年 5 月)。

回答by 4levels

You can use the Linux based syntax in one of your startup scripts. Just tested this on an OS X Mountain Lion box.

您可以在其中一个启动脚本中使用基于 Linux 的语法。刚刚在 OS X Mountain Lion 盒子上测试了这个。

eg. in your ~/.bash_profile

例如。在你的~/.bash_profile

export TERM="xterm-color" 
export PS1='\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]$ '

This gives you a nice colored prompt. To add the colored lsoutput, you can add alias ls="ls -G".

这为您提供了一个漂亮的彩色提示。要添加彩色ls输出,您可以添加alias ls="ls -G".

To test, just run a source ~/.bash_profileto update your current terminal.

要进行测试,只需运行 asource ~/.bash_profile来更新您当前的终端。

Side note about the colors: The colors are preceded by an escape sequence \eand defined by a color value, composed of [style;color+m]and wrapped in an escaped []sequence. eg.

关于颜色的旁注:颜色前面是转义序列\e并由颜色值定义,由[style;color+m]转义[]序列组成并包裹在其中。例如。

  • red = \[\e[0;31m\]
  • bold red (style 1) = \[\e[1;31m\]
  • clear coloring = \[\e[0m\]
  • 红色 = \[\e[0;31m\]
  • 粗红色(样式 1)= \[\e[1;31m\]
  • 清晰的着色 = \[\e[0m\]

I always add a slightly modified color-scheme in the root's .bash_profile to make the username red, so I always see clearly if I'm logged in as root (handy to avoid mistakes if I have many terminal windows open).

我总是在 root 的 .bash_profile 中添加一个稍微修改的配色方案,使用户名变为红色,所以我总是能清楚地看到我是否以 root 身份登录(如果我打开了很多终端窗口,这很容易避免错误)。

In /root/.bash_profile:

/root/.bash_profile

PS1='\[\e[0;31m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '

PS1='\[\e[0;31m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '

For all my SSH accounts online I make sure to put the hostname in red, to distinguish if I'm in a local or remote terminal. Just edit the .bash_profilefile in your home dir on the server.. If there is no .bash_profilefile on the server, you can create it and it should be sourced upon login.

对于我所有的在线 SSH 帐户,我确保将主机名设为红色,以区分我是在本地终端还是远程终端中。只需.bash_profile在服务器上的主目录中编辑文件。如果服务器上没有.bash_profile文件,您可以创建它,它应该在登录时获取。

If this is not working as expected for you, please read some of the comments below since I'm not using MacOS very often..

如果这不符合您的预期,请阅读下面的一些评论,因为我不经常使用 MacOS。

If you want to do this on a remote server, check if the ~/.bash_profilefile exists. If not, simply create it and it should be automatically sourced upon your next login.

如果要在远程服务器上执行此操作,请检查该~/.bash_profile文件是否存在。如果没有,只需创建它,它就会在您下次登录时自动获取。

回答by Nick Woodhams

MartinVonMartinsgrün and 4Levels methods confirmed work great on Mac OS X Mountain Lion.

MartinVonMartinsgrün 和 4Levels 方法证实在 Mac OS X Mountain Lion 上效果很好。

The file I needed to update was ~/.profile.

我需要更新的文件是 ~/.profile。

However, I couldn't leave this question without recommending my favorite application, iTerm 2.

但是,我不能不推荐我最喜欢的应用程序iTerm 2就离开这个问题。

iTerm 2 lets you load global color schemes from a file. Really easy to experiment and try a bunch of color schemes.

iTerm 2 允许您从文件加载全局配色方案。真的很容易试验和尝试一堆配色方案。

Here's a screenshot of the iTerm 2 window and the color preferences. iTerm2 Color Preferences Screenshot Mac

这是 iTerm 2 窗口和颜色首选项的屏幕截图。 iTerm2 Color Preferences Screenshot Mac

Once I added the following to my ~/.profile file iTerm 2 was able to override the colors.

一旦我将以下内容添加到我的 ~/.profile 文件中,iTerm 2 就能够覆盖颜色。

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export PS1='\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '

Here is a great repository with some nice presets:

这是一个很棒的存储库,其中包含一些不错的预设:

iTerm2 Color Schemes on Github by mbadolato

Github 上的 iTerm2 配色方案 by mbadolato

Bonus: Choose "Show/hide iTerm2 with a system-wide hotkey" and bind the key with BetterTouchToolfor an instant hide/show the terminal with a mouse gesture.

奖励:选择“使用系统范围的热键显示/隐藏 iTerm2”并将该键与BetterTouchTool绑定, 以便使用鼠标手势即时隐藏/显示终端。

回答by Marcio

If you want to have your ls colorized you have to edit your ~/.bash_profile file and add the following line (if not already written) :

如果你想让你的 ls 着色,你必须编辑你的 ~/.bash_profile 文件并添加以下行(如果尚未编写):

source .bashrc

Then you edit or create ~/.bashrc file and write an alias to the ls command :

然后编辑或创建 ~/.bashrc 文件并将别名写入 ls 命令:

alias ls="ls -G"

Now you have to type source .bashrcin a terminal if already launched, or simply open a new terminal.

现在source .bashrc,如果已经启动,您必须输入终端,或者只需打开一个新终端。

If you want more options in your lsjuste read the manual ( man ls). Options are not exactly the same as in a GNU/Linux system.

如果您想要更多选项,请ls阅读手册 ( man ls)。选项与 GNU/Linux 系统中的选项并不完全相同。

回答by smaccoun

If you are using tcsh, then edit your ~/.cshrcfile to include the lines:

如果您使用 tcsh,请编辑您的~/.cshrc文件以包含以下行:

setenv CLICOLOR 1
setenv LSCOLORS dxfxcxdxbxegedabagacad

Where, like Martin says, LSCOLORS specifies the color scheme you want to use.

正如 Martin 所说,LSCOLORS 指定了您要使用的配色方案。

To generate the LSCOLORS you want to use, checkout this site

要生成您要使用的 LSCOLORS,请查看此站点

回答by William Rose

Check what $TERM gives: mine is xterm-color and ls -alG then does colorised output.

检查 $TERM 给出的内容:我的是 xterm-color 并且 ls -alG 然后进行彩色输出。

回答by ccook

When I worked on Mac OS X in the lab I was able to get the terminal colors from using Terminal (rather than X11) and then editing the profile (from the Mac menu bar). The interface is a bit odd on the colors, but you have to set the modified theme as default.

当我在实验室中使用 Mac OS X 时,我能够通过使用终端(而不是 X11)获取终端颜色,然后编辑配置文件(从 Mac 菜单栏)。界面在颜色上有点奇怪,但您必须将修改后的主题设置为默认值。

Further settings worked by editing .bashrc.

进一步的设置通过编辑.bashrc.