Bash Command Line Editing
Enabling Command-Line Editing
set -o emacs or set -o vi
emacs-mode is more of a natural extension of the minimal editing capability you get with the bare shell.
The History List .bash_history
You can call this file whatever you like by setting the environment variable HISTFILE.
emacs Editing Mode
Basic Commands
CTRL-B Move backward one character (without deleting)
CTRL-F Move forward one character
DEL Delete one character backward
CTRL-D Delete one character forward
Word Commands
ESC-B Move one word backward
ESC-F Move one word forward
ESC-DEL Kill one word backward
ESC-CTRL-H Kill one word backward
ESC-D Kill one word forward
CTRL-Y Retrieve ("yank") last item killed
Line Commands
CTRL-A Move to beginning of line
CTRL-E Move to end of line
CTRL-K Kill forward to end of line
Moving Around in the History List
CTRL-P Move to previous line
CTRL-N Move to next line
CTRL-R Search backward
ESC-< Move to first line of history list
ESC-> Move to last line of history list
Textual Completion
TAB Attempt to perform general completion of the text
ESC- List the possible completions
ESC-/ Attempt filename completion
CTRL-X / List the possible filename completions
ESC-~ Attempt username completion
CTRL-X ~ List the possible username completions
ESC-$ Attempt variable completion
CTRL-X $ List the possible variable completions
ESC-@ Attempt hostname completion
CTRL-X @ List the possible hostname completions
ESC-! Attempt command completion
CTRL-X ! List the possible command completions
ESC-TAB Attempt completion from previous commands in the history list
Miscellaneous Commands
CTRL-L Clears the screen, placing the current line at the top of the screen
CTRL-O Same as RETURN, then display next line in command history
CTRL-O is useful for repeating a sequence of commands you have already entered.
CTRL-T Transpose two characters on either side of point and move point forward by one
CTRL-U Kills the line from the beginning to point
CTRL-V Quoted insert
CTRL-V will cause the next character you type to appear in the command line as is.
ESC-C Capitalize word after point
ESC-U Change word after point to all capital letters
ESC-L Change word after point to all lowercase letters
ESC-. Insert last word in previous command line after point
ESC-_ Same as ESC-.
ESC-. and ESC-_ are useful if you want to run several commands on a given file.
vi Editing Mode
Like vi, vi-mode has two modes of its own: input and control mode
Editing commands in vi input mode
DEL Delete previous character
CTRL-W Erase previous word (i.e., erase until a blank)
CTRL-V Quote the next character
ESC Enter control mode (see below)
Simple Control Mode Commands
h Move left one character
l Move right one character
w Move right one word
b Move left one word
W Move to beginning of next non-blank word
B Move to beginning of preceding non-blank word
e Move to end of current word
E Move to end of current non-blank word
0 Move to beginning of line
^ Move to first non-blank character in line
$ Move to end of line
Entering and Changing Text
i Text inserted before current character (insert)
a Text inserted after current character (append)
I Text inserted at beginning of line
A Text inserted at end of line
R Text overwrites existing text
Deletion Commands
dh Delete one character backwards
dl Delete one character forwards
db Delete one word backwards
dw Delete one word forwards
dB Delete one non-blank word backwards
dW Delete one non-blank word forwards
d$ Delete to end of line
d0 Delete to beginning of line
Abbreviations for vi-mode delete commands
D Equivalent to d$ (delete to end of line)
dd Equivalent to 0d$ (delete entire line)
C Equivalent to c$ (delete to end of line, enter input mode)
cc Equivalent to 0c$ (delete entire line, enter input mode)
X Equivalent to dl (delete character backwards)
x Equivalent to dh (delete character forwards)
Moving Around in the History List
k or - Move backward one line
j or + Move forward one line
G Move to line given by repeat count
/string Search backward for string
?string Search forward for string
n Repeat search in same direction as previous
N Repeat search in opposite direction of previous
Character-Finding Commands
fx Move right to next occurrence of x
Fx Move left to previous occurrence of x
tx Move right to next occurrence of x, then back one space
Tx Move left to previous occurrence of x, then forward one space
; Redo last character-finding command
, Redo last character-finding command in opposite direction
Miscellaneous vi-mode commands
~ Invert (twiddle) case of current character(s)
- Append last word of previous command, enter input mode
CTRL-L Clear the screen and redraw the current line on it; good for when your screen becomes garbled.
# Prepend # (comment character) to the line and send it to the history list; useful for saving a command to be executed later without having to retype it
The fc Command
Purpose Processes the command history list.
Syntax
To Open an Editor to Modify and Reexecute Previously Entered Commands
fc [ -r ] [ -e Editor ] [ First [ Last ] ]
To Generate a Listing of Previously Entered Commands
fc -l [ -n ] [ -r ] [ First [ Last ] ]
To Generate a Listing of Previously Entered Commands with Time of Execution
fc -t [ -n ] [ -r ] [ First [ Last ] ]
To Re-execute a Previously Entered Command
fc -s [ Old= New ] [ First ]
fc -l treats arguments in a rather complex way:
If you give two arguments, they serve as the first and last commands to be shown.
If you specify one number argument, only the command with that number is shown.
With a single string argument, it searches for the most recent command starting with that string and shows you everything from that command to the most recent command.
If you specify no arguments, you will see the last 16 commands you entered. bash also has a built-in command for displaying the history: history.
-n, suppresses the line numbers
fc -l 2 4; fc -l 3; fc -l v; fc -l m w
History Expansion
Event designators
! Start a history substitution
!! Refers to the last command
!n Refers to command line n
!-n Refers to the current command line minus n
!string Refers to the most recent command starting with string
!?string? Refers to the most recent command containing string; the ending is optional
^string1^string2 Repeat the last command, replacing string1 with string2
Word designators
Designator Description
0 The zeroth (first) word in a line
n The nth word in a line
^ The first argument (the second word)
$ The last argument in a line
% The word matched by the most recent ?string search
x-y A range of words from x to y. -y is synonymous with 0-y
* All words but the zeroth (first); synonymous with 1-$.; if there is only one word
on the line, an empty string is returned
x* Synonymous with x-$
x- The words from x to the second to last word
Modifiers
Modifier Description
h Removes a trailing pathname component, leaving the head
r Removes a trailing suffix of the form .xxx
e Removes all but the trailing suffix
t Removes all leading pathname components, leaving the tail
p Prints the resulting command but doesn't execute it
q Quotes the substituted words, escaping further substitutions
x Quotes the substituted words, breaking them into words at blanks and newlines
s/old/new/ Substitutes new for old
Emacs Mpde Keyboard Habits
For cursor motion around a command line, stick to CTRL-A and CTRL-E for beginning
and end of line, and CTRL-F and CTRL-B for moving around.
Delete using DEL (or whatever your "erase" key is) and CTRL-D
Use CTRL-P and CTRL-N (or the up and down arrow keys) to move through the
command history.
Use CTRL-R to search for a command you need to run again.
Use TAB for filename completion.
Resources: