好用的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.
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 >/dev/null >& $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:\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:\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

很明显,它是一个数组的分隔符.

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[]
# $status 使用一次就会被清空,所有暂存它
set result $status
if [ -eq $result ]
echo command execute success
else
echo $result
echo command execute failed
return
end

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

fish 中对argv参数进行interate:

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

其中 myecho 为:

function myecho
echo $argv[] $argv[]
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[..-]
echo $each_voter
./cleos -v -u http://api.eosnewyork.io push action eosnameswaps vote '[ "p.eos","'$each_voter'" ]' -p $each_voter@active
sleep
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
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
return
end # for a in $argv[..-]
# if test $a = "."
# echo "no"
# return
# end
# end for i in (seq (count $argv))
if test $argv[$i] = "."
echo "do not delete ./"
return
end
if test $argv[$i] = "./"
echo "do not delete ./"
return
end switch $argv[$i]
case "*..*"
echo "do not delete ../ or ../../ or ../file etc."
return
case "-*"
set argv[$i] ""
end
end # if test (count $argv) -lt -o "$argv[1]" = "--help"
# echo ""
# end echo "move it/them to trash."
gio trash $argv
end
上一篇:ORA-04091错误原因与解决方法


下一篇:httpclient org.apache.http.NoHttpResponseException: host:端口 failed to respond 错误原因和解决方法