Skip to main content

Access old commands from history with just few keystrokes

One of the key feature of Unix shells like C shell , Bash 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.

Launch any of the above mentioned shell on the command line by typing csh, bash or ksh. Now let start accessing old commands using history.

History let us recall commands and edit them before they are executed. Shell allows us to use different editing styles depending on what we are 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 we are using vi, emacs, or gmacs.

To set vi up as history editor type:
$set -o vi
or
$set -o emacs
or
$set -o gmacs

After setting “set -o vi" as our history editor, we can now access history:
  1. Press the escape key once (esc).
  2. Then press k to access last command that you typed will appear on the screen.
  3. Edit it if required as per the requirement and press enter to execute it.
Each time you press k you’ll go back one more command in your history. The minus key ( – ) works in exactly the same way. Press j or the plus key < + > to 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.

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, type the number of the command as displayed by history, and then type an upper case G. This will place the command with that number on the command line.

We can also use vi editor search command to access particular command from the history:
  1. Press the escape key once (esc).
  2. Type “/” along with the keyword you want to search in the history. For example, let say if we have used “ls -l” previously we can acess it directly by:
    • $/ls
  3. Above command will give back “ls -l” on the command line.
  4. If we have multiple search results we can traverse through them using “n” for forward search and “shift + n” keystrokes for backward search in the history. Like we normally do in Vi Editor.
If you like the idea of history, ask your system administrator to change your shell to any of the above shell, and add set -o vi, emacs or gmacs into your .profile file.

Comments

Popular posts from this blog

JUnit – Run unit test in an Sequence / Order

In JUnit, we can use @FixMethodOrder(MethodSorters.xxx) to run the test methods in a sequence or order.

import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runners.MethodSorters;importstatic org.hamcrest.CoreMatchers.is;importstatic org.junit.Assert.assertThat;//Sorts by method name@FixMethodOrder(MethodSorters.NAME_ASCENDING)publicclassExecutionOrderTest{@TestpublicvoidtestB(){assertThat(1+1,is(2));}@Testpublicvoidtest1(){assertThat(1+1,is(2));}@TestpublicvoidtestA

Create Runnable Jar - Eclipse Options

When exporting to a Runnable Jar, there are three options in eclipse Helios. Extract required libraries into JARPackage required libraries into JARCopy required libraries into sub folder next to JAR. What are differences : Extract required libraries into JAR - Extracts the actual .class files from the libraries your app uses and puts those .class files inside the runnable JAR. So, the runnable JAR will not only contain the .class files of your application, but also the .class files of all the libraries your application uses. Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible. Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the .class files of your application. Option #2 is convenient be…