在macOS上将终端键绑定与bash一起使用


51

我一直在尝试学习macOS上Shell的键盘快捷键,但是当我尝试使用ALT+时B,它不起作用。

您如何在外壳中发现,配置和使用键绑定?任何备忘单都会有所帮助。


重复的:superuser.com/questions/113103/… ...并非特定于OSX,但确实提供了大量有关标准bash键盘映射,自定义readline以及其他有趣bash技巧的信息。
奎克吉德

6
这不是上述问题的重复。这是关于如何使OSX不捕获绑定到终端的键绑定的内容
Arthur Ulfeldt 2010年

3
请取消标记为重复项。对于OSX终端,需要一个指示“使用选项作为元密钥”的答案。
bentford 2010年

Answers:


12

处理在OS X终端上不起作用的“元”键序列的一种方法是将特定的字符序列分配给特定的按键。对于使用非美式键盘的我们来说,这通常是比其他答案的注释中提到的“将选项用作元数据”设置更好的解决方案。(许多国际Mac键盘在没有Option / alt键的情况下基本上无法用于开发工作,因为否则某些关键字符将不可用。#例如,在英国Mac键盘上则没有。)

为了使bash的单词左右对齐,我使用了终端中“设置”的“键盘”部分。您可以告诉它在进行特定的按键操作时生成特定的代码序列。我已经配置好了,以便alt+ 生成\033b(实际上是两个字符:Esc,然后是小写b)和alt+ 生成\033f(即Esc f)。这使您可以使用箭头键并同时按住Option键来获得单词的左右行为。

我还没有解决的是如何获得Esc工作的钥匙-从理论上讲,您应该可以将其用于“元”序列,但似乎不起作用。(因此,只需输入Esc+ b即可返回一个单词。)

如果您使用的是美国键盘布局,或者Apple认为可以提供您实际需要的所有键的某些其他键盘,那么正如其他人所建议的那样,“将选项用作元键”(也位于“终端”设置的“键盘”部分)可能是一个更好的选择,因为您将能够使用任何元键组合。启用该功能后,Alt+会b按预期工作。


80

Mac OS X的终端是BASH,这是一些BASH快捷方式:

Ctrl + A    Go to the beginning of the line you are currently typing on
Ctrl + E    Go to the end of the line you are currently typing on
Ctrl + L    Clears the Screen, similar to the clear command
Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H    Same as backspace
Ctrl + R    Lets you search through previously used commands
Ctrl + C    Kill whatever you are running
Ctrl + D    Exit the current shell
Ctrl + Z    Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W    Delete the word before the cursor
Ctrl + K    Clear the line after the cursor
Ctrl + T    Swap the last two characters before the cursor
Esc + T  Swap the last two words before the cursor
Alt + F  Move cursor forward one word on the current line
Alt + B  Move cursor backward one word on the current line
Tab      Auto-complete files and folder names

您要寻找的是Ctrl+ H。(这与按退格键相同)

如果您正在寻找一个转义字符以返回一个字符,那么您正在寻找\b。如:

$ echo -e "one two\b\b\b\b three" # Will echo "one three"

25
为了使“ Alt”绑定生效,用户必须在Terminal中启用“ Use option as meta” 。这将替换通常的基于Option的扩展字符和死键。
克里斯·约翰森

6
旁注:将alt用作元键会使许多所需的字符(如@ [] | {})在许多国际键盘布局(如德语或奥地利语)上无法访问。您可以通过分别按ESC-f和ESC-b来向前或向后跳一个单词。对于单个字符,大多数情况下可以使用箭头键。在Terminal.app:首选项>设置[高级]中,可以在“删除发送Ctrl-H”上打勾,以使删除键在vim之类的应用中正常运行。
MacLemon

2
-1这不能回答问题。克里斯·约翰森(Chris Johnsen)回答正确,但发表了评论。
bentford 2010年

1
alt命令对我不起作用Alt + F,而Alt + B不起作用。我运行bash
Shervin阿斯卡里

@MacLemon,有趣的是Esc + fEsc + b对我有用,但是我无法按住Esc并多次按fb;在随后的印刷机上插入“ f”或“ b”。
肯尼·埃维特

10

在我的英国Mac电脑上,ALT +光标向左/向右后退/前进一个字。绝对必要。


6

尝试SS64。该网站非常适合命令行参考。

从此SS64页面获取的OS X CLI键盘快捷键。

Bash Keyboard Shortcuts

Moving the cursor:
  Ctrl + a   Go to the beginning of the line (Home)
  Ctrl + e   Go to the End of the line (End)
  Ctrl + p   Previous command (Up arrow)
  Ctrl + n   Next command (Down arrow)
   Alt + b   Back (left) one word
   Alt + f   Forward (right) one word
  Ctrl + f   Forward one character
  Ctrl + b   Backward one character
  Ctrl + xx  Toggle between the start of line and current cursor position

Editing:
 Ctrl + L   Clear the Screen, similar to the clear command
 Ctrl + u   Cut/delete the line before the cursor position.
  Alt + Del Delete the Word before the cursor.
  Alt + d   Delete the Word after the cursor.
 Ctrl + d   Delete character under the cursor
 Ctrl + h   Delete character before the cursor (backspace)
 Ctrl + w   Cut the Word before the cursor to the clipboard.
 Ctrl + k   Cut the Line after the cursor to the clipboard.
  Alt + t   Swap current word with previous
 Ctrl + t   Swap the last two characters before the cursor (typo).
 Esc  + t   Swap the last two words before the cursor.
 Ctrl + y   Paste the last thing to be cut (yank)
  Alt + u   UPPER capitalize every character from the cursor to the end of the current word.
  Alt + l   Lower the case of every character from the cursor to the end of the current word.
  Alt + c   Capitalize the character under the cursor and move to the end of the word.
  Alt + r   Cancel the changes and put back the line as it was in the history (revert).
 Ctrl + _   Undo

 TAB        Tab completion for file/directory names
For example, to move to a directory 'sample1'; Type cd sam ; then press TAB and ENTER. 
type just enough characters to uniquely identify the directory you wish to open.

History:
  Ctrl + r   Recall the last command including the specified character(s)
             searches the command history as you type.
             Equivalent to : vim ~/.bash_history. 
  Ctrl + p   Previous command in history (i.e. walk back through the command history)
  Ctrl + n   Next command in history (i.e. walk forward through the command history)
   Alt + .   Use the last word of the previous command
  Ctrl + s   Go back to the next most recent command.
            (beware to not execute it from a terminal because this will also launch its XOFF).
  Ctrl + o   Execute the command found via Ctrl+r or Ctrl+s
  Ctrl + g   Escape from history searching mode

Process control:
 Ctrl + C   Interrupt/Kill whatever you are running (SIGINT)
 Ctrl + l   Clear the screen
 Ctrl + s   Stop output to the screen (for long running verbose commands)
 Ctrl + q   Allow output to the screen (if previously stopped using command above)
 Ctrl + D   Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT)
 Ctrl + Z   Send the signal SIGTSTP to the current task, which suspends it.
            To return to it later enter fg 'process name' (foreground).

