51
58
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.
51
58
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.
12
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 \033f
(i.e., 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.
80
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"
@MacLemon, interestingly Esc+f and Esc+b work for me, but I can't hold Esc down and press f or b multiple times; 'f' or 'b' are inserted on subsequent presses. – Kenny Evitt – 2014-12-24T21:44:19.547
25To get the “Alt” bindings to work, the user will have to enable “Use option as meta” in Terminal. This will replace the usual Option-based extended characters and dead keys. – Chris Johnsen – 2010-03-26T15:54:15.937
6Sidenote: Using alt as meta key makes many needed characters like @[]|{} unaccessible on many international keyboard layouts like german or austrian.
You can jump forward or backwards a word by pressing ESC-f and ESC-b respectively. For single characters you can use the arrow keys in most cases.
In Terminal.app: Preferences > Settings [Advanced] you can tick a box to "Delete sends Ctrl-H" to have your delete key behave correctly in apps like vim. – MacLemon – 2010-03-26T18:51:19.557
esc-f does nothing, it just prints f to the terminal for mine – PositiveGuy – 2016-02-18T03:41:15.493
2-1 This does not answer the question. Chris Johnsen answered correctly, but in a comment. – bentford – 2010-11-08T21:22:40.487
1The alt commands doesnt work for me Alt + F, and Alt + B doesnt work. I am running bash – Shervin Asgari – 2011-10-20T11:23:11.030
10
On my UK mac, ALT + cursor left/right goes back/forward one word. Absolutely essential.
6
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"
1The "Use option as meta key" made it work for me. – cyber-monk – 2017-03-13T16:13:32.107
2
You want the READLINE section of the bash(1)
man page:
man 1 bash
/^READLINE
1
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.
TL;DR
option+left-arrow = back one word
option+right-arrow = forward one word
The option chords don't work in my OS X Terminal. – Kenny Evitt – 2014-12-24T21:46:38.723
0
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
duplicate: http://superuser.com/questions/113103/how-can-i-move-around-the-bash-commandline-efficiently ... not OSX specific but does provide a lot of information about standard bash keyboard mappings, customizing readline, and other fun bash tricks.
– quack quixote – 2010-03-26T17:35:53.1636this is not a duplicate of the above question. this one is about how to get OSX not to catch key bindings that are bound for the terminal – Arthur Ulfeldt – 2010-05-13T18:57:38.463
3Please unmark this as duplicate. It needs an answer indicating 'use option as meta key' for OSX terminal. – bentford – 2010-11-08T21:24:36.680