Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say I wrote a long command in the Mac OSX terminal, i.e

say "Hello, how are you? I am good thank you. How is it going with you? Fine, thanks"

and now I decided I want to change the word Hello to Hi. To do that, right now I have to keep pressing (or hold down) the left keyboard key until the "cursor" gets to the end of the word Hello, and then delete it. The usual 'holding down option' technique doesn't work as it does in most other OS X applications.

Is there a way to skip a word at a time instead (or any other shorter way of getting the cursor there)?

Thanks!

share|improve this question

2 Answers 2

up vote 14 down vote accepted

Command line editing is a function of your shell, not of Terminal. Probably your shell is bash and probably its command line editing style is set to “emacs”.

Here are a few of the Emacs-style key combinations that you might find handy:

  • C-a: beginning-of-line
  • C-e: end-of-line
  • M-f: forward-word
  • M-b: backward-word
  • C-d: delete-char
  • M-d: kill-word (delete the next ‘word’)
  • M-DEL: backward-kill-word

C-x means Control+x, so C-a is Control+a.

M-x means Meta+x, but there probably is no Meta key on your keyboard. So instead, you can use ESC x (i.e. Escape then x). Terminal has an setting to automatically send ESC before keys pressed with Option held down. Using this feature disables the extended character handling that Mac OS X usually provides when using the Option modifier. So, if you use few extended characters and want to have Option+x send ESC x, then you can enable this Terminal option.

There are lots of ways of moving to “Hello” in your example:

  • Search for “Hello”: C-r H e l l o C-j (or ESC)
    • In normal Emacs, you would just use RET (Return) to end the search at the current location and return to editing. But in bash, the default bindings cause RET (i.e. C-m) to always execute the current line, even if an incremental search is active. So the C-j/ESC part is a deviation from normal Emacs.
  • Jump to the beginning of the line and move forward: C-a M-f C-f (or )
  • Jump to the beginning of the line, then move by words: C-a M-f M-f M-b
  • Use M-b a lot (only really feasible if you map Option to Meta).

There are also several ways of accomplishing your desired replacement:

  • delete the word and replace it: M-d H i
  • delete characters and replace them: C-d C-d C-d C-d C-d H i
  • move past “H” and delete the following work, replace it: C-f M-d i
  • move past “H” and delete the remaining characters, replace them: C-f C-d C-d C-d C-d i

If you stopped at the end of the word (maybe via C-a M-f M-f), you could use M-DEL H i.

You might do something like bind -P | less to find other interesting bindings. Consult the readline section of the bash man page (or the readline parts of the bash info pages) for details.

share|improve this answer
    
Ok, this doesn't really pertain to the question, but how do you put the words that look like keyboard keys in there? –  Wuffers Apr 17 '10 at 22:46
1  
@Mr. Man: Use <kbd> elements. A subset of HTML is allowed in the markup here. The appearance is due to site-local styling of the tag (background, border-color, border-style, border-width, etc.). You can review the source markup of my post (“view source” on this page). See meta.stackexchange.com/questions/5527 even though the fancy style is disabled on MSO: meta.stackexchange.com/questions/1939/#23571 –  Chris Johnsen Apr 18 '10 at 4:42
    
I'm not sure if this is obvious or not, it took me a minute to figure it out, but I'm on a MBP without a meta key and I'm using a default Terminal configuration. To move forward and backward on words I can press Esc and then F, or Esc and then B. Just wanted to mention that because the way it was explained above was making me try and press Esc + x and then f or b... –  cwd Jan 23 '11 at 22:06
    
Is there a way to change the environment or shell so that I can do ctrl or fn + arrow keys to match my IDE? –  codecowboy May 6 '11 at 12:44
    
@codecowboy: It is technically possible (depending on which exact behavior you want), but it would not be trivial. You can configure Terminal to send additional codes for Control-modified keys (but not fn; it usually just replaces one key with another instead of adding a modifier “bit”). Then you would have to extend a terminfo entry and set the TERM environment variable to the new entry to let programs (e.g. your shell) know about the extra codes. Finally, you might need to redefine the bindings in individual programs (e.g. your shell) to trigger some behavior based on your new keys. –  Chris Johnsen May 6 '11 at 17:29

The default readline binding for forward-word is Meta-F (bash(1) man page, READLINE section, Commands for Moving subsection). I'm not sure what Meta translates to in OS X though. Option, perhaps.

share|improve this answer
    
+1 - you're actually right. this works in bash on OSX –  Yuval Jun 2 '10 at 6:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.