你可能不需要Oh My Zsh。
Oh My Zsh adds bloat

原始链接: https://rushter.com/blog/zsh-shell/

## 告别臃肿:更快的 Zsh 设置 Oh My Zsh (OMZ) 经常被推荐,但其丰富的特性也带来了代价:由于每次新建标签页都要解释大量的 shell 脚本,导致 shell 启动时间变慢。对于每天打开数百个终端的用户来说,这会累积起来。 作者提倡一个精简的 Zsh 配置,从历史设置 (`HISTSIZE`, `SAVEHIST`)、带时间戳的扩展历史记录、`autocd` 用于目录更改以及 `compinit` 用于补全等基本功能开始,从而实现 0.07 秒的启动时间。 他们建议使用 **Starship** 来获得快速、可定制的提示符,而不是 OMZ 插件,并禁用不必要的云服务显示。对于历史记录搜索,**fzf**(绑定到 Ctrl+R)提供了比 zsh-autosuggestions 更优秀的交互式模糊搜索。 最后,Vim 用户可以启用 Vi 模式以加快命令编辑速度。关键在于构建一个根据*你的*需求量身定制的 Zsh 环境,避免不必要的臃肿并优先考虑速度。

相关文章

原文

Oh My Zsh is still getting recommended a lot. The main problem with Oh My Zsh is that it adds a lot of unnecessary bloat that affects shell startup time.

Since OMZ is written in shell scripts, every time you open a new terminal tab, it has to interpret all those scripts. Most likely, you don't need OMZ at all.

Here are the timings from the default setup with a few plugins:

  ~ /usr/bin/time -f "%e seconds" zsh -i -c exit
0.38 seconds

And that's only for prompt and a new shell instance! Creating a new tab takes some time for your terminal too. It feels like a whole second to me.

My workflows involve opening and closing up to hundreds of terminal or tmux tabs a day. I do everything from the terminal. Just imagine that opening a new tab in a text editor would take half a second every time.

Once in a while, it also checks for updates, which can take up to a few seconds when you open a new tab.

I see no reason in frequent updates for my shell configuration. Especially, when a lot of third-party plugins are getting updates too. Why would you want you shell to fetch updates?

My advice is to start simple and only add what you really need.

Minimal Zsh configuration

Here is the minimal Zsh configuration that works well as a starting point:

export HISTSIZE=1000000000
export SAVEHIST=$HISTSIZE
setopt EXTENDED_HISTORY
setopt autocd
autoload -U compinit; compinit

It's an already pretty good setup with completions!

Some details about this configuration:

  • HISTSIZE and SAVEHIST set the size of your history.
  • EXTENDED_HISTORY adds timestamps to your history entries.
  • autocd allows you to change directories without typing cd.
  • compinit initializes the Zsh completion system.

Prompt customization

You also want to customize your prompt. For prompts, I'm using starship which is a fast and minimal prompt packed into a single binary.

The very old way of doing this in Oh My Zsh was to use plugins and custom themes. With starship, it's very simple and easy now. It replaces git, virtual environment and language specific plugins.

Here is my config for starship:

[aws]
disabled = true

[package]
disabled = true

[gcloud]
disabled = true

[azure]
disabled = true


[nodejs]
disabled = true

[character]
success_symbol = '[➜](bold green)'

[cmd_duration]
min_time = 500
format = 'underwent [$duration](bold yellow)'

[directory]
truncation_length = 255
truncate_to_repo = false
use_logical_path = false

Because cloud services are available globally, I've disabled them. I don't want them to be displayed on every prompt, since this adds visual noise.

Here is how my prompt looks like now:

This project uses both Python and Rust, they are highlighted in the prompt. When you run a command, it also shows how long it took to execute.

To enable it, add the following line to your .zshrc:

eval "$(starship init zsh)"

History search

A lot of people use zsh-autosuggestions plugin for history search. I find it distracting, because it shows all suggestions as you type.

Instead, I prefer using fzf binded to Ctrl+R for searching history. It gives an interactive fuzzy search.

To enable it, add the following lines to your .zshrc:

Final startup time

After these changes, the startup should look as follows:

 /usr/bin/time -f "%e seconds" zsh -i -c exit
0.07 seconds

Miscellaneous tips

For Vim users, I also suggest enabling Vim mode in Zsh. It makes editing commands much faster.

set -o vi
# Fix for backspace in vi mode
bindkey -v '^?' backward-delete-char

It works the same way as in Vim. By default, zle (the library that reads the shell input) uses Emacs keybindings.

Conclusion

After switching from OMZ a year ago, it only took me a few days to get used to the new workflow. If you still missing some of the plugins, you can always load them manually.

联系我们 contact @ memedata.com