Emacs mode vs Vi Mode    
All the above assume that bash is running in the default Emacs setting, if you prefer this can be switched to Vi shortcuts instead.

Set Vi Mode in bash:
$ set -o vi

Set Emacs Mode in bash:    
$ set -o emacs

注意:要使用Alt键快捷方式>>打开终端首选项>>设置选项卡>>键盘>>勾选“将选项用作元键”

终端首选项


1
“使用选项作为元密钥”使其对我有用。
网络僧侣


1

真的没有需要映射的option关键元,在OS X终端,option+ left-arrowoption+ right-arrow内容替换Alt+ FAlt+ B分别。其他常用的快捷方式都可以在其他平台上实现。 esc可以模仿Alt按键的原始功能,但esc用作修饰符可能会有点不舒服,而且您无法按住esc并反复按BF,您需要每次都放开并再次向下推。

TL; DR

option+ left-arrow=返回一个字

option+ right-arrow=转发一个字


选项和弦不要在我的OS X的终端工作。
肯尼·埃维特

0

您可能对我的个人资料感兴趣:https : //github.com/lingtalfi/mac-terminal-shortcuts

它提供了以下(直观的)快捷方式:

ALT-left: move one word backward
ALT-right: move one word forward
CTRL-left: move to the beginning of the line
CTRL-right: move to the end of the line
ALT-backspace: kill one word backward
ALT-del: kill one word forward
ALT-up: set word after cursor to uppercase
ALT-down: set word after cursor to lowercase
CTRL-backspace: Same as ALT-backspace
CTRL-del: Same as ALT-del
home: move to the beginning of the line
end: move to the end of the line
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.