172

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.

Gareth
  • 19,080
cwd
  • 18,788
  • 43
  • 123
  • 162

8 Answers8

177

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 to use ctrl:

bindkey -e
bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word 

To 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, 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 Settings → Keyboard → Keyboard Shortcuts... → Mission Control. Don't miss the dropdown called again "Mission Control" that you need to expand to find "Move left a space" and "Move right a space".

slhck
  • 235,242
151

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"

30

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! :-)

enter image description here

Player1
  • 107
rassom
  • 903
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
  • 477
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
  • 71
5

In the newer versions of iTerm (as of writing 3.5.11):

Navigate to SettingsProfilesKeysKey Mappings.

From Presets dropdown, select Natural Text Editing.

Saikat
  • 467
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
  • 121
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.

Giacomo1968
  • 58,727
Sid
  • 111