How can I get control+left arrow to go back one word in iTerm2?

122

58

I was reading up in the Linux manual and I noticed that it said I could use control+left and control+right to move forward and back words in the terminal while editing.

"Pressing Ctrl plus the Left or Right arrow key moves backward or forward a word at a time, as does pressing Esc and then B or F."

On OS X control+left and control+right normally control spaces. I have disabled those. I also tried to use the preferences pane to set the keyboard shortcuts:

enter image description here

enter image description here

However this does not work and causes this error:

enter image description here

Also, if I am in iTerm and use alt+escape then B or F the character moves back and forth. This was happening before any of my config changes. But I'd really like to be able to use control + the arrow keys.

cwd

Posted 2011-11-14T18:43:20.670

Reputation: 13 508

Incidentally, a more parsimonious way to achieve this might simply be to map command+left and command+right instead of ctrl+left and ctrl+right. I didn't need to disable anything system-wide to accomplish this. – Autumnsault – 2014-07-16T19:35:21.183

Answers

137

bash

Just add the following to ~/.inputrc:

"\e[1;5D": backward-word
"\e[1;5C": forward-word

See this archived Wiki post for some more explanation. If you want to use the alt key instead for word-to-word movement (like default OS X behavior), use:

"\e[1;9D": backward-word
"\e[1;9C": forward-word

zsh

zsh by default does not use the readline library and therefore won't read ~/.inputrc. To get the same functionality, you could add the following to your ~/.zshrc – this again would use the alt key:

bindkey -e
bindkey '^[[1;9C' forward-word
bindkey '^[[1;9D' backward-word

See this documentation for more about the built-in zsh line editor (zle).


Why is this? You've set up your profile to use the Xterm defaults:

enter image description here

This is why you'll need to "catch" this sequence and tell readline what to do.


If the above still doesn't work and you are using OS X 10.9 (Mavericks) or there abouts, you probably need to disable the global Mission Control shortcuts which prevent Control+arrow keys from reaching iTerm, even if Mission Control itself is disabled. You can do so from System Preferences → Keyboard → Shortcuts → Mission Control:

mission control keyboard prefs

slhck

Posted 2011-11-14T18:43:20.670

Reputation: 182 472

This works but only in the local shell. If I ssh to another box it only moves 1 character. In the default terminal app ^left and ^right work locally and remotely. Anyone have an idea? – marcantonio – 2015-05-22T23:49:02.603

Disregard. I just needed to start a new ssh session! – marcantonio – 2015-05-22T23:55:13.217

@ArturBodera thanks for posting! works perfectly and interprets holding the arrow key down as repeated movements, rather than having to press the arrow key each time for each word (which I had to do with the BetterTouchTool solution http://superuser.com/a/584175/83693)

– Julian A. – 2015-11-27T23:31:41.503

The link http://code.google.com/p/iterm2/wiki/Keybindings is dead, do you know where is it relocated to?

– gerrytan – 2016-01-20T21:45:19.827

@gerrytan You can still access it through the Wayback Archive.

– slhck – 2016-01-21T10:51:03.037

You really don't need bindkey -e to make this work. Maybe you just like EMACS and are trying subconsciously bring people to it. Try it with bindkey -v for vi. – Dr Beco – 2019-10-07T21:29:39.737

4I try to follow the instruction to get the alt + left and alt + right keys working. I added the two lines to my .inputrc file, but the only thing what happens when I press the keys is that either a "D" or a "C" gets printed to the shell. I'm using iTerm2 with zsh shell. Any ideas? – Flo – 2012-11-27T11:23:33.367

1@Flo Try adding the lines bindkey -e, bindkey '^[[1;9C' forward-word and bindkey '^[[1;9D' backward-word to your ~/.zshrc instead. IIRC zsh won't read .inputrc – or at least it works for me this way. – slhck – 2012-11-27T11:49:54.070

@slhck, sure I will create a new question, though alt+left does work on vim, alt+right doesn't. Very weird, I will create a new question now. – seds – 2013-09-02T16:15:08.977

@Ben No idea, sorry! I'm really just the guy who uses vim for commit messages and quick fixes. I bountied your question so you'd get more attention on it. – slhck – 2013-09-06T06:01:07.063

*UGH, god*, THANK YOU. especially for the zsh specific info. I was going to *SCREAM* if I typed D oe more time! – mralexgray – 2013-10-11T06:35:29.527

2Not working for me with ctrl-left/right + bash, but works fine if I set it up for alt-left/right. – Dalin – 2014-01-18T03:22:20.590

3Added an edit about Mission Control shortcuts preventing any of this from working in the most aggravatingly opaque manner. Hopefully it saves someone from going through the same thing I just did urgh... – rubyruy – 2014-01-22T18:22:50.127

12For OSX + zsh + iTerm2 + ctrl-left/right use the following: bindkey -e; bindkey '\e\e[C' forward-word; bindkey '\e\e[D' backward-word – Artur Bodera – 2014-04-11T09:30:24.707

For some reason my OSX preferences were restored to defaults.. – Koen. – 2014-04-28T15:19:26.877

57

Working solution for zsh. Simple, straightforward, out-of-the-box.

  1. Goto: ⌘, Preferences → Profiles → Keys → Keyboard Behavior

  2. Load Preset: Natural Text Editing

Load Preset "Natural Text Editing"

Happy Torturer

Posted 2011-11-14T18:43:20.670

Reputation: 671

1Thanks @Happy Torturer, I believe it's best answer mostly because I don't have on plain Terminal.app that problem so I don't want to modify bash/zsh, just want to work everything like in Terminal.app. – galuszkak – 2017-09-23T17:07:16.387

1This, this, this. Thank you so much for this solution! I've gone through this maddening process with each Mac I get. I now know the simplest way to make this happen. Thank you! – vernonk – 2017-11-22T04:55:16.133

This should be the accepted answer. I wonder, is there any way to load this preset from the command line rather than going through the settings GUI? – Casimir – 2019-12-16T09:20:02.827

9

I fixed it this way:

In top menu; go to

Profiles 
-> Open profiles... 
-> Edit profiles... (button) 
-> Keys (tab) 
-> Load Preset... (dropdown) 
->  Choose "Natural text editing". Done! :-)

