{"id":7491,"date":"2024-02-20T17:21:07","date_gmt":"2024-02-20T17:21:07","guid":{"rendered":"https:\/\/rakhesh.com\/?p=7491"},"modified":"2024-02-20T17:21:07","modified_gmt":"2024-02-20T17:21:07","slug":"mapping-optionleft-arrow-to-go-back-a-word","status":"publish","type":"post","link":"https:\/\/rakhesh.com\/linux-bsd\/mapping-optionleft-arrow-to-go-back-a-word\/","title":{"rendered":"Mapping Option+left arrow to go back a word"},"content":{"rendered":"

On macOS, using bash. I want to map the Option+left arrow<\/code> keys to go back a word, and Option+right arrow<\/code> keys to go forward a word.<\/p>\n

Am sure this is common knowledge, but I wasn’t sure what to do. Thanks to this StackOverflow<\/a> post, I figured it out.<\/p>\n

In the terminal, run cat<\/code>and then press Option+left arrow<\/code>. This appears as ^[^[[D<\/code>. The ^[<\/code> bit is your escape key basically. In bash you’d represent it as<\/a> \\e<\/code>. So ^[^[[D<\/code> translates to \\e\\e[D<\/code> in bash.<\/p>\n

Ditto for Option+right arrow<\/code> which translates to \\e\\e[C<\/code> in bash.<\/p>\n

Armed with these two pieces of info, you can use the builtin bind<\/code> command<\/a> to map them to back and forward word movements.<\/p>\n

bind '\"\\e\\e[D\": backward-word'\r\nbind '\"\\e\\e[C\": forward-word'<\/pre>\n

Add these to .bash_profile<\/code> (or .bashrc<\/code>) and you are in business.<\/p>\n

Extra info<\/strong>: By default the Esc+b<\/code> and Esc+f<\/code> keys are bound to these two. That can be seen by the bind -q<\/code> command.<\/p>\n

$ bind -q backward-word\r\nbackward-word can be invoked via \"\\eb\".\r\n\r\n$ bind -q forward-word\r\nforward-word can be invoked via \"\\ef\".<\/pre>\n

To make this useful in iTerm2 on macOS, one can map the Option<\/code> key to the Esc+<\/code> key<\/a>. This does not map the Option<\/code> key to the Esc<\/code> key, but the Esc+<\/code> action – which basically means Esc<\/code> plus whatever key you press. Treats it like a modifier basically.<\/p>\n","protected":false},"excerpt":{"rendered":"

On macOS, using bash. I want to map the Option+left arrow keys to go back a word, and Option+right arrow keys to go forward a word. Am sure this is common knowledge, but I wasn’t sure what to do. Thanks to this StackOverflow post, I figured it out. In the terminal, run catand then press … Continue reading Mapping Option+left arrow to go back a word<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[76,853],"tags":[264],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/posts\/7491"}],"collection":[{"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/comments?post=7491"}],"version-history":[{"count":2,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/posts\/7491\/revisions"}],"predecessor-version":[{"id":7493,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/posts\/7491\/revisions\/7493"}],"wp:attachment":[{"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/media?parent=7491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/categories?post=7491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakhesh.com\/wp-json\/wp\/v2\/tags?post=7491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}