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

Exploring Node.js Internals

I found a great article explaining Node JS internals, must read : https://www.smashingmagazine.com/2020/04/nodejs-internals/ Some other articles : Introduction to Node.js Being an official website, Node.dev explains what Node.js is, as well as its package managers, and lists web frameworks built on top of it. “ JavaScript & Node.js ”,  The Node Beginner Book This book by  Manuel Kiessling  does a fantastic job of explaining Node.js, after warning that JavaScript in the browser is not the same as the one in Node.js, even though both are written in the same language. Beginning Node.js This beginner book goes beyond an explanation of the runtime. It teaches about packages and streams and creating a web server with the Express framework. LibUV This is the official documentation of the supporting C++ code of the Node.js runtime. V8 This is the official documentation of the JavaScript engine that makes it possible to write Node.js with JavaScript.

Tum mujhko karna maaf priye – Poem By Dr. Kumar Vishwas

Lyrics : O kalp-vraksh ki son-juhi, O amal-tas ki amar kali Dharti ke aatap se jalte, Man par chaayi nirmal badli Main tumko madhu-sad-gandh yukt, sansaar nahi de paunga ! Tum mujhko karna maaf priye main pyar nahi de paunga !! Tum kalp-vraksh ka fool aur Main dharti ka adna gayak Tum jeewan ke upbhog yogya Main nahi adhuri gazal shubhey Tum saam-gaan si pawan ho Himshikharon par sehsa kaundha Bijuri si tum man bhawan ho Isliye vyarth shabdo wala, vyapar nahi de paunga ! Tum mujhko karna maaf priye main pyar nahi de paunga !! Tum jis shaiya par shayan karo, Veh sheer sindh si pawan ho Jis aangan ki ho maul-shree Veh aangan kya vrandawan ho Jin adhron ka chumban pao Veh adhar nahin ganga tat ho Jiski chaya ban saath raho Veh vyakti nahin vanshi-vat ho Par main vat jaisa saghan chaaon ,vistaar nahin de paunga ! Tum mujhko karna maaf main pyar nahi de paunga !! Main tumko chand sitaron ka Saupun uphar bhala kaise Main yayavar banjara sadhu sur-sansar bhala kaise Main jeewan