Quickly familiarizing when coming from bash
Yes, an article on ksh shouldn't be about bash. My reality though reflets that 99% of the time if I'm talking about ksh it's because of someone who is familiar with bash asking a question, so here's the bare-minimum they're looking for (note: this presumes the default 'emacs' mode):
^p = previous command. Similar to hitting up-arrow in bash. Repeat to go further.
^n = next command. Similar to hitting down-arrow in bash. Repeat to go further.
^r = reverse-search history. Similar to ^r in bash.
fc = fix-command, run with no arguments, will open previous command in $FCEDIT (or ed if undefined)
fc, fix command
fc is "fix-command" and allows you to modify your previous commands. It allows you to modify a previous command in an editor (ed by default). A brief example of fixing a previous command:
$ echo "Hello Woorld"
$ fc -l # l, "list" commands you can fix
1 echo "testing"
2 echo "Hello Woorld"
# For example purposes:
$ fc 2 # This will use $FCEDIT to edit the command. When $FCEDIT exists, the buffer will be run.
20 # ed showing how many bytes the buffer is
p # user input for ed to print the line
echo "Hello Woorld"
s/Woo/Wo # using substitution to fix the mistyped "oo" to just "o"
echo "Hello World" # ed automatically prints the line for you again
wq # write/quit - the command will now run again.
$ echo "Hello World"
Don't want to use ed (default)? Set FCEDIT to your editor of choice and it will be used instead - add to your .profile or .kshrc:
export FCEDIT=$(whence vim)
Setting multi-session history: if $HISTFILE is defined, it will be used. You can keep this per-session by using something like: HISTFILE=$HOME/.kshhist.$$ or cross-sessions using HISTFILE=$HOME/.ksh_history. This would normally be noted in ~/.profile
My basic setup
# ~/.kshrc
export PS1="\u@\h [j\j:h:\!:c\#]\W\$ "
export HISTFILE=$HOME/.ksh_history
export VISUAL=$(whence vim)
export EDITOR=$(whence vim)
export FCEDIT=$(whence vim)
set -o mode viraw
alias history="fc -l"
What I used to write this
$ echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2
OpenBSD maleah.dayid.org 6.9 GENERIC.MP#715 i386