在Fedora上优化bash或zsh
本文将向你展示如何在 Fedora 的命令行解释器(CLI)Shell 中设置一些强大的工具。如果使用bash(默认)或zsh,Fedora 可让你轻松设置这些工具。
前置需求
这需要一些已安装的软件包。在 Fedora 工作站上,运行以下命令:
<span class="kwd">sudo</span><span class="pln"> dnf install </span><span class="kwd">git</span><span class="pln"> </span><span class="kwd">wget</span><span class="pln"> curl ruby ruby</span><span class="pun">-</span><span class="pln">devel zsh util</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">user redhat</span><span class="pun">-</span><span class="pln">rpm</span><span class="pun">-</span><span class="pln">config </span><span class="kwd">gcc</span><span class="pln"> </span><span class="kwd">gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> </span><span class="kwd">make</span>
在 Silverblue 上运行:
<span class="kwd">sudo</span><span class="pln"> rpm</span><span class="pun">-</span><span class="pln">ostree install </span><span class="kwd">git</span><span class="pln"> </span><span class="kwd">wget</span><span class="pln"> curl ruby ruby</span><span class="pun">-</span><span class="pln">devel zsh util</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">user redhat</span><span class="pun">-</span><span class="pln">rpm</span><span class="pun">-</span><span class="pln">config </span><span class="kwd">gcc</span><span class="pln"> </span><span class="kwd">gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> </span><span class="kwd">make</span>
注意:在 Silverblue 上,你需要重新启动才能继续。
字体
你可以通过安装新字体使终端焕然一新。为什么不使用可以同时显示字符和图标的字体呢?
Nerd-Fonts
打开一个新终端,然后键入以下命令:
<span class="kwd">git</span><span class="pln"> </span><span class="kwd">clone</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/ryanoasis/nerd-fonts ~/.nerd-fonts</span>
<span class="kwd">cd</span><span class="pln"> </span><span class="pun">.</span><span class="pln">nerd</span><span class="pun">-</span><span class="pln">fonts</span>
<span class="kwd">sudo</span><span class="pln"> </span><span class="pun">./</span><span class="pln">install</span><span class="pun">.</span><span class="pln">sh</span>
Awesome-Fonts
在工作站上,使用以下命令进行安装:
<span class="kwd">sudo</span><span class="pln"> dnf fontawesome</span><span class="pun">-</span><span class="pln">fonts</span>
在 Silverblue 上键入:
<span class="kwd">sudo</span><span class="pln"> rpm</span><span class="pun">-</span><span class="pln">ostree install fontawesome</span><span class="pun">-</span><span class="pln">fonts</span>
Powerline
Powerline 是 vim 的状态行插件,并为其他几个应用程序也提供了状态行和提示符,包括 bash、zsh、tmus、i3、Awesome、IPython 和 Qtile。你也可以在官方文档站点上找到更多信息。
安装
要在 Fedora 工作站上安装 Powerline 实用程序,请打开一个新终端并运行:
<span class="kwd">sudo</span><span class="pln"> dnf install powerline </span><span class="kwd">vim</span><span class="pun">-</span><span class="pln">powerline </span><span class="kwd">tmux</span><span class="pun">-</span><span class="pln">powerline powerline</span><span class="pun">-</span><span class="pln">fonts</span>
在 Silverblue 上,命令更改为:
<span class="kwd">sudo</span><span class="pln"> rpm</span><span class="pun">-</span><span class="pln">ostree install powerline </span><span class="kwd">vim</span><span class="pun">-</span><span class="pln">powerline </span><span class="kwd">tmux</span><span class="pun">-</span><span class="pln">powerline powerline</span><span class="pun">-</span><span class="pln">fonts</span>
注意:在 Silverblue 上,你需要重新启动才能继续。
激活 Powerline
要使 Powerline 默认处于活动状态,请将下面的代码放在 ~/.bashrc
文件的末尾:
<span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f </span><span class="str">`which powerline-daemon`</span><span class="pln"> </span><span class="pun">];</span><span class="pln"> </span><span class="kwd">then</span>
<span class="pln"> powerline</span><span class="pun">-</span><span class="pln">daemon </span><span class="pun">-</span><span class="pln">q</span>
<span class="pln"> POWERLINE_BASH_CONTINUATION</span><span class="pun">=</span><span class="lit">1</span>
<span class="pln"> POWERLINE_BASH_SELECT</span><span class="pun">=</span><span class="lit">1</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">powerline</span><span class="pun">/</span><span class="kwd">bash</span><span class="pun">/</span><span class="pln">powerline</span><span class="pun">.</span><span class="pln">sh</span>
<span class="kwd">fi</span>
最后,关闭终端并打开一个新终端。它看起来像这样:
Oh-My-Zsh
Oh-My-Zsh 是用于管理 Zsh 配置的框架。它捆绑了有用的功能、插件和主题。要了解如何将 Zsh 设置为默认外壳程序,请参见这篇文章。
安装
在终端中输入:
<span class="pln">sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">"$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"</span>
或者,你也可以输入以下内容:
<span class="pln">sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">"$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"</span>
最后,你将看到如下所示的终端:
恭喜,Oh-my-zsh 已安装成功。
主题
安装后,你可以选择主题。我喜欢使用 powerlevel10k。优点之一是它比 powerlevel9k 主题快 100 倍。要安装它,请运行以下命令行:
<span class="kwd">git</span><span class="pln"> </span><span class="kwd">clone</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/themes/powerlevel10k</span>
并在你的 ~/.zshrc
文件设置 ZSH_THEME
:
<span class="pln">ZSH_THEME</span><span class="pun">=</span><span class="pln">powerlevel10k</span><span class="pun">/</span><span class="pln">powerlevel10k</span>
关闭终端。再次打开终端时,powerlevel10k 配置向导将询问你几个问题以正确配置提示符。
完成 powerline10k 配置向导后,你的提示符将如下所示:
如果你不喜欢它。你可以随时使用 p10k configure
命令来运行 powerline10k 向导。
启用插件
插件存储在 .oh-my-zsh/plugins
文件夹中。要激活插件,你需要编辑 ~/.zshrc
文件。安装插件意味着你创建了一系列执行特定功能的别名或快捷方式。
例如,要启用 firewalld 和 git 插件,请首先编辑 ~/.zshrc
:
<span class="pln">plugins</span><span class="pun">=(</span><span class="pln">firewalld </span><span class="kwd">git</span><span class="pun">)</span>
注意:使用空格分隔插件名称列表。
然后重新加载配置:
<span class="pln">source </span><span class="pun">~/.</span><span class="pln">zshrc</span>
要查看创建的别名,请使用以下命令:
<span class="kwd">alias</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> firewall</span>
更多配置
我建议安装语法高亮和语法自动建议插件。
<span class="kwd">git</span><span class="pln"> </span><span class="kwd">clone</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting</span>
<span class="kwd">git</span><span class="pln"> </span><span class="kwd">clone</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions</span>
将它们添加到文件 ~/.zshrc
的插件列表中。
<span class="pln">plugins</span><span class="pun">=(</span><span class="pln"> </span><span class="pun">[</span><span class="pln">plugins</span><span class="pun">...]</span><span class="pln"> zsh</span><span class="pun">-</span><span class="pln">syntax</span><span class="pun">-</span><span class="pln">highlighting zsh</span><span class="pun">-</span><span class="pln">autosuggestions</span><span class="pun">)</span>
重新加载配置。
<span class="pln">source </span><span class="pun">~/.</span><span class="pln">zshrc</span>
查看结果:
彩色的文件夹和图标
colorls
是一个 ruby gem,可使用颜色和超棒的字体图标美化终端的 ls
命令。你可以访问官方网站以获取更多信息。
因为它是个 ruby gem,所以请按照以下简单步骤操作:
<span class="kwd">sudo</span><span class="pln"> gem install colorls</span>
要保持最新状态,只需执行以下操作:
<span class="kwd">sudo</span><span class="pln"> gem update colorls</span>
为防止每次输入 colorls
,你可以在 ~/.bashrc
或 ~/.zshrc
中创建别名。
<span class="kwd">alias</span><span class="pln"> ll</span><span class="pun">=</span><span class="str">'colorls -lA --sd --gs --group-directories-first'</span>
<span class="kwd">alias</span><span class="pln"> </span><span class="kwd">ls</span><span class="pun">=</span><span class="str">'colorls --group-directories-first'</span>
另外,你可以为 colorls
的选项启用制表符补完功能,只需在 shell 配置末尾输入以下行:
<span class="pln">source $</span><span class="pun">(</span><span class="kwd">dirname</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$gem which colorls</span><span class="pun">))/</span><span class="pln">tab_complete</span><span class="pun">.</span><span class="pln">sh</span>
重新加载并查看会发生什么: