简体   繁体   English

执行MacOS终端命令时延迟小

[英]Small delay when executing MacOS terminal commands

I've noticed that there is a small but noticeable delay when I execute simple commands like ls and cd in the terminal. 我注意到在终端中执行ls和cd之类的简单命令时,会有很小但明显的延迟。 Can anyone tell me how to go about troubleshooting this problem? 谁能告诉我如何解决此问题? I really don't know where to start. 我真的不知道从哪里开始。

Summary of the discussion in the comments: 评论中的讨论摘要:

At each prompt you are executing the command __git_ps1 which creates a small delay. 在每个提示符下,您正在执行__git_ps1命令,这会产生一个小的延迟。 The time needed to execute __git_ps1 depends on your git configuration and your current working directory. 执行__git_ps1所需的时间取决于您的git配置和当前工作目录。

To get a faster prompt you could 为了获得更快的提示,您可以

  • improve the script __git_ps1 to run faster (probably not a real option). 改进脚本__git_ps1使其运行得更快(可能不是真正的选择)。
  • use a simple prompt that does not show all the information given by __git_ps1 . 使用一个简单的提示,不显示__git_ps1给出的所有信息。
  • use a timeout such that __git_ps1 is used only if it's not too slow (see following section). 使用超时,这样仅当__git_ps1不太慢时才使用(请参阅下一节)。
  • run __git_ps1 only when something changes (not explained here since you would have to identify all commands that could change the output of __git_ps1). 仅在发生某些更改时才运行__git_ps1 (此处不做解释,因为您将必须识别所有可能更改__git_ps1输出的命令)。

Use a timeout 使用超时

In bash the command timeout n cmd executes cmd for at most n seconds. bash ,命令timeout n cmd最多执行cmd n秒。 When used in your prompt 在提示中使用时

PS1="[\\u@\\h \\W\$(__git_ps1 \" (%s)\")]\\\$ "

becomes

PS1="[\\u@\\h \\W\$(timeout 0.2 __git_ps1 \" (%s)\")]\\\$ "

You can adjust the timeout to your needs. 您可以根据需要调整超时时间。

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

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