When working from the command line, you will at some point enter a command multiple times, possibly with minor modifications. In those cases the ability to recall and modify previous commands quickly and efficiently is beneficial. In the old DOS days, a utility called doskey would keep your history in a buffer. You could go back or forth through the command history one command at a time by using the up and down arrow keys. Individual commands could be edited by using the cursor and Delete or Backspace keys to make replacements. This basic form of recall and modification of recently typed commands is also available in bash
.
Modern shell versions (ksh
and bash)
also provide more-advanced command-line capabilities. You can set your shell session to act as though you were in a vi
editing session. The vi
editor has two modes of operation, insert and command. When typing commands at the prompt, you are working in the shell's equivalent of vi
's insert mode. You type a command, use the Backspace key to fix any typing mistakes, and then press Enter to input the command.
When using the shell's vi
editing mode, you can press Escape to change to command mode. This allows you to move around on the command line as in vi:
you use the H key to move left, the L key to move right, the B key to move one word left, the W key to move one word right, and so on. Once you have positioned the cursor where the modification is to be made, you can press the I key to go back into insert mode and start typing. Many vi
commands besides cursor motion are available, such as using the X key to delete a character, pressing D+W to delete a word, pressing Shift+A to append text to the end of the line, and many others that, once you are familiar with the vi
editor, will be second nature. In case you're not familiar with vi
, there are many online references and tutorials.
In addition to the editing commands, command mode gives you the ability to move through your command history. If you press the Escape key at the command prompt to change to command mode, the J and K keys will move you down and up through the command history list.
Think of your command history as a list in a simple flat file, where the earliest commands in your history are at the top of the list and the most recent ones are at the bottom. When you enter command mode you start at the bottom of your history and press the K key to move back up through the list. Pressing the J key moves you forward (down) through the history list. Once you have found the desired command, you can make any appropriate modifications and press Enter to run the command.
One last command-history function that has been extremely valuable to me is the ability to search for previous commands that match a pattern. You do this search in the same way as a search in a vi
session. First you press Escape to change to command mode and then you press the forward slash (/
). This gives you an opportunity to type in a substring to search for within the commands in the history stack. If the first match displayed isn't the correct one, you can press N to see further matches, as you would in vi
, until you find the command you're looking for.
I have been using this method to work at the prompt for a long time, and my fingers are accustomed to moving to the correct keys. This has improved my speed and agility immensely when working at the command line. If you're not comfortable with the vi
editor, I recommend practicing; vi
is the default text editor on UNIX/Linux systems, and you'll certainly have to use it at some point. Although learning the vi
editor is not the easiest thing to do, once you are comfortable with it you will be amazed at how powerful it is.
There is also an emacs
editing mode available. I recommend the vi
mode if you have no previous experience, mainly because it is a more standard tool and is available on all systems. If you're a fan of emacs
and comfortable with that editor, then by all means use the emacs
mode. The goal here is to become quicker and more efficient.
You can configure vi
editing for either ksh
or bash
systemwide or for individual users. You can also configure it right at the command prompt if you like.
With bash
there are a few ways to enable the vi
editing mode. The first is to modify the inputrc
file. There is a systemwide /etc/inputrc
file that all users use who have bash
set as their shell. There is also an individual .bashrc
file that is usually kept in the user's home directory. Adding the following line to one of those two files will enable vi
mode:
set editing-mode vi
Another way to enable vi
mode in bash
is to modify either the systemwide /etc/profile
or the .bash_profile
in an individual user's home directory to include the following line:
set -o vi
In ksh
configuring vi
editing mode can once again be set either systemwide or for an individual user. Modifying the systemwide /etc/profile
file enables this editing mode for all users, and modifying the .profile
file in a user's home directory enables it at an individual level. The line to add to either of these two files is the same as for bash
:
set -o vi
One additional option, available in both shells, that increases your efficiency at the command line is command and file completion. With this feature, you can start to type a path or command, and once you've typed a sufficient number of characters for the path or command to be uniquely determined, pressing a special key or key sequence will complete the command. For bash
you simply press the Tab key. In ksh
, you press either the Tab key or E
scape and then a backslash ()—whichever is your preference. The Escape+sequence works only when
vi
editing mode is set.
Command completion can be explained with a simple example. My shell on the system I'm working on is bash
and my home directory is /home/rbpeters
. If I wanted to cd
to that directory using command completion, I could type the following key sequence:
cd /h<TAB>rb<TAB><ENTER>
The first TAB
would complete the /h
to make it /home/
because no other subdirectories of the root directory begin with h. The second TAB
would complete the /home/rb
entry to /home/rbpeters
. Pressing Enter then executes the command.
52.15.147.20