Here is a table that summarizes how to start using Unix command-line editing. Command-line editing can be helpful whenever the number of keystrokes it takes you to edit and re-run a command is less than the number of keystrokes to re-type it. It is very helpful any time you are running commands repeatedly, and can signifcantly reduce the time and effort it takes to complete your Unix projects.
Part 1. Re-Running a command line depends on which shell you are running.
| How to Re-Run commands: | ksh | bash |
| re-run the last command | r | !! |
| re-run by <command number>, say: 90 | r <number> e.g., r 90 | !<number> e.g., !90 |
| re-run by <string>, e.g., grep
finds most recent command line that starts-with <string>. |
r <string> e.g., r grep | !<string> e.g., !grep |
Part 2: Editing a command line depends on
which
style of editing you are using.
(Just a few of the many possible commands for editing a line in "vi"
and "emacs" are listed.)
| Function | 'vi' style | 'emacs' style |
| Default for this shell | ksh | bash |
| "set" command to switch to it | set -o vi | set -o emacs |
| Finding a command line to Edit: | ||
| enter command line edit mode | <esc> | always on because we use control characters or escape sequences |
| go back a line | k | ^P |
| go forward a line | n | ^N |
| search back for a <string>, e.g.,
grep
finds most recent command line that contains <string>. If repeated, finds second most recent occurence. |
/<string> e.g., /grep | ^R<string> e.g., ^Rgrep |
| Positioning the cursor on the current command line: | ||
| start of line | ^ (shift-6) | ^A |
| end of line | $ | ^E |
| forward (right) one: character | <space> | ^F |
| word | w | <esc>f |
| back (left) one: character | h | ^B |
| word | b | <esc>b |
| Deleting Text: delete character under cursor | x | ^D |
| delete from cursor to end of word | dw | <esc>d |
| to end of line | d$ | ^K |
| Adding Text: insert before cursor | i (press <esc> to end) | just type |
| Executing the edited command line: | <enter> (from anywhere on line) | <enter> (from anywhere on line) |