Wiki2
ksh

KSH tips and tricks

Various Shell Prompt Tips

C-U - Clear the prompt line

Add prompt editing using VISUAL. TBD.

Customization of shell prompt. Add the following to .profile.

Make shell prompt more informative

HOST=`hostname`
export PS1='${USER}@${HOST%%.*} ${PWD##*/} # '

push and pop directories

pushd and popd is really useful when working in the command prompt. pushd <dir> push current directory on a directory stack and change currend directory to <dir>. popd change current directory to the laste pushed directory on stack.

cd /home/peter
pushd /root
popd

pushd and popd is part of csh but not in ksh.

In ksh you may use cd - and cd -- to go back 1 and 2 folders in history. This also works in csh.

cd /home/peter
cd /root
cd -

Command substitution

Command subtitution is performed by $( ) or ` ` notation. The first is recommended for compatibility reasons.

One thing to note abount ksh is that it removes all trailing newline characters when performing command substitution. See following example.

# echo $(ls)
file1 file2 file3

To preserve newline characters you can the substituted command in " ".

# echo "$(ls)"
file1
file2
file3

See Command substitution in the Korn shell or POSIX shell for more information.

Editing mode

ksh can (as other shells) work in either emacs (default) or vi mode. Some commonly used commands for emacs mode are.

CTRL-A	 Move to beginning of line
CTRL-E   Move to end of line
CTRL-K	 Delete ("kill") forward to end of line
ESC-B    Move one word back (ALT-RIGHT also works on Mac)    
ESC-F    Move one word forward (ALT-LEFT alsto works on Mac)

See Emacs Edit Mode.

If you want to switch to vi editing mode simply do.

set -o vi
set -o emacs    # go back to emacs mode

Some useful vi editing commands

ESC  escape to command mode
i    enter insert mode (initial mode)
w    Move one work forward (command mode)
b    Move one work backwards (command mode)
cw   Remove current word and enter insert mode (command mode) 

Command history (in ksh)

ksh does support command history just as e.g. bash does. Command history is stored by default in $HOME/.sh_history. But a custom history file can be used by setting HISTFILE environment variable. History can be shown using fc (in ksh) and/or history (in csh). In ksh history is usually aliased to fc -l. Default fc editor is ed but can be changed with FCEDIT environment variable.

fc -l           # or history
fc -e - ls      # call last ls command