🌈Fish Shell 安裝配置指南
一、🌟安裝 Fish Shell
YUM安裝 Fish2
yum -y install fish
RPM安裝 Fish3
選擇用於 RHEL 7 的軟件包:fish-3.1.2-1.8.x86_64.rpm
rpm -ivh fish-3.1.2-1.8.x86_64.rpm
二、🌟設置主題 Pure.fish
將主題文件 fish_prompt.fish
拷貝至如下目錄,重新登錄即可
mkdir -p ~/.config/fish/functions
cd ~/.config/fish/functions
三、🌟Fish Shell 使用說明
🔶配置環境變量
Fish 環境變量保存在兩個地方:
- ~/.config/fish/config.fish 用戶環境變量
- /etc/fish/config.fish 全局環境變量
vim ~/.config/fish/config.fish
#在最后一行加入(注意目錄間用空格隔開)
set -x PATH /opt/demo/bin /home/guest/bin $PATH
#最后重新加載fish即可
🔶配置別名(alias)
Fish中使用function來替代alias。定義的function存放於~/.config/fish/functions
中,function名就是文件名,后綴為.fish
,在fish啟動的時候,所有位於functions文件夾里的以后綴.fish
結尾的函數都會被自動加載。
config.fish文件中也可定義alias,等價於function。fish中的function不能在后台運行,即使加&也不能。
使用alias
定義別名的基本示例:
# After fish 3.0 can use -s in shell (recommend)
alias -s l "ls -lah"
# Define alias in shell
alias rmi "rm -i"
# Define alias in config file
alias rmi="rm -i"
# This is equivalent to entering the following function:
function rmi
rm -i $argv
end
# Then, to save it across terminal sessions:
funcsave rmi
每個函數都必須帶參數 $argv,這是shell傳過來的參數。
最后一條命令創建文件
~/.config/fish/functions/rmi.fish
。
🔶切換默認shell為fish
echo $SHELL
cat /etc/shells
chsh -s /usr/bin/fish
🔶執行 Bash 腳本
bash -c SomeBashCommand
🔶命令代換
有別於bash中的`鍵,fish里采用括號來完成命令代換的功能,如
# in bash shell:
[root@urmylucky ~]# echo `date`
Tue Nov 3 13:53:55 CST 2020
# in fish shell:
❯ echo (date)
Tue Nov 3 13:58:03 CST 2020
fish對子命令也使用括號,例如:
for i in (ls)
echo $i
end
🔶搜索歷史命令
bash 中使用 ctrl+r 搜索歷史命令,而在fish中,只需要鍵入想搜索的歷史命令中的某些字母,再按ctrl+p就能不斷搜索歷史命令。
🔶采納建議
->
或 ctr+F
為采納建議,Alt+->
則只采納部分建議。
🔶刪除問候語
[fish2] vim ~/.config/fish/fishd.*
[fish3] vim ~/.config/fish/fish_variables
#將`SET fish_greeting:...`冒號之后的刪除
SET fish_greeting:
四、🌟Fish Shell 語法
Fish 的語法非常自然,一眼就能看懂。
🔽if
語句:
if grep fish /etc/shells
echo Found fish
else if grep bash /etc/shells
echo Found bash
else
echo Got nothing
end
🔽switch
語句:
switch (uname)
case Linux
echo Hi Tux!
case Darwin
echo Hi Hexley!
case FreeBSD NetBSD DragonFly
echo Hi Beastie!
case '*'
echo Hi, stranger!
end
🔽while
循環:
while true
echo "Loop forever"
end
🔽for
循環:
for file in *.txt
cp $file $file.bak
end
五、🌟函數
Fish 的函數用來封裝命令,或者為現有的命令起別名。
function ll
ls -lhG $argv
end
上面代碼定義了一個ll
函數。命令行執行這個函數以后,就可以用ll
命令替代ls -lhG
。其中,變量$argv
表示函數的參數。
下面是另一個例子。
function ls
command ls -hG $argv
end
上面的代碼重新定義ls
命令。注意,函數體內的ls
之前,要加上command
,否則會因為無限循環而報錯。
六、🌟提示符
fish_prompt
函數用於定義命令行提示符(prompt)。
function fish_prompt
set_color purple
date "+%m/%d/%y"
set_color FF0
echo (pwd) '>'
set_color normal
end
執行上面的函數以后,你的命令行提示符就會變成下面這樣。
02/06/13
/home/tutorial
七、🌟配置
Fish 的配置文件是~/.config/fish/config.fish
,每次 Fish 啟動,就會自動加載這個文件。
我們可以在這個文件里面寫入各種自定義函數,它們會被自動加載。比如,上面的fish_prompt
函數就可以寫在這個文件里面,這樣每次啟動 Fish,就會出現自定義的提示符。
Fish 還提供 Web 界面配置該文件。
fish_config
輸入上面的命令以后,瀏覽器就會自動打開本機的 8000 端口,用戶可以在網頁上對 Fish 進行配置,比如選擇提示符和配色主題。