在bash中,我怎么绑定功能键的命令? [英] In bash, how do I bind a function key to a command?

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

问题描述

例如:我想绑定的<大骨节病> F12 到命令键回声foobar的,这样每次我打时间<大骨节病> F12 < /骨节病>消息FOOBAR将被打印到屏幕上。理想的情况下它可以是任意的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?

推荐答案

您可以决定由pressing用钥匙发出的字符序列<大骨节病>控制 - <大骨节病> v 在在命令行,然后pressing你感兴趣的关键。在我的系统为<大骨节病> F12 ,我得到 ^ [[24〜 。在 ^ [重新presents <大骨节病> ESC 。不同类型的终端或终端仿真器可以发出不同codeS为相同的密钥。

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"'

现在,当你preSS <大骨节病> 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\n"'

现在,当您preSS <大骨节病> F12 ,你会得到,而不必preSS <大骨节病>输入显示的当前目录。如果你已经输入就行了的东西,你使用这个自动执行?这可能会导致混乱。但是,您可以清除线路上宏的一部分:

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\n"'

该空间可确保<大骨节病>控制 - <大骨节病> U 有事删除,以保持钟声从铃声

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

一旦你得到了宏的工作您想要的方式,你可以把它持续的将它添加到你的〜/ .inputrc文件文件。没有必要为绑定命令或外集单引号的:

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\n"

编辑:

您还可以创建一个键绑定,将执行的东西,而不会干扰当前命令行。

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

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

然后当你输入一个需要用户名,例如一个命令,你需要知道谁是登录用户的名称,您可以preSS <大骨节病>替代 - <大骨节病>移 - <大骨节病>是W 和输出会显示并及时将您的部分命令完好无损重新发行,光标在线相同的位置。

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.

命令(在这种情况下),可能是任何可执行文件 - 一个程序,脚本或函数

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

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

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