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
.
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 typesudo !!
. The!!
gets replaced with the previous command. – Matthew Crumley – 2014-05-30T17:11:23.893