# allow CTRL-Q and CTRL-S keybindings
vim() {
(
# No ttyctl, so we need to save and then restore terminal settings
# osx users, use stty -g
local STTYOPTS="$(stty --save 2> /dev/null)"
trap "stty $STTYOPTS" EXIT
stty stop '' start '' -ixoff
command vim "$@"
)
}
我正在使用上面的 shell 函数临时更改 stty 选项,以便 CTRL-Q 和 CTRL-S 可以在 vim 中用作键绑定(bind)。
这很好用,但作为副作用,当我用 CTRL-Z 暂停 vim 时,我无法再看到哪个文件对应于后台作业。我经常在后台处理多个 session ,能够再次查看哪个作业是哪个作业真的很方便。
具有后台任务的作业的当前输出:
root@rock64:~# jobs
[1]+ Stopped ( local STTYOPTS="$(stty --save 2> /dev/null)"; trap "stty $STTYOPTS" EXIT; stty stop '' start '' -ixoff; command vim "$@" )
root@rock64:~#
像这样展开的输出将是理想的:
root@rock64:~# jobs
[1]+ Stopped vim .bashrc
root@rock64:~#
是否有其他方法可以在不压缩后台作业列表的情况下实现相同的效果(临时更改 STTY 选项并在作业完成时恢复)?
我目前正在运行 Bash 4.4.x,但如果需要,我可以轻松编译更新的版本。
最佳答案
因此,如果没有更好的代码格式,我在评论中的建议就很难传达。我只是建议删除周围的 (
和 )
。此外,需要删除 RETURN 陷阱,所以我只创建了第二个函数,当 return
被调用时,我们跳回到原始函数并删除陷阱。
runvim() {
local STTYOPTS="$(stty --save 2> /dev/null)"
trap "stty $STTYOPTS" RETURN # This may need to be changed to RETURN
stty stop '' start '' -ixoff
command vim "$@"
}
# allow CTRL-Q and CTRL-S keybindings
vim() {
# No ttyctl, so we need to save and then restore terminal settings
# osx users, use stty -g
runvim "$@"
trap - RETURN
}
备选
你可以把它放在 $HOME/bin
中名为 vim
的脚本中:
#!/bin/bash
STTYOPTS="$(stty --save 2> /dev/null)"
trap "stty $STTYOPTS" EXIT
stty stop '' start '' -ixoff
#/usr/bin/vim "$@" # or where ever your vim is
$( whereis vim | cut -d\ -f3) "$@" # Here is a more generic version.
然后通过将 export PATH="$HOME/bin:$PATH"
添加到您最喜欢的点文件,将该目录添加到您的 PATH
变量的前面。
关于linux - Bash 函数在作业暂停时压缩 'jobs' 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941976/