巧妙的ohmytmux配置

oh my tmux的配置,发现他们很巧妙的将配置和shell函数放到一个文件里

比如切换鼠标模式的相关配置和shell函数,

# : << EOF
# ...省略其他配置文件信息
# toggle mouse
bind m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
# EOF
#
# _toggle_mouse() {
# old=$(tmux show -gv mouse)
# new=""
#
# if [ "$old" = "on" ]; then
# new="off"
# else
# new="on"
# fi
#
# tmux set -g mouse $new
# }
#
# "$@"

主要功能时 将m键绑定上   cut -c3- ~/.tmux.conf | sh -s _toggle_mouse

cut命令切换注释

第一个巧妙的地方时   cut -c3- ~/.tmux.conf ,此命令运行的结果是

: << EOF
...省略其他配置文件信息
toggle mouse
nd m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
EOF _toggle_mouse() {
old=$(tmux show -gv mouse)
new="" if [ "$old" = "on" ]; then
new="off"
else
new="on"
fi tmux set -g mouse $new
} "$@"

一个完美的脚本就出现了!将每一行的前两个字符去掉,

  1. 使用  '<<EOF'  和  'EOF'  完美的将原来的配置放到多行注释里,
  2. 将原来使用 '# '  注释掉的shell函数 打开

sh执行命令

从sh的文档中可以看到 -s 使用执行标准输入里执行命令

OPTIONS
-s Read commands from the standard input.
STDIN
The standard input shall be used only if one of the following is true: * The -s option is specified. * The -c option is not specified and no operands are specified. * The script executes one or more commands that require input from standard input (such as a read command that does not redirect its input). See the INPUT FILES section. When the shell is using standard input and it invokes a command that also uses standard input, the shell shall ensure that the standard input file pointer
points directly after the command it has read when the command begins execution. It shall not read ahead in such a manner that any characters intended to be
read by the invoked command are consumed by the shell (whether interpreted by the shell or not) or that characters that are not read by the invoked command are
not seen by the shell. When the command expecting to read standard input is started asynchronously by an interactive shell, it is unspecified whether charac‐
ters are read by the command or interpreted by the shell. If the standard input to sh is a FIFO or terminal device and is set to non-blocking reads, then sh shall enable blocking reads on standard input. This shall
remain in effect when the command completes.

执行指定函数

从sh的man文档里没有看出来可以指定执行脚本里的哪个函数呢,ohmytmux怎么让sh知道执行哪个函数的呢?

注意到cut后的脚本没,最后一行是  "$@"

在shell中$@用来获得脚本的所有入参,所以   cut -c3- ~/.tmux.conf | sh -s _toggle_mouse 执行的真正脚本时

: << EOF
...省略其他配置文件信息
toggle mouse
nd m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
EOF _toggle_mouse() {
old=$(tmux show -gv mouse)
new="" if [ "$old" = "on" ]; then
new="off"
else
new="on"
fi tmux set -g mouse $new
} _toggle_mouse

这样是不是很明了了,

脚本第一行的  ':' 是shell中的空命令占位符,它什么也不干,永远返回0

<<EOF   到  EOF定义了一个多行文本,这个多行文本没有人用, 相当于把原来的配置都注释掉了

接着 定义一个名字为_toggle_mouse的函数

最后一行  执行该行数

这就是另一个巧妙的地方, 将要执行的函数名当做参数传给shell,并通过$@将 指定的函数 放到最后一样

通过这种方式我们就可以方便的执行shell中指定的函数了

参考

oh my tmux github : https://github.com/gpakosz/.tmux

