Using history and command line editing

Unix Insider |   1 comment

One
of the great features of the C shell and the Korn shell is history.
The ability to recall and reuse commands that you have entered earlier
can save many keystrokes of repetitive typing. History and command line
editing are closely related. You may use command line editing to
edit the command that you are currently working on after you have typed
it in, or you may recall a previous command and then edit it.

In this article, I will show you some of the ins and outs of
history and command line editing in the Korn shell. For history to
work, you need to be in the Korn shell, so begin by typing:


ksh

to launch a Korn shell.

The second thing you need to make history work is an easy way to
access it. History is more than just the ability to recall
previous commands; it lets you recall commands and edit them before they are
executed. The Korn shell allows you to use different editing styles
depending on what you're trying to do. The primary
editors are vi, emacs, and gmacs. I recommend vi, as most people are
familiar with it. History and command line editing behave very
differently depending on whether you are using vi, emacs, or gmacs. I
will cover some of these differences.

To set vi up as your history editor type:


set -o vi

If you are going to continue to use history, you will want to include
this in your profile, or the emacs/gmacs equivalents. These set -o
commands only work in the Korn shell:


set -o emacs
or
set -o gmacs

Assuming that you have set -o vi as your history editor,
you may now access history. Press the escape key once (esc) and
release it and press k. The last command that you typed will
appear on the screen:


$ set -o vi

Press enter and that command will be executed. Try a few more
commands so that you can accumulate some history. End with ls
-l
on the current directory. Now press esc
and k. The ls -l command appears once more. Press k again and
the command just prior to ls -l will appear. Each time you press k
you'll go back one more command in your history. The minus key ( - )
works in exactly the same way. I use the minus key because it's easier for me
to remember. Eventually, if you press k or minus enough times, you'll
end up back at the set -o vi command.

Now press j or the plus key < + > and you will move forward in the command history.
(When you are rapidly running back through history looking for the command you need, and you realize you've passed it, it's good to know how to move forward again.)

If you have set -o emacs or gmacs, the keys are
control-p for previous command and control-n for next command. In vi
you hit esc once and then k or n will move you backward or
forward through the commands. For emacs and gmacs, use control-p or
control-n each time.

To view more of your history, type "history", and a list of your most
recent commands will be displayed along with numbers. If you see the number of a
command that you need, recall it by hitting the esc key, typing the number of the command as displayed by history, and then typing an upper case G. This will place the command with that number on the command line.

Once you have located the command that you want, you can press enter to
execute the command, but you can also edit the command before executing it.

Moving around in vi

There are several vi commands for moving around the command line and
editing the line. I will only cover a few here. For further information
consult the manual on ksh. If you are familiar with vi you will
recognize the commands. The vi editor operates in two main modes. In
command mode single keys move the cursor, delete characters and perform
other actions. In input mode single keys are actually typed into the
command line.

Command Editing -- Cursor Movement in command mode for vi

1 comment

    Anonymous 1 year ago
    That is a good trick! Thanks for sharing it.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Ask a question

      Ask a Question