I have been trying to learn the keyboard shortcuts for the shell on macOS, but when I tried using ALT+B, it did not work.
How do you discover, configure and use keybindings in the shell? Any cheatsheets would be helpful.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
One way to deal with 'meta' key sequences not working on the OS X terminal is to assign specific character sequences to particular keypresses. For those of us with non-US keyboards, this is often a better solution than the "Use option as meta" setting mentioned in the comments of other answers. (Many international Mac keyboards are essentially unusable for development work without the Option/alt key because certain critical characters are otherwise unavailable. There's no # on a UK Mac keyboard, for example.)
To get word-left and word-right working for bash, I've used the "Keyboard" section of the Settings in Terminal. You can tell it to generate particular code sequences when particular keypresses are made. I've got mine configured so that alt+◀ generates
\033b (that's actually two characters: Esc, and then a lowercase b) and alt+▶ generates
Esc f). This lets you use the arrow keys with the option key held down to get the word left and right behaviour.
What I've not yet worked out is how to get the Esc key to work - in theory you should be able to use that for 'meta' sequences but it appears not to work. (So just typing Esc+b should go back one word.)
If you have a US keyboard layout, or some other keyboard in which Apple have seen fit to provide all the keys you actually need, then as others have suggested, "Use option as meta key" (also on the Keyboard section of Terminal's settings) is probably a better choice because you'll be able to get to any meta key combination. With that switched on, Alt+b works as expected.
Mac OS X's terminal is BASH, here's some BASH shortcuts:
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
The one you are looking for is Ctrl + H. (This is the same as hitting the backspace key)
If you are looking for an escape character to go back one character, you are looking for
\b. As in:
$ echo -e "one two\b\b\b\b three" # Will echo "one three"
Try SS64. This website is just great for command line reference.
OS X CLI Keyboard Shortcuts as taken from this SS64 page.
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
Note: To use the Alt Key Shortcuts >> Open Terminal Preferences >> Settings Tab >> Keyboard >> Tick "Use option as meta key"
There's really no need to map the option key to meta, in an OS X Terminal, option+left-arrow, option+right-arrow replaces Alt+F and Alt+B respectively. Other commonly used shortcuts all work as implemented on other platforms. esc can emulate the original functionality of the Alt key, but it can be a bit uncomfortable using esc as a modifier, plus you can't hold esc and repeatedly press B or F, you need to let it up and push it back down again each time.
option+left-arrow = back one word
option+right-arrow = forward one word
You might be interested in my profile: https://github.com/lingtalfi/mac-terminal-shortcuts
It provides the following (intuitive) 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