好用的shell命令行: fish的配置


fish的可視化配置命令:

$ fish_config

 

fish 很好的資源列表:

https://github.com/JorgeBucaran/awesome-fish

 

 

其配置文件夾為 ~/.config/fish。

1、要設置環境變量,在配置文件夾里新建 config.fish 文件,它會作為fish 啟動時的加載文件,相當與bash的.bashrc,然后在里面配置環境變量,其環境變量配置方法與bash不同,格式如下:

# There are three kinds of variables in fish: universal, global and local variables. 
# Universal variables are shared between all fish sessions a user is running on one computer. 
# Global variables are specific to the current fish session, but are not associated with any 
#   specific block scope, and will never be erased unless the user explicitly requests it using set -e. 
# Local variables are specific to the current fish session, and associated with a specific block of 
#   commands, and is automatically erased when a specific block goes out of scope. 
# A block of commands is a series of commands that begins with one of the commands for, while , if, function, 
#   begin or switch, and ends with the command end. 
# The user can specify that a variable should have either global or local scope using the -g/--global or -l/--local switches.

# Variables can be explicitly set to be universal with the -U or --universal switch, 
# global with the -g or --global switch, or local with the -l or --local switch.

set -x JAVA_HOME /home/hzh/hzh/soft/jdk
set -x JRE_HOME {$JAVA_HOME}/jre
# 按照bash風格用:來分割多個值,但在fish中PATH不能用:來分割,必須用空格分割 set
-x CLASSPATH .:{$JAVA_HOME}/lib:{$JRE_HOME}/lib set -x PATH {$JAVA_HOME}/bin {$PATH} set -x ANDROID_HOME /home/zhou/android/android_adt/sdk set -x PATH {$PATH} {$ANDROID_HOME}/build-tools/22.0.1 set -x PATH {$PATH} {$ANDROID_HOME}/platform-tools set -x PATH {$PATH} {$ANDROID_HOME}/tools set -x NDK_HOME /home/zhou/android/android_adt/ndk set -x PATH {$PATH} {$NDK_HOME} set -x PATH {$PATH} /home/zhou/android/android_adt/eclipse set -x GRADLE_HOME /home/hzh/hzh/soft/gradle set -x PATH {$PATH} {$GRADLE_HOME}/bin
# 設置局部變量,必須用空格分割name及value,不能象bash那樣用=來賦值 set
-l MAVEN_HOME '/home/hzh/hzh/soft/maven' set -x PATH {$PATH} {$MAVEN_HOME}/bin set -x CATALINA_HOME /home/hzh/hzh/soft/tomcat

 

2、若要定義alias,但fish里沒有alias這個說法,因此官方介紹用function來替代alias。具體是這樣的,在配置文件夾里( ~/.config/fish/ )新建一個名為functions的文件夾(建完之后 ~/.config/fish/functions),此文件夾里存放你所定義的function,function名就是文件名,后綴為.fish, 在fish啟動的時候,所有位於functions文件夾里的以后綴.fish結尾的函數都會被自動加載,這樣就定義了一個alias。如:

functions/meld.fish:

function meld --description 'compare files'
  /usr/bin/meld 1>/dev/null 2>&1 $argv
end

每個函數都必須帶參數 $argv,這是shell傳過來的參數。

上面的說法有誤,fish有alias的,放到 config.fish 里,但是它也等價於函數,無法這后台運行:

# 下面這句可以后台運行,因為 xdg-open 本身就是后台運行的程序
alias hopen 'xdg-open 2>/dev/null'

 

