Bash Command Line Editing

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.


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


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.


Learning the bash Shell

Post a Comment


Java (160) Lucene-Solr (112) Interview (64) All (58) J2SE (53) Algorithm (45) Soft Skills (39) Eclipse (33) Code Example (31) JavaScript (23) Linux (22) Spring (22) Windows (22) Tools (21) Web Development (20) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) Troubleshooting (14) J2EE (13) Network (13) PowerShell (11) Problem Solving (10) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) Tips (8) bat (8) blogger (8) Big Data (7) Database (7) Google (7) Guava (7) JSON (7) Shell (7) System Design (7) ANT (6) Coding Skills (6) Lesson Learned (6) Programmer Skills (6) Scala (6) css (6) Algorithm Series (5) Cache (5) Continuous Integration (5) IDE (5) adsense (5) xml (5) AIX (4) Become a Better You (4) Code Quality (4) Concurrency (4) Dynamic Languages (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Review (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Distributed (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Life (3) Python (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Fiddler (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Logging (2) Mac (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Firefox (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) Invest (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts