简体   繁体   English

Mac:替换“用户/<computer-name> " 在所有终端</computer-name>

[英]Mac: Replace "users/<computer-name>" in all terminals

I'm running into an issue with hiding my computer name.我遇到了隐藏我的计算机名称的问题。 I want all terminal outputs on my mac (iTerm ->zsh & IntelliJ) to stop showing my computer name and show ~/.我希望我的 Mac 上的所有终端输出(iTerm ->zsh 和 IntelliJ)停止显示我的计算机名称并显示 ~/. Can someone help me achieve this, I'm not sure what setting I'm looking to change.有人可以帮助我实现这一目标,我不确定我要更改的设置。

ie. IE。

From

users/ComputerName/app/src/main/java/com/virtualprodigy/android_compose_template用户/ComputerName/app/src/main/java/com/virtualprodigy/android_compose_template

To

~/app/src/main/java/com/virtualprodigy/android_compose_template ~/app/src/main/java/com/virtualprodigy/android_compose_template

In your terminal, you can open the file called.zshenv.在您的终端中,您可以打开名为 .zshenv 的文件。 This file is used by zsh to set environment variables. zsh 使用此文件设置环境变量。 You can add the line "export PS1='$ '" to the bottom of the file.您可以将“export PS1='$'”行添加到文件底部。 This will set the prompt to only display the $ symbol instead of your computer name.这会将提示设置为仅显示 $ 符号而不是您的计算机名称。

If you are using IntelliJ, you can click on the "Settings" icon (the gear icon) and choose "Appearance & Behavior" and then "Appearance".如果您使用的是 IntelliJ,则可以单击“设置”图标(齿轮图标)并选择“外观和行为”,然后选择“外观”。 From there, you can uncheck the box for "Show full path in window title".从那里,您可以取消选中“在 window 标题中显示完整路径”复选框。 This will stop IntelliJ from displaying your computer name in the window title.这将阻止 IntelliJ 在 window 标题中显示您的计算机名称。

Prompt format in zsh is controlled by PROMPT or PS1 variable. zsh 中的提示格式由PROMPTPS1变量控制。 You can check if one of these variables is set up in ~/.zshrc (if there's no such file, create it by running touch ~/.zshrc ).您可以检查是否在~/.zshrc中设置了这些变量之一(如果没有这样的文件,请通过运行touch ~/.zshrc创建它)。

If in your.zshrc you see a line setting either PROMPT or PS1 , in its value substitute %/ (absolute path) with %~ (relative to home path).如果在 your.zshrc 中看到一行设置PROMPTPS1 ,请在其值中将%/ (绝对路径)替换为%~ (相对于主路径)。

If there's no such line in your.zshrc, you can just add this:如果你的 .zshrc 中没有这样的行,你可以添加:

PROMPT='%~ %# '

Then save the file and restart your terminal.然后保存文件并重新启动终端。

To change the output of the pwd command and other commands that display the current working directory, you can create an alias in your shell profile.要更改 pwd 命令的 output 和其他显示当前工作目录的命令,您可以在 shell 配置文件中创建一个别名。

STEP 1: To do this, open the shell profile in a text editor:第 1 步:为此,请在文本编辑器中打开 shell 配置文件:

nano ~/.zshrc

STEP 2: Add the following line to the file, which will create an alias for the pwd command that replaces "users/ComputerName" with "~":第 2 步:将以下行添加到文件中,这将为 pwd 命令创建一个别名,将“users/ComputerName”替换为“~”:

alias pwd="pwd | sed 's/\/users\/ComputerName/~/g'"

STEP 3: You need to replace "ComputerName" with your computer's name.第 3 步:您需要将“ComputerName”替换为您的计算机名称。 Save the file and exit the editor.Then, you need to activate the changes by running保存文件并退出编辑器。然后,您需要通过运行来激活更改

source ~/.zshrc

This will create an alias for the pwd command that will replace "users/ComputerName" with "~" in the output.这将为 pwd 命令创建一个别名,它将 output 中的“users/ComputerName”替换为“~”。

You can change the prompt in your terminal to display ~ instead of the full path by modifying your shell's configuration file.您可以通过修改 shell 的配置文件将终端中的提示更改为显示 ~ 而不是完整路径。

For zsh, you can add the following line to your ~/.zshrc file:对于 zsh,您可以将以下行添加到您的 ~/.zshrc 文件中:

PROMPT='%~ %# '

For bash, you can add this line to your ~/.bash_profile or ~/.bashrc file:对于 bash,您可以将此行添加到您的 ~/.bash_profile 或 ~/.bashrc 文件中:

PS1="\w \$ "

This will change the prompt to display only the current directory, represented by ~ for the home directory, and the command prompt symbol ($ for normal users, # for superusers).这会将提示更改为仅显示当前目录,用 ~ 表示主目录,命令提示符符号 ($ 表示普通用户,# 表示超级用户)。

You will need to either restart your terminal, or run the command source ~/.zshrc or source ~/.bashrc to apply the changes.您将需要重新启动终端,或运行命令source ~/.zshrcsource ~/.bashrc以应用更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM