All this tips assume that you use the terminal(bash/ksh/csh) in “vi” mode.
If you don’t use them in vi mode; here is how you can do it ..

sangeek@ubuntu:~\
$ set -o vi


You set this in your profile i.e. ~/.profile file, so that any new terminal created will open up in vi mode.


To get a glimpse of the advantages of working in vi mode, you can look here http://www.hypexr.org/bash_tutorial.php#vi. A simple search in google will land you with many more results and cheetsheets.

One such good cheet-sheet containing a lot of shortcuts is : http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf

Here I am going to highlight two of them, which I recently got to know about, and which seem very useful too :

1. Reducing the pain of retyping by commenting out

There are times when you would have typed out a long command and just before you are about to execute it; you realize that there was something else to be typed before that …
what do you do?? Ctrl+C
It would mean you would have to type out the previously typed long command again, maybe by copy-paste parts of the previously typed command visible in the terminal

Now, here is the trick, let me show with an example ..
I want to run a loop, I type it out :
sangeek@ubuntu:~\
$ while [ $i -le 10 ]; do (( i++ )); date

just when I was about to execute it, oops .. i forgot to initialize “i”. So, what now ..
the traditional way of “ctrl+c” would make me type out the entire script again after I have intialized “i”.

But in “vi” mode of bash, I get to the command mode by typing “esc” and then do “#”.
sangeek@ubuntu:~\
$ #while [ $i -le 10 ]; do (( i++ )); date

What did typing # do? It sent out my entire command to history by adding a # in front of it. So my command becomes a comment but it is still there in history.
I can get it back from history after I have intialized “i” and remove the hash and run it ..

sangeek@ubuntu:~\
$ i=1
sangeek@ubuntu:~\
$ while [ $i -le 10 ]; do (( i++ )); date

quite easy on my fingers 🙂

2. Edit/Execute the command in a text editor

Sometimes you have a big command as in previous example; or sometimes even bigger.
And if you want to edit it, it’s not easier to do it from the command-line(even in vi mode), it helps a lot to open up the existing line in a vi editor and edit.

How do we do that? Go to Esc mode and type v.
Now your existing line in bash is opened up in an editor. Edit it as you want .. type :wq to save and execute the newly edited command.

reduces the strain a lot .. really!!

 

What do you think ?

Set your Twitter account name in your settings to use the TwitterBar Section.
%d bloggers like this: