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

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


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.


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.


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 $.