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


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
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
Move forward one word
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
Kill the characters to the right of the cursor
Kill the characters to the left of the cursor (ksh clears line)
Delete the word to the right of the cursor
M-C-hM-BsDelete the word to the left of the cursor
Convert the next word to upper case (not ksh)
Convert the next word to lower case (not ksh)
Capitalise the first character of the next word (ksh capitalised word)
Transpose order of the 2 characters before the cursor & move right
Insert `x' literally, even if `x' is special (not ksh)
Edit the current line using program $EDITOR (then run the line)

Kill ring and previous line

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


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.