macos 如何在 OS X 终端中逐字移动光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/81272/
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
How to move the cursor word by word in the OS X Terminal
提问by
I know the combination Ctrl+Ato jump to the beginning of the current command, and Ctrl+Eto jump to the end.
我知道组合Ctrl+A跳转到当前命令的开头,Ctrl+E跳转到结尾。
But is there any way to jump word by word, like Alt+←/→in Cocoa applications does?
但是有没有什么办法可以逐字跳转,就像Cocoa 应用程序中的Alt+ ←/→那样?
采纳答案by Kristian J.
Out of the box you can use the quite bizarre Esc+Fto move to the beginning of the next word and Esc+Bto move to the beginning of the current word.
开箱即用,您可以使用非常奇怪的Esc+F移动到下一个单词的开头,使用Esc+B移动到当前单词的开头。
回答by Varun Katta
On Mac OS X - the following keyboard shortcuts work by default. Note that you have to make Optionkey act like Metain Terminal preferences (under keyboard tab)
在 Mac OS X 上 - 以下键盘快捷键默认有效。请注意,您必须在终端首选项中(在键盘选项卡下)使Option键的行为类似于Meta
- alt (?)+Fto jump Forward by a word
- alt (?)+Bto jump Backward by a word
- alt (?)+F跳˚F一个词orward
- alt (?)+B跳乙由字ackward
I have observed that default emacs key-bindings for simple text navigation seem to work on bash shells. You can use
我观察到用于简单文本导航的默认 emacs 键绑定似乎适用于 bash shell。您可以使用
- alt (?)+Dto delete a word starting from the current cursor position
- ctrl+Ato jump to start of the line
- ctrl+Eto jump to end of the line
- ctrl+Kto kill the line starting from the cursor position
- ctrl+Yto paste text from the kill buffer
- ctrl+Rto reverse search for commands you typed in the past from your history
- ctrl+Sto forward search (works in zsh for me but not bash)
- ctrl+Fto move forward by a char
- ctrl+Bto move backward by a char
- ctrl+Wto remove the word backwards from cursor position
- alt (?)+D从当前光标位置开始删除一个单词
- ctrl+A跳转到行首
- ctrl+E跳转到行尾
- ctrl+K终止从光标位置开始的行
- ctrl+Y从终止缓冲区粘贴文本
- ctrl+R从历史记录中反向搜索您过去输入的命令
- ctrl+S转发搜索(适用于我的 zsh 但不适用于 bash)
- ctrl+F向前移动一个字符
- ctrl+B向后移动一个字符
- ctrl+W从光标位置向后删除单词
回答by Fil
By default, the Terminal has these shortcuts to move (left and right) word-by-word:
默认情况下,终端有这些快捷键可以逐字移动(向左和向右):
- esc+B(left)
- esc+F(right)
- esc+ B(左)
- esc+ F(右)
You can configure alt+←and →to generate those sequences for you:
您可以配置alt+←并→为您生成这些序列:
- Open Terminal preferences (cmd+,);
- At Settings tab, select Keyboard and double-click
? ←
if it's there, or add it if it's not. - Set the modifier as desired, and type the shortcut key in the box: esc+B, generating the text
\033b
(you can't type this text manually). - Repeat for word-right (esc+Fbecomes
\033f
)
- 打开终端首选项(cmd+ ,);
- 在“设置”选项卡上,选择“键盘”并双击
? ←
它(如果它存在),或者如果它不存在则添加它。 - 根据需要设置修饰符,然后在框中键入快捷键:esc+ B,生成文本
\033b
(您不能手动键入此文本)。 - 对 word-right 重复(esc+F变为
\033f
)
Alternatively, you can refer to this blog post over at textmate:
或者,您可以在 textmate 上参考这篇博文:
回答by cwd
Switch to iTerm2. It's free and much nicer than plain old terminal. Also it has a lot more options for customization, like keyboard shortcuts.
切换到iTerm2。它是免费的,而且比普通的旧终端好得多。此外,它还有更多自定义选项,例如键盘快捷键。
Also I love that you can use cmdand 1-9 to switch between tabs. Try it and you will never go back to regular terminal :)
我也喜欢你可以使用cmd和 1-9 在选项卡之间切换。尝试一下,您将永远不会回到常规终端:)
How to set up custom keyboard preferences in iterm2
如何在 iterm2 中设置自定义键盘首选项
- Install iTerm2
- Launch and then go to preference pane.
- Choose the keyboard profiles tab
- You will either need to copy the profile to something new and then delete the arrow key shortcuts such as ^+ Right/Leftor if you don't care about a backup just delete them from the default profile.
- Next make sure your modified profile is selected (starred)
- 安装 iTerm2
- 启动然后转到首选项窗格。
- 选择键盘配置文件选项卡
- 您要么需要将配置文件复制到新的内容,然后删除诸如^+ Right/ 之类的箭头键快捷方式,Left或者如果您不关心备份,只需从默认配置文件中删除它们。
- 接下来确保选择了您修改后的个人资料(已加星标)
- Now choose the keyboard tab (very top row)
- 现在选择键盘选项卡(最顶行)
- Click on the plus button to add a new keyboard shortcut
- In the first box type CMD+Left arrow
- In the second box choose "send escape code"
- In the third box type the letter B
- 单击加号按钮添加新的键盘快捷键
- 在第一个框中键入CMD+Left arrow
- 在第二个框中选择“发送转义码”
- 在第三个框中键入字母 B
- Repeat with desired key combinations. escape+Bmoves one word to the left, escape+fmoves one word to the right.
- you may also wish to set up cmd+dto delete the word in front of the cursor with escape+d
- 重复所需的组合键。escape+B向左移动一个单词,escape+f向右移动一个单词。
- 你也不妨设立cmd+d以删除光标前面的单词escape+d
I often hit the wrong button (cmd/ control/ alt) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.
我经常按错键(cmd/ control/alt有一个箭头键),所以我有我的方向键组合使用这些按钮都设置跳跃前进和后退的话,但是请做哪种最适合您。
回答by mdubez
Actually there is a much better approach. Hold option( alton some keyboards) and press the arrow keys left or right to move by word. Simple as that.
其实还有更好的办法。按住option(alt在某些键盘上)并按向左或向右箭头键按单词移动。就那么简单。
option←
option→
option←
option→
Also ctrlewill take you to the end of the line and ctrlawill take you to the start.
也ctrle将带你到该行的结束,ctrla将带你到开始。
回答by Peter Hilton
I have Alt+←/→working: open Preferences ? Settings ? Keyboard, set the entry for option cursor leftto send string to shell: \033b, and set option cursor rightto send string to shell: \033f. You can also use this for other Control key combinations.
我有Alt+ ←/→工作:打开首选项?设置?键盘,为条目设置选项光标离开来发送字符串外壳:\ 033B,并设置选项光标向右来发送字符串外壳:\ 033f。您也可以将其用于其他 Control 键组合。
回答by Mecki
Actually it depends on what shell you use, however most shells have similar bindings. The bindings you are referring to (e.g. Ctrl+Aand Ctrl+E) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.
实际上这取决于您使用的外壳,但是大多数外壳具有类似的绑定。您所指的绑定(例如Ctrl+A和Ctrl+ E)是您可以在许多其他程序中找到的绑定,并且它们已经使用了很长时间,顺便说一句,也适用于大多数 UI 应用程序。
Here's a look of default bindings for Bash:
下面是 Bash 的默认绑定:
Most Important Bash Keyboard Shortcuts
Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named ".bashrc"that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:
另请注意,您可以自定义它们。您需要创建一个文件,名称随心所欲,我将我的文件命名为 .bash_key_bindings 并将其放入我的主目录中。您可以在那里设置一些常规的 bash 选项,也可以设置键绑定。为了确保它们被应用,您需要修改一个名为“.bashrc”的文件,bash 在启动时读取该文件(您必须创建它,如果它不存在)并在那里进行以下调用:
bind -f ~/.bash_key_bindings
~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.
~ 表示 bash 中的主目录,如上所述,您可以随意命名文件,也可以将其放置在您喜欢的位置,只要您输入正确的路径+名称即可绑定。
Let me show you some excerpts of my .bash_key_bindings file:
让我向您展示我的 .bash_key_bindings 文件的一些摘录:
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
set show-all-if-ambiguous on
set bell-style none
set print-completions-horizontally off
These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).
这些只是设置了几个选项(例如禁用响铃;这可以在 bash 网页上查找)。
"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert
These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).
这些确保单独的字符只是确保字符被“输入”(它们将自己插入到外壳中)。
"\C-dW": kill-word
"\C-dL": kill-line
"\C-dw": backward-kill-word
"\C-dl": backward-kill-line
"\C-da": kill-line
This is quite interesting. If I hit Ctrl+Dalone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).
这很有趣。如果我单独点击Ctrl+ D(我选择 d 进行删除),则什么也不会发生。但是如果我再输入一个小写的 w,光标左边的单词就会被删除。但是,如果我输入大写字母,光标右侧的单词将被删除。对于从光标开始的整行,l 和 L 也是如此。如果我输入“a”,实际上整行都被删除了(光标前后的所有内容)。
I placed jumping one word forward on Ctrl+Fand one word backward on Ctrl+B
我把跳向前移动一个单词Ctrl+F上,一个字向后Ctrl+B
"\C-f": forward-word
"\C-b": backward-word
As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.
如您所见,您可以创建一个快捷方式,立即导致一个操作,或者您可以创建一个,它只是初始化一个字符序列,然后您必须键入一个(或多个)字符以导致一个操作发生在上面的示例中进一步显示。
So if you are not happy with the default bindings, feel free to customize them as you like. Here's a link to the bash manualfor more information.
回答by Dziamid
Use Natural Text Editingpreset!
使用自然文本编辑预设!
Essentially it binds, among other key sequences, Option + LeftArrow to ^[b
sequence and Option + RightArrow to ^[f
本质上,除了其他键序列,它绑定了 Option + LeftArrow 到^[b
序列和 Option + RightArrow 到^[f
This works in fish and bash, as well as in psql terminal.
这适用于 fish 和 bash,以及 psql 终端。
回答by Matt
If you check Use option as meta key
in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and ?F
(Alt+F) and ?B
(Alt+B) respectively.
如果您Use option as meta key
在首选项的键盘选项卡中签入,则默认 emacs 样式命令分别用于前向和后向字以及?F
( Alt+ F) 和?B
( Alt+ B)。
I'd recommend reading From Bash to Z-Shell. If you want to increase your bash/zsh prowess!
我建议阅读From Bash to Z-Shell。如果你想提高你的 bash/zsh 实力!
回答by jches
If you happen to be a Vim user, you could try bash's vim mode. Run this or put it in your ~/.bashrc
file:
如果你碰巧是 Vim 用户,你可以尝试 bash 的 vim 模式。运行这个或把它放在你的~/.bashrc
文件中:
set -o vi
By default you're in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is w
or b
, and the usual movement keys also work.
默认情况下,您处于插入模式;按 Esc 键,您可以像在正常模式 Vim 中一样四处移动,因此逐字移动是w
或b
,并且通常的移动键也可以使用。