Is there a way to jump to an earlier part of a command line command in the Mac Terminal?

9

3

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?

GlyphGryph

Posted 2011-10-25T13:42:49.983

Reputation: 466

2You might want to get rid of the default terminal app, and download iTerm – Clement Herreman – 2011-10-25T15:44:48.333

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 – 2011-10-25T15:49:57.073

This seems like something you can setup with .inputrc – Rob – 2011-10-25T16:42:41.973

Also, to jump one word at a time, use ⌥B and ⌥F to jump back and forth, respectively. – fideli – 2011-11-01T01:08:35.307

@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 – 2011-11-03T03:40:46.547

Answers

12

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.

glenn jackman

Posted 2011-10-25T13:42:49.983

Reputation: 18 546

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 – 2011-10-25T16:09:08.257

6

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.

mouviciel

Posted 2011-10-25T13:42:49.983

Reputation: 2 858

3

set -o vi

Then Esc followed by

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

kizzx2

Posted 2011-10-25T13:42:49.983

Reputation: 889

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. – Cascabel – 2011-10-25T18:31:25.803

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 – 2011-10-26T22:23:45.607

Actually, the OP uses vim for all text editing, so knowing you can set vi standards is amazingly useful. Much thanks! – GlyphGryph – 2011-10-27T13:36:36.413

@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 – 2011-10-27T16:44:28.483

1

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

Sam

Posted 2011-10-25T13:42:49.983

Reputation: 121

0

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

m0skit0

Posted 2011-10-25T13:42:49.983

Reputation: 1 317

1Nope, that scrolls to the top of the terminal window. – GlyphGryph – 2011-10-25T14:27:00.940

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 – 2011-11-03T03:45:04.843

0

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

Daniel Beck

Posted 2011-10-25T13:42:49.983

Reputation: 98 421

On Lion ⌥← and ⌥→ are bound to M-b and M-f by default. And ⌃← and ⌃→ are the default shortcuts for changing spaces. – Lri – 2011-10-26T01:28:05.440

0

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 

Tom

Posted 2011-10-25T13:42:49.983

Reputation: 3 056

0

Best way to move around the command line in Mac Os X is to use my profile: https://github.com/lingtalfi/mac-terminal-shortcuts

It provides the following (intuitive) shortcuts:

ALT-left: move one word backward
ALT-right: move one word forward
CTRL-left: move to the beginning of the line
CTRL-right: move to the end of the line
ALT-backspace: kill one word backward
ALT-del: kill one word forward
ALT-up: set word after cursor to uppercase
ALT-down: set word after cursor to lowercase
home: move to the beginning of the line
end: move to the end of the line    
CTRL-backspace: Same as ALT-backspace
CTRL-del: Same as ALT-del

ling

Posted 2011-10-25T13:42:49.983

Reputation: 163