从标准输入流中读取并执行shell指定函数的更多相关文章

  1. 也谈matlab中读取视频的一个重要函数mmreader

    也谈matlab中读取视频的一个重要函数mmreader 在matlab中输入help mmreader来查阅一下该函数,有如下信息: MMREADER Create a multimedia rea ...

  2. 从hivesql结果中读取数值到shell变量的方法

    为了检查hive表中的数据,并统计展现,需要将查出的结果传入到shell变量,然后统一输出到文本. 最后使用了以下两个方法: 方法一 QUAN=$(hive -S -e "select co ...

  3. expect中使用exec执行shell命令

    今天想在expect脚本中获取本机ip,执行脚本是报错,脚本如下: set localip [exec ifconfig eth0 | grep Mask | cut -d: -f2 | awk '{ ...

  4. python从excel中读取数据传给其他函数使用

    首先安装xlrd库 pip install xlrd 方法1: 表格内容如下: 场景描述,读取该表格A列数据,然后打印出数据 代码何解析如下: import xlrd #引入xlrd库 def exc ...

  5. Java实践 — SSH远程执行Shell脚本(转)

    原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介         SSH是Secure Shell的缩写,一 ...

  6. Java实践 — SSH远程执行Shell脚本

    1. SSH简介         SSH是Secure Shell的缩写,一种建立在应用层和传输层基础上的安全协议.SSH在连接和传送过程中会加密所有数据,可以用来在不同系统或者服务器之间进行安全连接 ...

  7. Jmeter 5.1 从excel读取数据执行接口自动化

    思路:数据在excel文件中进行维护,然后转换成csv格式,jme中读取数据执行: 1.将接口各数据在excel文件中进行维护,然后存为csv格式,我的数据如下: 2.jmeter脚本,配置csv文件 ...

  8. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...

  9. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

随机推荐

  1. 让子弹飞,零成本让你的网站更快一点,boxopened http/3 (QUIC) 协议实战

    最近HTTP-over-QUIC 协议被正式命名为 HTTP/3,协议带来的最大改变是协议底层将采用UDP协议,而不再是TCP协议,这样的好处吗,就是更低时延,更好的拥塞控制,更精确的RTT时间,更高 ...

  2. IDEA 生成返回值对象快捷键Ctrl+Alt+V失效

    在IDEA上运用快捷键返回对象(Ctrl+Alt+V)的时候一直无效,找了很久的问题,发现是有快捷键冲突,发现QQ音乐快捷键与IDEA冲突了,把那处改掉或者关闭即可. 所以边敲代码边听音乐也要注意一下

  3. EMS邮箱数据库常用命令(二)

    1.查询邮箱数据库的使用空间 查询Exchange环境中所有邮箱数据库. 键入以下命令 Get-MailboxDatabase -Status | FL name,DatabaseSize 命令执行后 ...

  4. rem,px,em最大的区别;

    px:px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.移动端的分辨率很多.所以px不适用移动端em:em的值不固定:其长度继承父级元素的字体大小rem:相对于根元素htm ...

  5. 在原有mysql机器上增加一台实例

    采用的是yum install mysql-community-server yum方式安装mysql(社区版) 文章基础上新加一个mysql实例. 这个完全可以直接实战上应用,只要规划好即可 服务器 ...

  6. Educational Codeforces Round 119 (Div. 2), (C) BA-String硬着头皮做, 能做出来的

    题目链接 Problem - C - Codeforces 题目 Example input 3 2 4 3 a* 4 1 3 a**a 6 3 20 **a*** output abb abba b ...

  7. Jx.Cms开发笔记(二)-系统登录

    界面 此界面完全抄了BootstrapAdmin css隔离 由于登录页面的css与其他页面没有什么关系,所以为了防止其他界面的css被污染,我们需要使用css隔离. css隔离需要在_Host.cs ...

  8. HCNP Routing&Switching之组播技术PIM-SM RP

    前文我们了解了组播技术中,组播路由协议PIM的稀疏模式相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16142795.html:今天我们来聊一聊组播路 ...

  9. 24张图攻克border-image

    大家好,我是半夏,一个刚刚开始写文的沙雕程序员.如果喜欢我的文章,可以关注 点赞 加我微信:frontendpicker,一起学习交流前端,成为更优秀的工程师-关注公众号:搞前端的半夏,了解更多前端知 ...

  10. 异步请求与中断 ( XHR,Axios,Fetch对比 )

    随着AJAX技术的诞生,前端正式进入了局部刷新和前后端分离的新时代,最初的服务请求技术是XHR,随着技术发展和ES6的诞生,jquery ajax,axios,fetch 等技术的产生让前端的异步请求 ...