Bash CTRL 在单词/字符串之间移动光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5029118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash CTRL to move cursor between words/strings
提问by Bryan Ruiz
I am use to using the CTRLkey to move faster when using the left and right arrow keys (goes to end of a word, instead of one char at a time).
我CTRL习惯于在使用左右箭头键时使用该键更快地移动(转到单词的末尾,而不是一次一个字符)。
Can I do that in bash somehow?
我可以以某种方式在 bash 中做到这一点吗?
I could probably code it, but I was wondering if there is something easier / already done.
我可能可以编码它,但我想知道是否有更简单的/已经完成的事情。
回答by Thomas
With the default readline key bindings, ALT+Bgoes back one word, ALT+Fgoes forward one word.
使用默认的 readline 键绑定,ALT+B后退一个词,ALT+F前进一个词。
The default Ubuntu setup additionally provides CTRL+arrows like you're used to. These are in /etc/inputrc
and specified as follows:
默认的 Ubuntu 设置还CTRL像您习惯的那样提供+ 箭头。这些在/etc/inputrc
并指定如下:
# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
Not sure why we need three of them...
不知道为什么我们需要三个...
回答by Bryan Ruiz
As Thomas explained, you can add the bindings to /etc/inputrc
.
正如 Thomas 所解释的,您可以将绑定添加到/etc/inputrc
.
Another alternative so it loads every time you log in, is putting them in ~/.bashrc
like this:
每次登录时都会加载的另一种选择是将它们放入~/.bashrc
:
#use ctl keys to move forward and back in words
bind '"\eOC":forward-word'
bind '"\eOD":backward-word'
I learned that you can use cat > /dev/null
to look at the characters that your keyboard is sending, e.g., CTRL + right arrow shows:
我了解到您可以使用cat > /dev/null
来查看键盘发送的字符,例如 CTRL + 向右箭头显示:
^[OC
where ^[
is the same as \e
so that's where the code comes from in the bind
command.
where^[
与\e
so that 是bind
命令中代码的来源相同。
You can also look up bindings like this:
您还可以像这样查找绑定:
bind -p | grep forward-word
All of this is pretty damn awesome and I'm glad I found out some more power of bash.
所有这些都非常棒,我很高兴我发现了 bash 的更多功能。
回答by Constantin De La Roche
A .inputrc in your home directory will cause ctrl+left to stop working on Ubuntu (for example).
主目录中的 .inputrc 将导致 ctrl+left 停止在 Ubuntu 上工作(例如)。
To get everything working, add the following to ~/.inputrc:
要使一切正常,请将以下内容添加到 ~/.inputrc:
# Include system-wide inputrc, which is ignored by default when
# a user has their own .inputrc file.
$include /etc/inputrc
credit to f.kowal
归功于f.kowal