Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Modify cursor movement keys in Terminal Apps
The defacto standard in Terminal's shells is meta-b (backward) and meta-f (forward) for word-by-word movement. In every other Cocoa app, however, it's Option-left arrow and Option-right arrow. I wanted Terminal to behave the same way.

Terminal has a Keyboard page in the Terminal Inspector which allows you to map keys any way you want. For example, to make Option-left arrow move to the previous word, you only need to add a new key binding entry like the following:
  • Open Terminal.
  • Go to File > Show Info (or type Command-I) to bring up the Terminal Inspector.
  • Select Keyboard from the drop-down menu at the top of the Inspector.
  • Press Add.
  • Set the following:
    • Key = cursor left
    • Modifier = option
    • Action = send string to shell:
  • Press Esc (this should insert the text \033 in the text box) and then press b.
  • Press OK.
  • Lastly, hit the Use Settings as Defaults button.
You want to do the same for option cursor right. Simply repeat the above, changing Key to cursor left, and instead of pressing b after pressing Esc, press f (the resultant string will be \033f).

You can also bind control left/right to begin/end of paragraph. The shell keys for these two actions are Control-A for beginning of paragraph, and Control-E for end of paragraph. Again, simply repeat the steps above, changing the Modifier to control, make sure the Key is set to cursor left, and instead of pressing Esc and b, simply press Control-A (the resultant string will be \001). That's all ... press OK, and you can do the same again with cursor right and Control-E. Remember to hit the Use Settings as Defaults button when you are done!

This is a summary of a more-detailed version of this hint available on the Textmate blog.
    •    
  • Currently 4.29 / 5
  You rated: 5 / 5 (7 votes cast)
 
[25,123 views]  

Modify cursor movement keys in Terminal | 16 comments | Create New Account
Click here to return to the 'Modify cursor movement keys in Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Modify cursor movement keys in Terminal
Authored by: georgeocrawford on Aug 24, '06 08:28:20AM
Great hint. Also, as user "Roland" point out on the Textmate page you linked to:
In this same vein, you can also make the forward delete key stop being worthless and inserting ~ by mapping it to control-option-d.


[ Reply to This | # ]
Making Foward Delete useful
Authored by: simbalala on Aug 25, '06 07:32:29AM

Call me dumb but I can't figure out how to make the text field accept the option key. Option is reserved and the cursor turns into a crosshair when it's pressed.



[ Reply to This | # ]
Making Foward Delete useful
Authored by: PsiPhi on Aug 25, '06 10:49:35AM

Here's how to do it:
1. get the 'Window Settings' window open and select 'Keyboard' as noted
2. click the 'Add' button
3. select 'del (forward delete)' from the 'Key' dropdown
4. select 'none' in the 'Modifier' dropdown
5. 'Action' should be set to 'send string to shell:'
6. now, with the focus on the text field, press control+option+d

That's it, there should now be a string in the field that reads '\004' and the forward delete key should function as intended in the current window and (if you clicked the 'Use settings as default' button) in any NEW terminal windows also.

Does that help?



[ Reply to This | # ]
Nope
Authored by: simbalala on Aug 26, '06 07:21:31AM

As soon as the option key is depressed (in combination with the others) the cursor becomes a crosshair and nothing is entered into the field.

I'm sure it's a simple thing but you don't get a lot of flexibility for experimenting in that window.



[ Reply to This | # ]
OK, it's one of those mysteries
Authored by: simbalala on Aug 26, '06 07:37:56AM

I've got a Powerbook _and_ an Apple Bluetooth keyboard.

When I try from the Bluetooth keyboard Option gives me the crosshair cursor. So I now tried from the PB keyboard and Option does not give me the crosshair but still nothing goes into the text field.

So I tried simply Control-d from the PB keyboard and it works just fine now. Why is Option needed at all?

All is OK now but still mysterious.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: Anonymous on Dec 22, '06 09:28:34AM

The problem with using control-D is that when you're on a blank line, Del suddenly logs you out.

Other solutions have been offered, but the best in this vein that I've worked out is to use the key combination space, control-T, backspace, backspace. That inserts a space, transposes the space and the next character while moving the cursor forward, then two backspaces delete the two. When you're at the end of a line, this doesn't produce a bell sound/flash like control-F, control-H does.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: rhowell on Aug 24, '06 08:40:58AM

Hint of the month!



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: PsiPhi on Aug 24, '06 08:47:01AM

Best. Hint. Ever.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: nedaf7 on Aug 24, '06 10:28:30AM

Anybody know of a way to delete an entire word in terminal, so we can map option-backspace to it? That would be incredibly useful, especially when you are working with directories. Imagine you typed in "cd /Library/WebServer/Documents/", but then realized you want to go to "/Library/WebServer/". Now you have to delete "Documents/" letter by letter, but if we bind option-backspace to delete words, it would only take one keystroke.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: ctierney on Aug 24, '06 11:20:36AM

Control-W should delete the word to the left of the cursor.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: bomolub on Aug 24, '06 11:43:54AM

In my Terminal Inspector, I checked "Use option key as meta key". Then I edited the key mapping for the del key to \004 (type control-d). I added a new key mapping for del using a key modifier of option and set this to \033d (type escape d).

Now the del key deletes forward by character, option-del deletes forward by word. Also backspace (or delete key) deletes backward by character and option-backspace deletes backward by word.



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: davconvent on Sep 29, '06 02:14:22AM

Can you tell me how you modified the option-backspace mapping? backspace doesn't show up in the list of modifiable keys in the small window for adding/editing a key mapping (OS X 10.4.7).



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: i5ao on Aug 24, '06 02:50:18PM

ctrl-w deletes one word to the right of the cursor (forward delete word)
esc-del deletes one word to the left of the cursor (backward delete word)

OMM anyway. I use tcsh with the following bindings in my ~/.tcshrc file (from a previous hint on this site, a long time ago IIRC). see also ''man tsch''

# keys
bindkey "^I" complete-word-fwd
bindkey -k down history-search-forward
bindkey -k up history-search-backward
bindkey ^w delete-word
bindkey "^[[3~" delete-char # fix fwd delete



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: sjk on Aug 24, '06 05:02:43PM
[this is followup to iSao's comment, if you're seeing it non-threaded]

Setting the wordchars variable influences word-oriented tcsh editor commands. I use:

set wordchars = '*?[]~='

And magic-space can be a handy function, meant to be bound to the space character:

bindkey ' ' magic-space


[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: PsiPhi on Aug 24, '06 01:50:12PM

Something I found quite by accident: You can do what you are asking without modifying anything. Just press (and release) the escape key before pressing delete/backspace and it will delete backwards to the nearest word-boundary character (i.e.: space, hyphen, slash, etc...).



[ Reply to This | # ]
Modify cursor movement keys in Terminal
Authored by: PsiPhi on Aug 24, '06 01:52:01PM

Oops, that was supposed to be a reply to the previous comment. Apologies.



[ Reply to This | # ]