Friday 29 March 2019

Using the Terminal keybindings with bash on macOS


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.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...