Where do I find a list of terminal key codes to remap shortcuts in bash?

Where do I find a list of terminal key codes to remap shortcuts in bash?

For example:

"\e[1;5C"
"\e[Z"
"\e-1\C-i"

I only know bits and pieces, like \e stands for escape and C- for Ctrl, but what are these numbers (1) and letters (Z)? What are the ;, [ and - signs for?

Is there only trial and error, or is there a complete list of bash key codes and an explanation of their syntax?

Answers


Those are sequences of characters sent by your terminal when you press a given key. Nothing to do with bash or readline per se, but you'll want to know what sequence of characters a given key or key combination sends if you want to configure readline to do something upon a given key press.

When you press the A key, generally terminals send the a (0x61) character. If you press <Ctrl-I> or <Tab>, then generally send the ^I character also known as TAB or \t (0x9). Most of the function and navigation keys generally send a sequence of characters that starts with the ^[ (control-[), also known as ESC or \e (0x1b, 033 octal), but the exact sequence varies from terminal to terminal.

The best way to find out what a key or key combination sends for your terminal, is run sed -n l and to type it followed by Enter on the keyboard. Then you'll see something like:

$ sed -n l
^[[1;5A
\033[1;5A$

The first line is caused by the local terminal echo done by the terminal device (it may not be reliable as terminal device settings would affect it).

The second line is output by sed. The $ is not to be included, it's only to show you where the end of the line is.

Above that means that Ctrl-Up (which I've pressed) send the 6 characters ESC, [, 1, ;, 5 and A (0x1b 0x5b 0x31 0x3b 0x35 0x41)

The terminfo database records a number of sequences for a number of common keys for a number of terminals (based on $TERM value).

For instance:

TERM=rxvt tput kdch1 | sed -n l

Would tell you what escape sequence is send by rxvt upon pressing the Delete key.

You can look up what key corresponds to a given sequence with your current terminal with infocmp (here assuming ncurses infocmp):

$ infocmp -L1 | grep -F '=\E[Z'
    back_tab=\E[Z,
    key_btab=\E[Z,

Key combinations like Ctrl-Up don't have corresponding entries in the terminfo database, so to find out what they send, either read the source or documentation for the corresponding terminal or try it out with the sed -n l method described above.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
### 回答1: 对于 `codes` 中的每一个元素 `i`,执行 `f[i]=codes`。这条语句的意思是将 `f` 字典中键为 `i` 的元素赋值为 `codes`。 注意,这条语句的意思只有在上下文中清楚 `f` 是什么、`codes` 是什么的情况下才能理解。如果缺少这些信息,这条语句的意思就不明确了。 ### 回答2: 这段代码中的语句 for i in codes: f[i]=codes 是一种循环迭代的结构。代码中的 "codes" 是一个可迭代的对象,比如一个列表或者元组。在每一次迭代中,将 "codes" 中的元素赋值给变量 "i",然后执行 f[i]=codes 这一语句。 假设 "codes" 是一个列表,其中存储了几个不同的数值。那么这段代码的含义是,将 "codes" 中的每个元素作为字典 "f" 中的键,将整个 "codes" 列表作为对应键的值。这意味着,字典 "f" 中的每个键都是 "codes" 列表中的一个元素,而对应的值都是整个 "codes" 列表。 举例来说,如果 "codes" 列表是 [1, 2, 3],那么在执行这段代码后,字典 "f" 就会变成 {1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]}。 需要注意的是,由于字典 "f" 的键必须是唯一的,如果 "codes" 列表中有重复的元素,只会有一个对应的键被创建,并且对应的值仍然是整个 "codes" 列表。 总结来说,这段代码的作用是将一个可迭代对象 "codes" 中的元素作为字典 "f" 的键,并将整个 "codes" 对象作为对应键的值,最终得到一个字典 "f"。 ### 回答3: 这段代码的作用是将列表codes中的元素作为字典f的键,并将键的值都设置为codes。代码中的循环语句for i in codes: 表示依次遍历列表codes中的每个元素,并将每个元素赋值给变量i。然后,将变量i作为字典f的键,并将值设置为codes。 假设codes列表为[1, 2, 3],则经过这段代码的处理后,字典f的键值对为{1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]}。可以看到,字典f中的每个键都与codes列表本身相同,而值都是codes列表。 这样的代码可能在某些情况下会有应用,例如当我们需要根据某个值来建立映射关系时,可以使用字典来实现。而此处的代码就是将codes列表的每个元素与codes列表本身建立映射关系。字典的键必须是唯一的,所以可能会出现codes列表中存在相同元素的情况时,后面的元素会覆盖前面的元素。 总之,这段代码的作用是将列表codes的元素作为字典f的键,并将每个键的值都设为codes列表本身。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值