在 macOS 上使用带有 bash 的终端键绑定

Ope*_*way 51 keyboard-shortcuts bash terminal.app macos

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

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

Ark*_*rko 81

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    Let’s 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
Run Code Online (Sandbox Code Playgroud)

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

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

$ echo -e "one two\b\b\b\b three" # Will echo "one three"
Run Code Online (Sandbox Code Playgroud)

  • 要使“Alt”绑定起作用,用户必须在 *Terminal* 中启用“Use option as meta”。这将取代通常的基于 Option 的扩展字符和死键。 (26认同)
  • 旁注:使用 alt 作为元键会使许多需要的字符(如@[]|{})在许多国际键盘布局(如德语或奥地利语)上无法访问。您可以分别按 ESC-f 和 ESC-b 向前或向后跳转一个单词。对于单个字符,您可以在大多数情况下使用箭头键。在 Terminal.app: Preferences > Settings [Advanced] 中,您可以勾选一个框以“删除发送 Ctrl-H”,以使您的删除键在 vim 等应用程序中正确运行。 (6认同)
  • -1 这不能回答问题。克里斯约翰森回答正确,但在评论中。 (2认同)
  • alt 命令对我不起作用 Alt + F,Alt + B 不起作用。我正在运行 bash (2认同)

小智 12

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

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

我还没有弄清楚如何获得Esc工作的关键——理论上你应该能够将它用于“元”序列,但它似乎不起作用。(所以只要输入Esc+ 就b可以返回一个词。)

如果您有美式键盘布局,或 Apple 认为适合提供您实际需要的所有键的其他键盘,那么正如其他人所建议的,“使用选项作为元键”(也在终端设置的键盘部分)可能是更好的选择,因为您将能够使用任何元键组合。打开后,Alt+b按预期工作。


小智 9

在我的英国 mac 上,ALT + 向左/向右光标向后/向前移动一个词。绝对必要。


Rah*_*kur 6

试试SS64。这个网站非常适合命令行参考。

OS X CLI 键盘快捷键取自此 SS64 页面

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
Run Code Online (Sandbox Code Playgroud)

注意:要使用 Alt Key Shortcuts >> Open Terminal Preferences >> Settings Tab >> Keyboard >> 勾选“Use option as meta key”

终端偏好