Parliament Hill Computers LtdParliament Hill Computers Ltd

Command Line Editing in Bash and Ksh


Bash & ksh History

Bash is great for interactive use, although you will probably want to use ksh for shell scripts (for portability to other Unix systems). You will have bash and ksh available under Linux.

You can configure bash to use emacs or vi editing sequences:

set -o emacs

or

set -o vi

If you are not running ksh or bash (eg it is not your login shell), you can invoke ksh by typing:

ksh -o emacs

The default mode is emacs and is what is explained here. Emacs mode is similar to emacs mode in ksh, a few of the differences are noted below. Only the most commonly used key sequences are listed below.

First some notation that is commonly used to describe key combinations that you will need to type. Using the letter x as an example:

Some actions are also bound to keys with special markings, eg: Up Arrow, Del (or Delete). These are also shown where applicable. Note that these might not work if the terminal emulation is incorrect, ksh does not support them as well as does bash.

I have represented special keys (eg Tab) with the first letter only being capitalised, this makes is clearer when they need to be used in combination (eg TabTab).

Finding a line in history

C-pUpArrowMove to previous line in history
C-nDownArrowMove to next line in history
C-r
Reverse search (differs slightly from ksh)

Moving in the current line

C-aHomeMove to start of the current line
C-eEndMove to end of the current line
C-fRightArrowMove forward (right) one character
C-bLeftArrowMove backward (left) one character
M-f
Move forward one word
M-b
Move backward one word

Editing the current line

C-dDelDelete the character under the cursor. Also is EOF on an empty line
C-hBsDelete the character to the left of the cursor
C-k
Kill the characters to the right of the cursor
C-u
Kill the characters to the left of the cursor (ksh clears line)
M-d
Delete the word to the right of the cursor
M-C-hM-BsDelete the word to the left of the cursor
M-u
Convert the next word to upper case (not ksh)
M-l
Convert the next word to lower case (not ksh)
M-c
Capitalise the first character of the next word (ksh capitalised word)
C-t
Transpose order of the 2 characters before the cursor & move right
C-vx
Insert `x' literally, even if `x' is special (not ksh)
C-xC-e
Edit the current line using program $EDITOR (then run the line)

Kill ring and previous line

C-y   Yank (paste) the last deleted characters
M-y
Immediately following Yank, remove yanked text and replace with previous deleted text (not ksh)
M-.
Paste the last word of the previous line
C-o
Execute the current line, then present the next line in the history

Expansion

M-C-e
Expand the current line as the shell would before execution (not ksh)
C-iTabIf on first word of line, complete the command name (not ksh) If not on first word of line, complete the file name (ksh use M-M-)
C-iC-iTabTabShow possible command/file name completions (ksh use M-= for file names)

You may also find useful

history [-n]Display n lines of history (default n is 20)
C-lRefresh the line that is being typed (bash also clears the screen)
fcFix Command: put the last line into a file, run $EDITOR, then run the line

It is worth working hard today to learn the above and so be lazy tomorrow.

There are more sequences than the above, but these are the most commonly used.

Return to tutorial home.

If you want any help using the above, or have any comments or suggestions, please contact us.