How to jump back to the first character in *nix command line?

17

4

When writing a long command in the *nix command line and having to go back to the first character, in order to add something at the beginning (for instance a nohup, when you realize the process will be a long one, or a sudo, when you realize you need root permissions) it can take a long time for the cursor to make its way back to the first character...

Is there a short cut that allows you to jump straight there?

I'm using a mac, so Home is not an option

clami219

Posted 2014-05-30T13:50:33.320

Reputation: 375

2If you're on an Apple laptop, Fn + Left/Right are equivalent to Home/End. (And relatedly, Fn + Up/Down are PageUp/Down.) – duskwuff -inactive- – 2014-05-30T15:16:29.107

2To those reading this question, this is in effect asking about the shell, more specifically command line editing in the shell. The Ctrl-A answer before is bash (most likely) with Emacs style editing, the default. – Rich Homolka – 2014-05-30T15:23:17.090

4An alternative if you just need to add sudo is to type sudo !!. The !! gets replaced with the previous command. – Matthew Crumley – 2014-05-30T17:11:23.893

Answers

29

Ctrl+A should work. Ctrl+E corresponds to the end of the command line.

choroba

Posted 2014-05-30T13:50:33.320

Reputation: 14 741

Thank you, it works on Linux as well. – Gideon Babu – 2019-11-21T15:38:39.727

1Great! This works perfectly also on mac! – clami219 – 2014-05-30T13:57:52.087

1This also applies in many code editors. – Jwosty – 2014-05-31T01:35:21.033

2@clami219: It actually works in most text contexts in OSX. Emacs bindings is a wonderful thing... – tommyo – 2014-06-01T02:19:42.450

10

This depends on the shell in question.

Some shells (like AT&T ksh88) offer virtually no input line editing.

In shells implementing vi mode editing (you may have to enable this with set -o vi), this is done by pressing Esc (to switch from insert mode) followed by 0 to jump to the beginning of line or $ to jump to the end of line. Then re-enter insert mode either by pressing i – the cursor will stay where it is – or a – the cursor will move one to the right to append text.

vi mode editing has recently been mandated by the POSIX standard.

The much more common emacs mode editing (thank gods, it has nothing to do with the Emacs editor-slash-operating-system) uses Ctrl-A to jump to the beginning of the line and Ctrl-E to jump to the end of the line. This mode requires you to run set -o emacs on many shells (most prominently AT&T ksh93) but is enabled by default in mksh and GNU bash.

Most modern shells support both emacs and vi modes. (Both these modes require a tty to work.)

In many shells, you can customise keybindings; usually for the emacs mode, although some shells also permit customising the keybinding for the vi mode. If you have a key you'd rather have this bound to, you first need to figure out the key sequences it produces (for example, on my system, Alt-CursorLeft produces Esc+[+1+;+3+D (^[[1;3D; ^X is Ctrl-X and ^[ is Esc), so I can type something like

bind '^[[1;3D=beginning-of-line'
bind '"\e[1;3D":beginning-of-line'

and will have this keybinding changed, depending on the shell. You can usually persist them in either the startup file (~/.mkshrc, ~/.kshrc) or, for GNU bash, in ~/.inputrc. Note that not all shells support bindind all keys in all versions.

You can usually find out what chars a key generates by just running cat on the shell, typing the key and watching. Then press ^C (Ctrl-C) to abort cat.

mirabilos

Posted 2014-05-30T13:50:33.320

Reputation: 284

I like to use ^D, the Unix end-line character, to end cat. In my mind, it's ^D because the text stream is "done". – Aaron Hall – 2014-05-30T22:03:03.077

@AaronHall: that will not work if there's already something on the line. Plus, we want to abort it, not end it ;-) But yes, in the normal case you're right. But this is a special case. – mirabilos – 2014-05-30T22:06:11.570

The terminal may do line editing as well. – Samuel Edwin Ward – 2014-05-31T05:55:19.080

@mirabilos To send an EOF without a newline, simply do ^D^D (i.e. press D twice). – Potatoswatter – 2014-05-31T14:38:53.960

@Potatoswatter yes (fun, I didn't know that myself), but I specifically want to abort here. When sending ^D^D I still get “the junk” echoed… – mirabilos – 2014-05-31T18:55:32.137

2

Have you tried Home key? I use MobXterm to connect to linux box and use both Home and End key on the keyboard to go to first or last char of the command.

PAS

Posted 2014-05-30T13:50:33.320

Reputation: 253

You are right! I had to specify that I'm using a mac... – clami219 – 2014-05-30T13:57:00.630

2

If you've switched to vi mode, using set -o vi, you can hit Esc and either I or A to enter text at the start or the end of the line. Or if you're just looking to move the cursor, it's ^ and $.

TankorSmash

Posted 2014-05-30T13:50:33.320

Reputation: 281

vi mode of what shell? – David Richerby – 2014-05-31T09:15:35.413

2

In emacs mode (switched by: set -o emacs) it's by pressing:

  • Ctrl+A and Ctrl+E
  • Home and End
  • Esc+b and Esc+f (moves the cursor backward and forward of the current word )

In vi mode (switched by: set -o vi) when in command mode (Esc) it's by pressing:

  • 0/^ and $ (it'll stay in command mode)
  • I and A (it'll switch to edit mode)

See also:

kenorb

Posted 2014-05-30T13:50:33.320

Reputation: 16 795