15

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

  • 2
    If you're on an Apple laptop, Fn + Left/Right are equivalent to Home/End. (And relatedly, Fn + Up/Down are PageUp/Down.) – duskwuff May 30 '14 at 15:16
  • 2
    To 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 May 30 '14 at 15:23
  • 4
    An alternative if you just need to add sudo is to type sudo !!. The !! gets replaced with the previous command. – Matthew Crumley May 30 '14 at 17:11
26

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

  • 1
    Great! This works perfectly also on mac! – clami219 May 30 '14 at 13:57
  • 1
    This also applies in many code editors. – Jwosty May 31 '14 at 1:35
  • 2
    @clami219: It actually works in most text contexts in OSX. Emacs bindings is a wonderful thing... – tommyo Jun 1 '14 at 2:19
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.

  • 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 May 30 '14 at 22:03
  • @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 May 30 '14 at 22:06
  • The terminal may do line editing as well. – Samuel Edwin Ward May 31 '14 at 5:55
  • @mirabilos To send an EOF without a newline, simply do ^D^D (i.e. press D twice). – Potatoswatter May 31 '14 at 14:38
  • @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 May 31 '14 at 18:55
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 $.

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:

1

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.

  • You are right! I had to specify that I'm using a mac... – clami219 May 30 '14 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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