There are several layers in which you can ask for the last command.
Line editor
The line editor (where you type your commands) is provided directly by ksh, by the library readline in bash, and by the library zle in zsh. Other shells, like dash may not have an editing library to understand this keys (unless compiled with the --with-libedit
option enabled) .
This work in ksh, bash, zsh:
Ctrl-P # Emacs mode (previous)
Ctrl-N # Emacs mode (next)
ESC-k # vi mode ESC to go to command mode and k for up
ESC-j # vi mode ESC to go to command mode and j for down
history
The history system provided by many shells allow the expansion of some shortcuts:
!! # bash and zsh, may be provided by `alias \!\!='fc -e -'` in ksh.
!-1 # bash and zsh only
commands
There are some commands which are able to retrieve history commands.
Like the quite verbose command (only to print):
history 2 | head -n-1
or the older and therefore usually more portable fc
command (directly re-executes):
fc -e - -1
Or maybe the simpler fc -s -1
, but zsh doesn't accept such option for fc.
If you want to only print the command, use: fc -nl -1 -1
.
To edit, set FCEDIT
to a text editor (ex, ed, emacs, vi, etc.) and use fc directly: fc -1
.