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

好用的shell命令行: fish的配置的更多相关文章

  1. 几种在shell命令行中过滤adb logcat输出的方法

    我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adblog ...

  2. 自学Linux Shell2.1-进入shell命令行

    点击返回 自学Linux命令行与Shell脚本之路 2.1-进入shell命令行 进入文本命令行界面(CLI)两种方法: 控制台终端 图形化终端 1. 通过Linux控制台终端访问CLI 按下Ctrl ...

  3. Hadoop HDFS的shell(命令行客户端)操作实例

    HDFS的shell(命令行客户端)操作实例 3.2 常用命令参数介绍 -help 功能:输出这个命令参数手册 -ls                  功能:显示目录信息 示例: hadoop fs ...

  4. Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本

    Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...

  5. HDFS shell命令行常见操作

    hadoop学习及实践笔记—— HDFS shell命令行常见操作 附:HDFS shell guide文档地址 http://hadoop.apache.org/docs/r2.5.2/hadoop ...

  6. Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本

    Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本 不知道为什么,我总想用 Shell 脚本来实现把一个站点内容给下载下来.但是下载什么站点我确不知道.今天尝试了一下利用 curl ...

  7. Shell 命令行,实现对若干网站状态批量查询是否正常的脚本

    Shell 命令行,实现对若干网站状态批量查询是否正常的脚本 如果你有比较多的网站,这些网站的运行状态是否正常则是一件需要关心的事情.但是逐一打开检查那简直是一件太糟心的事情了.所以,我想写一个 sh ...

  8. Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件

    Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件 前面我写了一篇博文Shell 从日志文件中选择时间段内的日志输出到另一个文件,利用循环实现了我想要实现的内容. 但是用这个脚本的同 ...

  9. Shell 命令行统计 apache 网站日志访问IP以及IP归属地

    Shell 命令行统计 apache 网站日志访问IP以及IP归属地 我的一个站点用 apache 服务跑着,积攒了很多的日志.我想用 shell 看看有哪些人访问过我的站点,并且他来自哪里. 因为日 ...

随机推荐

  1. BZOJ 2752 高速公路(road)(线段树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2752 题意:给出一个数列A,维护两种操作: (1)将区间[L,R]之内的所有数字增加de ...

  2. HDU 4747 Mex(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:给出一个数列A.计算所有的mex(i,j)之和.1<=i<=j<=n. ...

  3. 总结Selenium自动化测试方法(一)自动化测试基础

    总结Selenium自动化测试方法 一.自动化测试基础 1.什么样的项目适合自动化测试 ①任务测试明确,不会频繁变动 ②每日构建后的测试验证 ③比较频繁的回归测试 ④软件系统界面稳定.变动少 ⑤需要在 ...

  4. Velocity模板中的注释

    Velocity ——VTL模板中的注释 注释允许在模板中包含描述文字,而这些文字不会被放置到模板引擎的输出中.注释是一种有效的提醒自己和向别人解释你的VTL语句要做什么事情的方法.你也可以把注释用来 ...

  5. UVa 10905 Children's Game

    注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...

  6. 漫游kafka实战篇之搭建Kafka开发环境

    上篇文章中我们搭建了kafka的服务器,并可以使用Kafka的命令行工具创建topic,发送和接收消息.下面我们来搭建kafka的开发环境.   添加依赖   搭建开发环境需要引入kafka的jar包 ...

  7. *ecshop 模板中foreach用法详解

    1.foreach分以下几个参数 from, item, name, iteration, index 2.使用foreach循环      如果php要传递一个数组(如:$array)给ecshop ...

  8. 深入学习Heritrix---解析处理器(Processor)(转)

    深入学习Heritrix---解析处理器(Processor) 本节解析与处理器有关的内容. 与处理器有关的主要在以下几个类:Processor(处理器类),ProcessorChain(处理器类), ...

  9. 解析AFNetWorking 网络框架(二)

    转:http://blog.csdn.net/andy_jiangbin/article/details/17114989 接着前面写. 本帖先讲AFURLConnectionOperation,它是 ...

  10. vs2010 please select a valid location in the repository

    vs2010 please select a valid location in the repository AnkhSvn版本有问题,我后来使用2.1就ok了记录一下