在 bash 中,如何将功能键绑定到命令? [英] In bash, how do I bind a function key to a command?

查看:30
本文介绍了在 bash 中,如何将功能键绑定到命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:我想将 F12 键绑定到命令 echo "foobar" 以便每次我点击 F12 时都会显示消息"foob​​ar"将被打印到屏幕上.理想情况下,它可以是任意的 shell 命令,而不仅仅是内置命令.这是怎么回事?

Example: I want to bind the F12 key to the command echo "foobar" such that every time I hit F12 the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?

推荐答案

你可以通过在命令行按下Ctrl-v来确定一个键发出的字符序列,然后按您感兴趣的键.在我的 F12 系统上,我得到 ^[[24~.^[ 代表 Esc.不同类型的终端或终端模拟器可以为同一个密钥发出不同的代码.

You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, then pressing the key you're interested in. On my system for F12, I get ^[[24~. The ^[ represents Esc. Different types of terminals or terminal emulators can emit different codes for the same key.

在 Bash 提示符下,您可以输入这样的命令来启用密钥宏,以便您可以试用.

At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.

bind '"e[24~":"foobar"'

现在,当您按 F12 时,您将在命令行上看到foobar",可以进行进一步编辑.如果您希望按键立即输入命令,则可以添加换行符:

Now, when you press F12, you'll get "foobar" on the command line ready for further editing. If you wanted a keystroke to enter a command immediately, you can add a newline:

bind '"e[24~":"pwd
"'

现在,当您按 F12 时,无需按 Enter 即可显示当前目录.如果你已经在线上输入了一些东西并且你使用它自动执行怎么办?它可能会变得混乱.但是,您可以清除该行作为宏的一部分:

Now when you press F12, you'll get the current directory displayed without having to press Enter. What if you've already typed something on the line and you use this which automatically executes? It could get messy. However, you could clear the line as part of your macro:

bind '"e[24~":"C-k C-upwd
"'

空格确保 Ctrl-u 可以删除某些内容以防止铃声响起.

The space makes sure that the Ctrl-u has something to delete to keep the bell from ringing.

一旦你让宏按照你想要的方式工作,你可以通过将它添加到你的 ~/.inputrc 文件来使其持久化.不需要 bind 命令或外部单引号集:

Once you've gotten the macro working the way you want, you can make it persistent by adding it to your ~/.inputrc file. There's no need for the bind command or the outer set of single quotes:

"e[24~":"C-k C-upwd
"

您还可以创建一个键绑定,在不干扰当前命令行的情况下执行某些操作.

You can also create a key binding that will execute something without disturbing the current command line.

bind -x '"eW":"who"'

然后,当您键入需要用户名的命令时,例如,您需要知道登录用户的姓名,您可以按 Alt-Shift-Wwho 的输出将被显示,并且将重新发出提示,您的部分命令完好无损,光标在行中的相同位置.

Then while you're typing a command that requires a username, for example, and you need to know the names of user who are logged in, you can press Alt-Shift-W and the output of who will be displayed and the prompt will be re-issued with your partial command intact and the cursor in the same position in the line.

不幸的是,这对于像 F12 这样输出超过两个字符的键不能正常工作.在某些情况下,这可以解决.

Unfortunately, this doesn't work properly for keys such as F12 which output more than two characters. In some cases this can be worked around.

命令(在本例中为who)可以是任何可执行文件——程序、脚本或函数.

The command (who in this case) could be any executable - a program, script or function.

这篇关于在 bash 中,如何将功能键绑定到命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