rassom

Posted 2011-11-14T18:43:20.670

Reputation: 593

1all i have under that preset is "Factory Defaults" – MaylorTaylor – 2018-09-10T02:29:27.887

7

I used a different approach. Using BetterTouchTool I programmed a custom keyboard shortcut for iTerm2. When I press alt-left in iTerm2, it sends the keyboard shortcut ctrl-left with the action "Send keyboard shortcut to specific application". The application is iTerm2. I did the same for alt-right mapped to ctrl-right.

The effect is that pressing alt-left or alt-right in iTerm sends a ctrl-left or ctrl-right directly to iTerm2, bypassing the usual system-wide shortcut to move a desktop left or right. Like this I get the typical mac behaviour in iTerm2, on local terminal sessions, but also on remote SSH sessions and I can use the standard ctrl-left / ctrl-right to move to different desktops. I wrote it up here:

http://www.callum-macdonald.com/2013/04/17/ctrl-left-and-ctrl-right-on-iterm2/

chmac

Posted 2011-11-14T18:43:20.670

Reputation: 407

2BetterTouchTool is not open source and could very well be doing things you wish it wasn't. The guy says he's a student but he could just as well be a three letter agency. – revacuate – 2016-01-04T17:43:03.200

7

Actually, I found the easiest solution was to go to my profile settings (found in Profiles/Keys), removing the offending profile shortcuts(as profile overrides global in iTerm), in my case alt+left and alt+right and then the global shortcuts worked perfectly for me!

Also, if you're using OS X, it's probably best to stick with system wide shortcuts, i.e. using alt+left and alt+right instead for this purpose, having different behaviour in terminal is bound to cause a pain eventually.

Other than that chmac's solution to use Better Touch Tool was an elegant way to change control+arrow's behaviour in iterm only.

nayyarv

Posted 2011-11-14T18:43:20.670

Reputation: 71

actually this is the best solution. It is confusing as most of us go to Keys tab. Instead we have to go to Profiles tab -> A profile(eg default) -> keys. – Paschalis – 2015-11-03T17:26:30.130

This is definitely the safest and easiest solution. – revacuate – 2016-01-04T19:14:41.473

2

This post teaches this and others shortcuts as ⌥←Delete to delete a word:

http://elweb.co/making-iterm-2-work-with-normal-mac-osx-keyboard-shortcuts/

Seralto

Posted 2011-11-14T18:43:20.670

Reputation: 121

This doesn't actually answer the question, which is about using <ctrl><left> – DavidPostill – 2016-06-02T15:38:37.250

All you have to do is use the shortcut that fits your need. "<ctrl><left>" or whatever you want. – Seralto – 2016-06-02T16:05:22.947

1So please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change. – DavidPostill – 2016-06-02T16:12:56.243

1

You need to go into the Profiles tab and delete the mapping for alt+left and alt+right as by default it outputs some hex values.

Sid

Posted 2011-11-14T18:43:20.670

Reputation: 111

This did it for me! – Philip – 2016-05-26T17:52:21.587