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.

I'll often find myself writing out long commands in the bash shell - things with many arguments, web addresses, routes, etc and so on. Every so often I will realize I forgot to, say, put my "bundle exec" at the beginning of the command, or misspelled something, or forgot quotes. Or something as simply as putting "cd" instead of "vim".

Thus begins the tedious process of holding the left arrow key until I get back to the beginning of the command.

Is there an any way to jump to the beginning of the line again?

share|improve this question
2  
You might want to get rid of the default terminal app, and download iTerm –  Clement Herreman Oct 25 '11 at 15:44
    
Why? I just checked, and it seems to behave pretty much the same. (The home key works, now, but since I have the other keys that doesn't really matter). What does it offer that would make it worth redoing all my settings and configurations and tasks and whatnot? –  GlyphGryph Oct 25 '11 at 15:49
    
This seems like something you can setup with .inputrc –  Rob Oct 25 '11 at 16:42
    
Also, to jump one word at a time, use ⌥B and ⌥F to jump back and forth, respectively. –  fideli Nov 1 '11 at 1:08
    
@fideli, but only if you’ve enabled “Use Option as meta key”. Note that as of Mac OS X Lion 10.7, Terminal’s default keyboard settings map Option-Left Arrow/Right Arrow to Esc-b/f so there’s no configuration needed. –  Chris Page Nov 3 '11 at 3:40

7 Answers 7

up vote 12 down vote accepted

In addition to ^A and ^E, you can do

  • Esc-b to jump back one word
  • Esc-f to jump forward one word
  • Ctrl-b to move back one character
  • Ctrl-f to move forward one character

See the bash manual for commands for moving.

share|improve this answer
    
A thing to note -- this is from Emacs, and by adding the emacs option to your shellopts you can get some more of that. –  TC1 Oct 25 '11 at 16:09

You can use Emacs commands, e.g.:

  • ctrl-A to go to the beginning of the command line
  • ctrl-E to go to the end of the command line

This is bash, not specific to Mac.

share|improve this answer
set -o vi

Then Esc followed by

^ start of line $ end of line b one word backword w one word forward

share|improve this answer
    
I'm a pretty big fan of Vim, so the principle of your answer is great by me, but modal command line editing might be a bit confusing to the OP, especially without any mention of how to get back to being able to type! Return to insert mode using i (insert) to type before the cursor or a (append) to type after the cursor. –  Jefromi Oct 25 '11 at 18:31
    
I've been using vi as my primary text editor for decades, but I still prefer the emacs key bindings for my shell. –  Keith Thompson Oct 26 '11 at 22:23
    
Actually, the OP uses vim for all text editing, so knowing you can set vi standards is amazingly useful. Much thanks! –  GlyphGryph Oct 27 '11 at 13:36
    
@Kieth Thompson: I used to think the same. I just thought shell doesn't mix well with this mode switching stuff (like the irritating v key in vi mode). But after a while I just got vim infected so I suggest you give it a try :P –  kizzx2 Oct 27 '11 at 16:44

This doesn't answer your question but it may solve your problem in some cases.

In bash the !! token is substituted with your previous command. So if you forget to add something to the beginning of a command (like sudo), you can do something like this

# ./super_secret_command --with-args
=> ACCESS DENIED

# sudo !!
sudo ./super_secret_command --with-args
=> ACCESS GRANTED
share|improve this answer

You can configure the regular Ctrl/Opt+Left/Right shortcuts for use in Terminal. That way, you don't have to relearn everything when you're using Terminal.

Open Terminal » Preferences… » Settings » (select a profile) » Keyboard.

There, assign the following shortcuts:

  • Control + Cursor Left: Send string \033[1~ to shell
  • Control + Cursor Right: Send string \033[4~ to shell
  • Option + Cursor Left: Send string \033[5D to shell
  • Option + Cursor Right: Send string \033[5C to shell

I also like the following (Fn + Left/Right):

  • Home: Send string \033[1~ to shell
  • End: Send string \033[4~ to shell

enter image description here

share|improve this answer
    
On Lion ⌥← and ⌥→ are bound to M-b and M-f by default. And ⌃← and ⌃→ are the default shortcuts for changing spaces. –  Lri Oct 26 '11 at 1:28

I needed this in Cygwin, not a Mac, but adding the following to ~/.inputrc may help too:

"\e[1;5C": forward-word   # ctrl + right
"\e[1;5D": backward-word  # ctrl + left 
share|improve this answer

I'm not a Mac user but what about the Home key? That works fine on a Linux terminal.

share|improve this answer
1  
Nope, that scrolls to the top of the terminal window. –  GlyphGryph Oct 25 '11 at 14:27
    
By default, Home and End scroll the view, which is the standard Mac behavior. By default Shift-Home and Shift-End send “ESC [ H” and “ESC [ F”, respectively. These are all customizable in Terminal > Preferences > Settings > [profile] > Keyboard. –  Chris Page Nov 3 '11 at 3:45

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.