3、有別與bash的`鍵,fish里采用括號來完成命令執行的功能:

在bash中,使用 ls `which ls` 可以顯示 /bin/ls。 而在fish中,使用 ls (which ls) 可以顯示 /bin/ls。 用 echo a(data)則輸出: a2016年 09月 23日 星期五 15:49:18 CST。

 

4、bash中的ctrl+r的搜索歷史命令的功能(不斷按ctrl+r可以繼續搜索),在fish中已經得到了很好的解決,你只需要鍵入你想搜索的歷史命令中的某寫字母,再按ctrl+p就能不斷搜索歷史命令。

 

5、fish的function不能運行在后台,即加 & 沒什么用。

 

6、fish的prompt, 在functions目錄加入文件 fish_prompt.fish ,其內容為:

# 其中的 (hostname) (prompt_pwd) 為shell命令, $USER 為環境變量, __fish_prompt_hostname 為臨時變量
function
fish_prompt if not set -q __fish_prompt_hostname set -g __fish_prompt_hostname (hostname) end set_color -o cyan echo -n -s "$USER" @ "$__fish_prompt_hostname" ": " set_color -o green echo -n (prompt_pwd)
# 也可以用這個: echo -n ' $ '
echo -n " \$ " set_color normal end

 

7、去掉歡迎信息(greeting message), 在fishd.* 文件中,替換SET fish_greeting:*** SET fish_greeting:  注意冒號。(如下文件只修改有注釋的地方就可以了)

# This file is automatically generated by the fish.
# Do NOT edit it directly, your changes will be overwritten.
SET __fish_init_1_50_0:\x1d
SET fish_color_autosuggestion:555\x1eyellow
# SET fish_color_command:005fd7\x1epurple SET fish_color_command:ffff00\x1epurple\x1e--bold SET fish_color_comment:red SET fish_color_cwd:green SET fish_color_cwd_root:red SET fish_color_error:red\x1e\x2d\x2dbold SET fish_color_escape:cyan SET fish_color_history_current:cyan SET fish_color_match:cyan SET fish_color_normal:normal SET fish_color_operator:cyan # SET fish_color_param:00afff\x1ecyan
SET fish_color_param:00ff00\x1ecyan\x1e--bold SET fish_color_quote:brown SET fish_color_redirection:normal SET fish_color_search_match:\x2d\x2dbackground\x3dpurple SET fish_color_selection:\x2d\x2dbackground\x3dpurple SET fish_color_valid_path:\x2d\x2dunderline # SET fish_greeting:Welcome\x20to\x20fish\x2c\x20the\x20friendly\x20interactive\x20shell\x0aType\x20\x1b\x5b32mhelp\x1b\x5b30m\x1b\x28B\x1b\x5bm\x20for\x20instructions\x20on\x20how\x20to\x20use\x20fish SET fish_greeting: SET fish_key_bindings:fish_default_key_bindings SET fish_pager_color_completion:normal SET fish_pager_color_description:
555\x1eyellow SET fish_pager_color_prefix:cyan SET fish_pager_color_progress:cyan

 

8、fish種的 \x1e 是什么,其實就一個分隔符,它的定義為:

/** Character for separating two array elements. We use 30, i.e. the ascii record separator since that seems logical. */
#define ARRAY_SEP 0x1e

/** String containing the character for separating two array elements */
#define ARRAY_SEP_STR L"\x1e"

測試結果:

    # 數組 [a] [b]
$ set aaa (printf 'a\x1eb') $ count $aaa 2
    # 數組 [a\x1fb]
$ set aaa (printf 'a\x1fb') $ count $aaa 1

很明顯,它是一個數組的分隔符.

 

ubuntu下fish shell設置 256 color:

首先運行:

  1. tput colors   -This will report how many colors your terminal is using.
  2. echo $TERM   -This will tell you what terminal you are using.
  3. echo $COLORTERM  -If you are using a gnome you should see gnome-terminal.

確保使用的是 xterm。

在終端點擊菜單  編輯-》配置文件首選項, 再點“命令”tab, 然后勾選上“運行自定義命令而不是shell”,在里面敲入:  env TERM=xterm-256color /usr/bin/fish    

 

 

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 的函數

Fish 的函數用來封裝命令,或者為現有的命令起別名,可在配置文件夾里定義函數文件(文件名必須是函數名),或者在命令行直接定義函數:

function llss
    ls -lhG $argv
end

上面代碼定義了一個llss函數。命令行執行這個函數以后,就可以用llss命令替代ls -lhG。其中,變量$argv表示函數的參數,每個定義的函數都必須帶這個參數,由fish負責參數值的傳遞。

下面是另一個例子:

function ls
    command ls -hG $argv
end

上面的代碼重新定義ls命令。注意,函數體內的ls之前,要加上command,否則會因為無限循環而報錯。

 

下面是我自己寫的一個較完整的fish函數示例:

function testecho
    echo $argv[1]
    # $status 使用一次就會被清空,所有暫存它
    set result $status
    if [ 0 -eq $result ]
        echo command execute success
    else 
        echo $result
        echo command execute failed
        return
    end

  # 下面的命令執行結果不為0,即執行不成功 SOME_ERROR_COMMAND set result $status
if [ 0 -eq $result ] echo command execute success else echo $result echo command execute failed return end sleep 1 echo $argv[2] end

 

fish 中對argv參數進行interate:

function sss
    for a in $argv
        set aa (math "$a+1")
        myecho $a $aa
    end
    echo ""
    for a in $argv[1..-1]
        myecho $a $a+1
    end
end

其中 myecho 為:

function myecho
    echo $argv[1] $argv[2]
end

 

 

對於單引號和雙引號的使用請參照如下示例:

$ A=B\ C
$ echo '"$A"'        # 最外面的是單引號, 輸出結果:    "$A"
$ echo "'$A'"        # 最外面的是雙引號, 輸出結果:    'B C'

 

function vote_eosnameswaps      --description 'vote each account of eosnameswaps'
    echo "begin ..."
    echo ""

    set CLEOS /home/hzh/github/eos_build/programs/cleos/cleos

    for each_voter in $argv[1..-1]
        echo $each_voter
        ./cleos -v -u http://api.eosnewyork.io push action eosnameswaps vote '[ "p.eos","'$each_voter'" ]' -p $each_voter@active
        sleep 1
    end

    echo ""
    echo "finished"
end

 

fish shell 讀取文本文件然后將頭尾的空字符去掉,替換中間所有2個及2個以上的連續空字符為一個空格,然后在分割成可以單獨使用的元素:

function read-file-trim-split
    for line in (cat /tmp/hzh)
        set all "$all $line"
    end
    set all (string trim $all)
    set all (string replace -ar "\\s{2,}" " " $all)
    set all (string split " " $all)
    echo $all
    for word in $all
        echo $word
    end
end

 

fish shell 中的比較和數學運算:

function go
    set d 66
    while true
        set d (math "$d * 1.1 + 0.003")
        if math "$d > 117"
            break
        end
        echo $d
    end
end

 

fish 中的字符串比較,參數iterate,  switch等:

function rm     --description 'vote each account of eosnameswaps'
    if test (count $argv) -lt 1
        return 0
    end

#    for a in $argv[1..-1]
#        if test $a = "."
#            echo "no"
#            return 1
#        end
#    end

    for i in (seq (count $argv))
        if test $argv[$i] = "."
            echo "do not delete ./"
            return 1
        end
        if test $argv[$i] = "./"
            echo "do not delete ./"
            return 1
        end

        switch $argv[$i]
        case "*..*"
            echo "do not delete ../ or ../../ or ../file   etc."
            return 1
        case "-*"
            set argv[$i] ""
        end
    end

#        if test (count $argv) -lt 2 -o "$argv[1]" = "--help"
#            echo ""
#        end

    echo "move it/them to trash."
    gio trash $argv
end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM