SEESenv/web/html/chn10session4.html
author amit@thunder
Thu, 25 Feb 2010 00:04:27 +0530
changeset 27 cb14131583c6
child 28 514098969b11
permissions -rw-r--r--
Started on final changes to the build scripts so that we can start with totally automatic builds

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   Chapter. session
  </title>
  <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
  <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
  <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
  <script type="text/javascript" src="/review/support/jquery-min.js">
  </script>
  <script type="text/javascript" src="/review/support/form.js">
  </script>
  <script type="text/javascript" src="/review/support/hsbook.js">
  </script>
 </head>
 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  <div class="chapter" id="chn10session4">
   <div class="titlepage">
   </div>
   <div class="toc">
    <p>
     <b>
      Table of Contents
     </b>
    </p>
    <dl>
     <dt>
      <span class="article">
       <a href="#id2501738">
       </a>
      </span>
     </dt>
     <dd>
      <dl>
       <dt>
        <span class="section">
         <a href="#id2311829">
          1. More text processing
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2499853">
            1.1.
            <code class="literal">
             sort
            </code>
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2552760">
            1.2.
            <code class="literal">
             uniq
            </code>
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2552862">
            1.3.
            <code class="literal">
             join
            </code>
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2552962">
          2. Generating a word frequency list
         </a>
        </span>
       </dt>
       <dt>
        <span class="section">
         <a href="#id2553109">
          3. Basic editing and editors
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2553118">
            3.1. vim
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2553684">
            3.2. SciTE
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2553729">
          4. Personalizing your Environment
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2553738">
            4.1. .bashrc
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2554039">
            4.2. .vimrc
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2554257">
          5. Subshells and
          <code class="literal">
           source
          </code>
         </a>
        </span>
       </dt>
      </dl>
     </dd>
    </dl>
   </div>
   <div class="article">
    <div class="titlepage">
     <div>
      <div>
       <h2 class="title" id="id2501738">
       </h2>
      </div>
      <div>
       <h2 class="title" id="id2501738">
       </h2>
      </div>
     </div>
     <hr />
    </div>
    <div class="toc">
     <p>
      <b>
       Table of Contents
      </b>
     </p>
     <dl>
      <dt>
       <span class="section">
        <a href="#id2311829">
         1. More text processing
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2499853">
           1.1.
           <code class="literal">
            sort
           </code>
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2552760">
           1.2.
           <code class="literal">
            uniq
           </code>
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2552862">
           1.3.
           <code class="literal">
            join
           </code>
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2552962">
         2. Generating a word frequency list
        </a>
       </span>
      </dt>
      <dt>
       <span class="section">
        <a href="#id2553109">
         3. Basic editing and editors
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2553118">
           3.1. vim
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2553684">
           3.2. SciTE
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2553729">
         4. Personalizing your Environment
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2553738">
           4.1. .bashrc
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2554039">
           4.2. .vimrc
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2554257">
         5. Subshells and
         <code class="literal">
          source
         </code>
        </a>
       </span>
      </dt>
     </dl>
    </div>
    <div class="section" title="1.More text processing">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2311829">
         </a>
         1.More text processing
        </h2>
       </div>
      </div>
     </div>
     <div class="section" title="1.1.sort">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2499853">
          </a>
          1.1.
          <code class="literal">
           sort
          </code>
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_1">
       Let's say we have a file which lists a few of the stalwarts of the open source community and a few details about them, like their "other" name, their homepage address, and what they are well known for or their claim to fame.
      </p>
      <pre class="programlisting">
       Richard Stallman%rms%GNU Project
Eric Raymond%ESR%Jargon File
Ian Murdock% %Debian
Lawrence Lessig% %Creative Commons
Linus Torvalds% %Linux Kernel
Guido van Rossum%BDFL%Python
Larry Wall% %Perl
      </pre>
      <p id="ch10session4_2">
       The sort command enables us to do this in a flash! Just running the sort command with the file name as a parameter sorts the lines of the file alphabetically and prints the output on the terminal.
      </p>
      <pre class="programlisting">
       $ sort stalwarts.txt
Eric Raymond%ESR%Jargon File
Guido van Rossum%BDFL%Python
Ian Murdock% %Debian
Larry Wall% %Perl
Lawrence Lessig% %Creative Commons
Linus Torvalds% %Linux Kernel
Richard Stallman%rms%GNU Project
      </pre>
      <p id="ch10session4_3">
       If you wish to sort them reverse alphabetically, you just need to pass the
       <code class="literal">
        -r
       </code>
       option. Now, you might want to sort the lines, based on each person's claim to fame or their "other" name. What do we do in that case?
      </p>
      <p id="ch10session4_4">
       Below is an example that sorts the file based on "other" names.
      </p>
      <pre class="programlisting">
       $ sort -t % -k 2,2  stalwarts.txt

Ian Murdock% %Debian
Larry Wall% %Perl
Lawrence Lessig% %Creative Commons
Linus Torvalds% %Linux Kernel
Guido van Rossum%BDFL%Python
Eric Raymond%ESR%Jargon File
Richard Stallman%rms%GNU Project
      </pre>
      <p id="ch10session4_5">
       Sort command assumes white space to be the default delimiter for columns in each line. The
       <code class="literal">
        -t
       </code>
       option specifies the delimiting character, which is
       <code class="literal">
        %
       </code>
       in this case.
      </p>
      <p id="ch10session4_6">
       The
       <code class="literal">
        -k
       </code>
       option starts a key at position 2 and ends it at 2, essentially telling the sort command that it should sort based on the 2nd column, which is the other name.
       <code class="literal">
        sort
       </code>
       also supports conflict resolution using multiple columns for sorting. You can see that the first three lines have nothing in the "other" names column. We could resolve the conflict by sorting based on the project names (the 3rd column).
      </p>
      <pre class="programlisting">
       $ sort -t % -k 2,2 -k 3,3  stalwarts.txt

Lawrence Lessig% %Creative Commons
Ian Murdock% %Debian
Linus Torvalds% %Linux Kernel
Larry Wall% %Perl
Guido van Rossum%BDFL%Python
Eric Raymond%ESR%Jargon File
Richard Stallman%rms%GNU Project
      </pre>
      <p id="ch10session4_7">
       <code class="literal">
        sort
       </code>
       also has a lot of other options like ignoring case differences, month sort(JAN&lt;FEB&lt;...), merging already sorted files.
       <code class="literal">
        man sort
       </code>
       would give you a lot of information.
      </p>
     </div>
     <div class="section" title="1.2.uniq">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2552760">
          </a>
          1.2.
          <code class="literal">
           uniq
          </code>
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_8">
       Suppose we have a list of items, say books, and we wish to obtain a list which names of all the books only once, without any duplicates. We use the
       <code class="literal">
        uniq
       </code>
       command to achieve this.
      </p>
      <pre class="programlisting">
       Programming Pearls
The C Programming Language
The Mythical Man Month: Essays on Software Engineering
Programming Pearls
The C Programming Language
Structure and Interpretation of Computer Programs
Programming Pearls
Compilers: Principles, Techniques, and Tools
The C Programming Language
The Art of UNIX Programming
Programming Pearls
The Art of Computer Programming
Introduction to Algorithms
The Art of UNIX Programming
The Pragmatic Programmer: From Journeyman to Master
Programming Pearls
Unix Power Tools
The Art of UNIX Programming
      </pre>
      <p id="ch10session4_9">
       Let us try and get rid of the duplicate lines from this file using the
       <code class="literal">
        uniq
       </code>
       command.
      </p>
      <pre class="programlisting">
       $ uniq items.txt
Programming Pearls
The C Programming Language
The Mythical Man Month: Essays on Software Engineering
Programming Pearls
The C Programming Language
Structure and Interpretation of Computer Programs
Programming Pearls
Compilers: Principles, Techniques, and Tools
The C Programming Language
The Art of UNIX Programming
Programming Pearls
The Art of Computer Programming
Introduction to Algorithms
The Art of UNIX Programming
The Pragmatic Programmer: From Journeyman to Master
Programming Pearls
Unix Power Tools
The Art of UNIX Programming
      </pre>
      <p id="ch10session4_a">
       Nothing happens! Why? The
       <code class="literal">
        uniq
       </code>
       command removes duplicate lines only when they are next to each other. So, we get a sorted file from the original file and work with that file, henceforth.
      </p>
      <pre class="programlisting">
       $ sort items.txt &gt; items-sorted.txt
$ uniq items-sorted.txt
Compilers: Principles, Techniques, and Tools
Introduction to Algorithms
Programming Pearls
Structure and Interpretation of Computer Programs
The Art of Computer Programming
The Art of UNIX Programming
The C Programming Language
The Mythical Man Month: Essays on Software Engineering
The Pragmatic Programmer: From Journeyman to Master
Unix Power Tools
      </pre>
      <p id="ch10session4_b">
       <code class="literal">
        uniq -u
       </code>
       command gives the lines which are unique and do not have any duplicates in the file.
       <code class="literal">
        uniq -d
       </code>
       outputs only those lines which have duplicates. The
       <code class="literal">
        -c
       </code>
       option displays the number of times each line occurs in the file.
      </p>
      <pre class="programlisting">
       $ uniq -u items-sorted.txt
Compilers: Principles, Techniques, and Tools
Introduction to Algorithms
Structure and Interpretation of Computer Programs
The Art of Computer Programming
The Mythical Man Month: Essays on Software Engineering
The Pragmatic Programmer: From Journeyman to Master
Unix Power Tools

$ uniq -dc items-sorted.txt
5 Programming Pearls
3 The Art of UNIX Programming
3 The C Programming Language
      </pre>
     </div>
     <div class="section" title="1.3.join">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2552862">
          </a>
          1.3.
          <code class="literal">
           join
          </code>
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_c">
       Now suppose we had the file
       <code class="literal">
        stalwarts1.txt
       </code>
       , which lists the home pages of all the people listed in
       <code class="literal">
        stalwarts.txt
       </code>
       .
      </p>
      <pre class="programlisting">
       Richard Stallman%http://www.stallman.org
Eric Raymond%http://www.catb.org/~esr/
Ian Murdock%http://ianmurdock.com/
Lawrence Lessig%http://lessig.org
Linus Torvalds%http://torvalds-family.blogspot.com/
Guido van Rossum%http://www.python.org/~guido/
Larry Wall%http://www.wall.org/~larry/
      </pre>
      <p id="ch10session4_d">
       It would be nice to have a single file with the information in both the files. To achieve this we use the
       <code class="literal">
        join
       </code>
       command.
      </p>
      <pre class="programlisting">
       $ join stalwarts.txt stalwarts1.txt -t %
Richard Stallman%rms%GNU Project%http://www.stallman.org
Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
Ian Murdock% %Debian%http://ianmurdock.com/
Lawrence Lessig% %Creative Commons%http://lessig.org
Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
Larry Wall% %Perl%http://www.wall.org/~larry/
      </pre>
      <p id="ch10session4_e">
       The
       <code class="literal">
        join
       </code>
       command joins the two files, based on the common field present in both the files, which is the name, in this case.
      </p>
      <p id="ch10session4_f">
       The
       <code class="literal">
        -t
       </code>
       option again specifies the delimiting character. Unless that is specified, join assumes that the fields are separated by spaces.
      </p>
      <p id="ch10session4_10">
       Note that, for
       <code class="literal">
        join
       </code>
       to work, the common field should be in the same order in both the files. If this is not so, you could use
       <code class="literal">
        sort
       </code>
       , to sort the files on the common field and then join the files. In the above example, we have the common field to be the first column in both the files. If this is not the case we could use the
       <code class="literal">
        -1
       </code>
       and
       <code class="literal">
        -2
       </code>
       options to specify the field to be used for joining the files.
      </p>
      <pre class="programlisting">
       $ join -2 2 stalwarts.txt stalwarts2.txt -t %
Richard Stallman%rms%GNU Project%http://www.stallman.org
Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
Ian Murdock% %Debian%http://ianmurdock.com/
Lawrence Lessig% %Creative Commons%http://lessig.org
Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
Larry Wall% %Perl%http://www.wall.org/~larry/
      </pre>
     </div>
    </div>
    <div class="section" title="2.Generating a word frequency list">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2552962">
         </a>
         2.Generating a word frequency list
        </h2>
       </div>
      </div>
     </div>
     <p id="ch10session4_11">
      Now, let us use the tools we have learnt to use, to generate a word frequency list of a text file. We shall use the free text of Alice in Wonderland.
     </p>
     <p id="ch10session4_12">
      The basic steps to achieve this task would be -
     </p>
     <div class="orderedlist">
      <ol class="orderedlist" type="1">
       <li class="listitem">
        <p id="ch10session4_13">
         Eliminate the punctuation and spaces from the document.
        </p>
       </li>
       <li class="listitem">
        <p id="ch10session4_14">
         Generate a list of words.
        </p>
       </li>
       <li class="listitem">
        <p id="ch10session4_15">
         Count the words.
        </p>
       </li>
      </ol>
     </div>
     <p id="ch10session4_16">
      We first use
      <code class="literal">
       grep
      </code>
      and some elementary
      <code class="literal">
       regex
      </code>
      to eliminate the non-alpha-characters.
     </p>
     <pre class="programlisting">
      $ grep "[A-Za-z]*" alice-in-wonderland.txt
     </pre>
     <p id="ch10session4_17">
      This outputs all the lines which has any alphabetic characters on it. This isn't of much use, since we haven't done anything with the code. We only require the alphabetic characters, without any of the other junk.
      <code class="literal">
       man grep
      </code>
      shows us the
      <code class="literal">
       -o
      </code>
      option for outputting only the text which matches the regular expression.
     </p>
     <pre class="programlisting">
      $ grep "[A-Za-z]*" -o alice-in-wonderland.txt
     </pre>
     <p id="ch10session4_18">
      Not very surprisingly, we have all the words, spit out in the form of a list! Now that we have a list of words, it is quite simple to count the occurrences of the words. You would've realized that we can make use of
      <code class="literal">
       sort
      </code>
      and
      <code class="literal">
       uniq
      </code>
      commands. We pipe the output from the
      <code class="literal">
       grep
      </code>
      to the
      <code class="literal">
       sort
      </code>
      and then pipe it's output to
      <code class="literal">
       uniq
      </code>
      .
     </p>
     <pre class="programlisting">
      $ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c
     </pre>
     <p id="ch10session4_19">
      Notice that you get the list of all words in the document in the alphabetical order, with it's frequency written next to it. But, you might have observed that Capitalized words and lower case words are being counted as different words. We therefore, replace all the Upper case characters with lower case ones, using the
      <code class="literal">
       tr
      </code>
      command.
     </p>
     <pre class="programlisting">
      $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c
     </pre>
     <p id="ch10session4_1a">
      Now, it would also be nice to have the list ordered in the decreasing order of the frequency of the appearance of the words. We sort the output of the
      <code class="literal">
       uniq
      </code>
      command with
      <code class="literal">
       -n
      </code>
      and
      <code class="literal">
       -r
      </code>
      options, to get the desired output.
     </p>
     <pre class="programlisting">
      $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
     </pre>
    </div>
    <div class="section" title="3.Basic editing and editors">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2553109">
         </a>
         3.Basic editing and editors
        </h2>
       </div>
      </div>
     </div>
     <div class="section" title="3.1.vim">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2553118">
          </a>
          3.1.vim
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_1b">
       Vim is a very powerful editor. It has a lot of commands, and all of them cannot be explained here. We shall try and look at a few, so that you can find your way around in vim.
      </p>
      <p id="ch10session4_1c">
       To open a file in vim, we pass the filename as a parameter to the
       <code class="literal">
        vim
       </code>
       command. If a file with that filename does not exist, a new file is created.
      </p>
      <pre class="programlisting">
       $ vim first.txt
      </pre>
      <p id="ch10session4_1d">
       To start inserting text into the new file that we have opened, we need to press the
       <code class="literal">
        i
       </code>
       key. This will take us into the
       <span class="emphasis">
        <em>
         insert
        </em>
       </span>
       mode from the
       <span class="emphasis">
        <em>
         command
        </em>
       </span>
       mode. Hitting the
       <code class="literal">
        esc
       </code>
       key, will bring us back to the
       <span class="emphasis">
        <em>
         command
        </em>
       </span>
       mode. There is also another mode of vim, called the
       <span class="emphasis">
        <em>
         visual
        </em>
       </span>
       mode which will be discussed later in the course.
      </p>
      <p id="ch10session4_1e">
       In general, it is good to spend as little time as possible in the insert mode and extensively use the command mode to achieve various tasks.
      </p>
      <p id="ch10session4_1f">
       To save the file, use
       <code class="literal">
        :w
       </code>
       in the command mode. From here on, it is understood that we are in the command mode, whenever we are issuing any command to vim.
      </p>
      <p id="ch10session4_20">
       To save a file and continue editing, use
       <code class="literal">
        :w FILENAME
       </code>
       The file name is optional. If you do not specify a filename, it is saved in the same file that you opened. If a file name different from the one you opened is specified, the text is saved with the new name, but you continue editing the file that you opened. The next time you save it without specifying a name, it gets saved with the name of the file that you initially opened.
      </p>
      <p id="ch10session4_21">
       To save file with a new name and continue editing the new file, use
       <code class="literal">
        :saveas FILENAME
       </code>
      </p>
      <p id="ch10session4_22">
       To save and quit, use
       <code class="literal">
        :wq
       </code>
      </p>
      <p id="ch10session4_23">
       To quit, use
       <code class="literal">
        :q
       </code>
      </p>
      <p id="ch10session4_24">
       To quit without saving, use
       <code class="literal">
        :q!
       </code>
      </p>
      <div class="section" title="3.1.1.Moving around">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2553239">
           </a>
           3.1.1.Moving around
          </h4>
         </div>
        </div>
       </div>
       <p id="ch10session4_25">
        While you are typing in a file, it is in-convenient to keep moving your fingers from the standard position for typing to the arrow keys. Vim, therefore, provides alternate keys for moving in the document. Note again that, you should be in the command mode, when issuing any commands to vim.
       </p>
       <p id="ch10session4_26">
        The basic cursor movement can be achieved using the keys,
        <code class="literal">
         h
        </code>
        (left),
        <code class="literal">
         l
        </code>
        (right),
        <code class="literal">
         k
        </code>
        (up) and
        <code class="literal">
         j
        </code>
        (down).
       </p>
       <pre class="programlisting">
        ^
      k
&lt; h       l &gt;
      j
      v
       </pre>
       <p id="ch10session4_27">
        Note: Most commands can be prefixed with a number, to repeat the command. For instance,
        <code class="literal">
         10j
        </code>
        will move the cursor down 10 lines.
       </p>
       <div class="section" title="3.1.1.1.Moving within a line">
        <div class="titlepage">
         <div>
          <div>
           <h5 class="title">
            <a name="id2553291">
            </a>
            3.1.1.1.Moving within a line
           </h5>
          </div>
         </div>
        </div>
        <p id="ch10session4_28">
         Cursor Movement
        </p>
        <p id="ch10session4_29">
         Command
        </p>
        <p id="ch10session4_2a">
         Forward by a screenful of text
        </p>
        <p id="ch10session4_2b">
         <code class="literal">
          C-f
         </code>
        </p>
        <p id="ch10session4_2c">
         Backward by a screenful of text
        </p>
        <p id="ch10session4_2d">
         <code class="literal">
          C-b
         </code>
        </p>
        <p id="ch10session4_2e">
         Beginning of the screen
        </p>
        <p id="ch10session4_2f">
         <code class="literal">
          H
         </code>
        </p>
        <p id="ch10session4_30">
         Middle of the screen
        </p>
        <p id="ch10session4_31">
         <code class="literal">
          M
         </code>
        </p>
        <p id="ch10session4_32">
         End of the screen
        </p>
        <p id="ch10session4_33">
         <code class="literal">
          L
         </code>
        </p>
        <p id="ch10session4_34">
         End of file
        </p>
        <p id="ch10session4_35">
         <code class="literal">
          G
         </code>
        </p>
        <p id="ch10session4_36">
         Line number
         <code class="literal">
          num
         </code>
        </p>
        <p id="ch10session4_37">
         <code class="literal">
          [num]G
         </code>
        </p>
        <p id="ch10session4_38">
         Beginning of file
        </p>
        <p id="ch10session4_39">
         <code class="literal">
          gg
         </code>
        </p>
        <p id="ch10session4_3a">
         Next occurrence of the text
under the cursor
        </p>
        <p id="ch10session4_3b">
         <code class="literal">
          *
         </code>
        </p>
        <p id="ch10session4_3c">
         Previous occurrence of the text
under the cursor
        </p>
        <p id="ch10session4_3d">
         <code class="literal">
          #
         </code>
        </p>
        <p id="ch10session4_3e">
         Note:
         <code class="literal">
          C-x
         </code>
         is
         <code class="literal">
          Ctrl
         </code>
         +
         <code class="literal">
          x
         </code>
        </p>
       </div>
      </div>
      <div class="section" title="3.1.2.The visual mode">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2553434">
           </a>
           3.1.2.The visual mode
          </h4>
         </div>
        </div>
       </div>
       <p id="ch10session4_3f">
        The visual mode is a special mode that is not present in the original vi editor. It allows us to highlight text and perform actions on it. All the movement commands that have been discussed till now work in the visual mode also. The editing commands that will be discussed in the future work on the visual blocks selected, too.
       </p>
      </div>
      <div class="section" title="3.1.3.Editing commands">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2553452">
           </a>
           3.1.3.Editing commands
          </h4>
         </div>
        </div>
       </div>
       <p id="ch10session4_40">
        The editing commands usually take the movements as arguments. A movement is equivalent to a selection in the visual mode. The cursor is assumed to have moved over the text in between the initial and the final points of the movement. The motion or the visual block that's been highlighted can be passed as arguments to the editing commands.
       </p>
       <p id="ch10session4_41">
        Editing effect
       </p>
       <p id="ch10session4_42">
        Command
       </p>
       <p id="ch10session4_43">
        Cut the character under the cursor
       </p>
       <p id="ch10session4_44">
        <code class="literal">
         x
        </code>
       </p>
       <p id="ch10session4_45">
        Replace the character under the
cursor with
        <code class="literal">
         a
        </code>
       </p>
       <p id="ch10session4_46">
        <code class="literal">
         ra
        </code>
       </p>
       <p id="ch10session4_47">
        Cut an entire line
       </p>
       <p id="ch10session4_48">
        <code class="literal">
         dd
        </code>
       </p>
       <p id="ch10session4_49">
        Copy/yank an entire line
       </p>
       <p id="ch10session4_4a">
        <code class="literal">
         yy
        </code>
       </p>
       <p id="ch10session4_4b">
        Note: You can prefix numbers to any of the commands, to repeat them.
       </p>
      </div>
      <div class="section" title="3.1.4.Undo and Redo">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2553532">
           </a>
           3.1.4.Undo and Redo
          </h4>
         </div>
        </div>
       </div>
       <p id="ch10session4_4c">
        You can undo almost anything using
        <code class="literal">
         u
        </code>
        .
       </p>
       <p id="ch10session4_4d">
        To undo the undo command type
        <code class="literal">
         C-r
        </code>
       </p>
      </div>
      <div class="section" title="3.1.5.Searching and Replacing">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2553557">
           </a>
           3.1.5.Searching and Replacing
          </h4>
         </div>
        </div>
       </div>
       <p id="ch10session4_4e">
        Finding and Replacing
       </p>
       <p id="ch10session4_4f">
        Command
       </p>
       <p id="ch10session4_50">
        Replace the first instance of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the current line.
       </p>
       <p id="ch10session4_51">
        <code class="literal">
         :s/old/new
        </code>
       </p>
       <p id="ch10session4_52">
        Replace all instances of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the current line.
       </p>
       <p id="ch10session4_53">
        <code class="literal">
         :s/old/new/g
        </code>
       </p>
       <p id="ch10session4_54">
        Replace all instances of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the current line,
but ask for confirmation each time.
       </p>
       <p id="ch10session4_55">
        <code class="literal">
         :s/old/new/gc
        </code>
       </p>
       <p id="ch10session4_56">
        Replace the first instance of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the entire file.
       </p>
       <p id="ch10session4_57">
        <code class="literal">
         :%s/old/new
        </code>
       </p>
       <p id="ch10session4_58">
        Replace all instances of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the entire file.
       </p>
       <p id="ch10session4_59">
        <code class="literal">
         :%s/old/new/g
        </code>
       </p>
       <p id="ch10session4_5a">
        Replace all instances of
        <code class="literal">
         old
        </code>
        with
        <code class="literal">
         new
        </code>
        in the entire file but ask
for confirmation each time.
       </p>
       <p id="ch10session4_5b">
        <code class="literal">
         :%s/old/new/gc
        </code>
       </p>
      </div>
     </div>
     <div class="section" title="3.2.SciTE">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2553684">
          </a>
          3.2.SciTE
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_5c">
       SciTE is a
       <span class="emphasis">
        <em>
         source code
        </em>
       </span>
       editor, that has a feel similar to the commonly used GUI text editors. It has a wide range of features that are extremely useful for a programmer, editing code. Also it aims to keep configuration simple, and the user needs to edit a text file to configure SciTE to his/her liking.
      </p>
      <p id="ch10session4_5d">
       Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice.
      </p>
      <p id="ch10session4_5e">
       SciTE can syntax highlight code in various languages. It also has auto-indentation, code-folding and other such features which are useful when editing code.
      </p>
      <p id="ch10session4_5f">
       SciTE also gives you the option to (compile and) run your code, from within the editor.
      </p>
     </div>
    </div>
    <div class="section" title="4.Personalizing your Environment">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2553729">
         </a>
         4.Personalizing your Environment
        </h2>
       </div>
      </div>
     </div>
     <div class="section" title="4.1..bashrc">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2553738">
          </a>
          4.1..bashrc
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_60">
       What would you do, if you want bash to execute a particular command each time you start it up? For instance, say you want the current directory to be your Desktop instead of your home folder, each time bash starts up. How would you achieve this? Bash reads and executes commands in a whole bunch of files called start-up files, when it starts up.
      </p>
      <p id="ch10session4_61">
       When bash starts up as an interactive login shell, it reads the files
       <code class="literal">
        /etc/profile
       </code>
       ,
       <code class="literal">
        ~/.bash_profile
       </code>
       ,
       <code class="literal">
        ~/.bash_login
       </code>
       , and
       <code class="literal">
        ~/.profile
       </code>
       in that order.
      </p>
      <p id="ch10session4_62">
       When it is a shell that is not a login shell,
       <code class="literal">
        ~/.bashrc
       </code>
       is read and the commands in it are executed. This can be prevented using the
       <code class="literal">
        --norc
       </code>
       option. To force bash to use another file, instead of the
       <code class="literal">
        ~/.bashrc
       </code>
       file on start-up, the
       <code class="literal">
        --rcfile
       </code>
       option may be used.
      </p>
      <p id="ch10session4_63">
       Now, you know what you should do, to change the current directory to you Desktop. Just put a
       <code class="literal">
        cd ~/Desktop
       </code>
       into your
       <code class="literal">
        ~/.bashrc
       </code>
       and you are set!
      </p>
      <p id="ch10session4_64">
       This example is quite a simple and lame one. The start-up files are used for a lot more complex things than this. You could set (or unset) aliases and a whole bunch of environment variables in the
       <code class="literal">
        .bashrc
       </code>
       . We shall look at them, in the next section where we look at environment variables and
       <code class="literal">
        set
       </code>
       command.
      </p>
     </div>
     <div class="section" title="4.2..vimrc">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2554039">
          </a>
          4.2..vimrc
         </h3>
        </div>
       </div>
      </div>
      <p id="ch10session4_65">
       <code class="literal">
        .vimrc
       </code>
       is a file similar to
       <code class="literal">
        .bashrc
       </code>
       for vim. It is a start-up file that vim reads and executes, each time it starts up. The options that you would like to be set every time you use vim, are placed in the
       <code class="literal">
        .vimrc
       </code>
       file, so that they are automatically set each time vim starts. The recommended place for having your
       <code class="literal">
        .vimrc
       </code>
       is also your home directory.
      </p>
      <p id="ch10session4_66">
       The file
       <code class="literal">
        /etc/vimrc
       </code>
       is the global config file and shouldn't usually be edited. You can instead edit the
       <code class="literal">
        ~/.vimrc
       </code>
       file that is present in your home folder.
      </p>
      <p id="ch10session4_67">
       There are a whole bunch of variables that you could set in the
       <code class="literal">
        .vimrc
       </code>
       file. You can look at all the options available, using the
       <code class="literal">
        :set all
       </code>
       command in vim. You could use the
       <code class="literal">
        :help option_name
       </code>
       to get more information about the option that you want to set. Once you are comfortable with what you want to set a particular variable to, you could add it to
       <code class="literal">
        .vimrc
       </code>
       . You should also look at
       <code class="literal">
        :help vimrc
       </code>
       for more info on the
       <code class="literal">
        .vimrc
       </code>
       file. If you already have a
       <code class="literal">
        .vimrc
       </code>
       file, you can edit it from within vim, using
       <code class="literal">
        :e $MYVIMRC
       </code>
       command. We shall look at some of the most commonly used options.
      </p>
      <p id="ch10session4_68">
       Command
      </p>
      <p id="ch10session4_69">
       Vim action
      </p>
      <p id="ch10session4_6a">
       <code class="literal">
        set nocompatible
       </code>
      </p>
      <p id="ch10session4_6b">
       Explicitly disable compatibility with vi
      </p>
      <p id="ch10session4_6c">
       <code class="literal">
        set backspace=indent,eol,start
       </code>
      </p>
      <p id="ch10session4_6d">
       In the insert mode, vim allows the backspace key to delete white spaces at the
start of line, line breaks and the character before which insert mode started.
      </p>
      <p id="ch10session4_6e">
       set autoindent
      </p>
      <p id="ch10session4_6f">
       Vim indents a new line with the same indentation of the previous line.
      </p>
      <p id="ch10session4_70">
       set backup
      </p>
      <p id="ch10session4_71">
       Vim keeps a backup copy of a file when overwriting it.
      </p>
      <p id="ch10session4_72">
       set history=50
      </p>
      <p id="ch10session4_73">
       Vim keeps 50 commands and 50 search patterns in the history.
      </p>
      <p id="ch10session4_74">
       set ruler
      </p>
      <p id="ch10session4_75">
       Displays the current cursor position in the lower right corner of the vim window.
      </p>
      <p id="ch10session4_76">
       set showcmd
      </p>
      <p id="ch10session4_77">
       Displays the incomplete command in the lower right corner.
      </p>
      <p id="ch10session4_78">
       set incsearch
      </p>
      <p id="ch10session4_79">
       Turns on incremental searching. Displays search results while you type.
      </p>
      <p id="ch10session4_7a">
       You can see the effect of the changes made to your
       <code class="literal">
        .vimrc
       </code>
       file by restarting vim. If you want to see the changes that you made to your
       <code class="literal">
        .vimrc
       </code>
       file immediately, you could source the file from within vim.
      </p>
      If the
      <code class="literal">
       .vimrc
      </code>
      file has been sourced when this instance of vim was started, you could just resource the file again::
      <p id="ch10session4_7b">
       :so $MYVIMRC
      </p>
      <p id="ch10session4_7c">
       If you just created the
       <code class="literal">
        .vimrc
       </code>
       file or it was not sourced when you stared this instance of vim, just replace the
       <code class="literal">
        $MYVIMRC
       </code>
       variable above, with the location of the
       <code class="literal">
        .vimrc
       </code>
       file that you created/edited.
      </p>
     </div>
    </div>
    <div class="section" title="5.Subshells and source">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2554257">
         </a>
         5.Subshells and
         <code class="literal">
          source
         </code>
        </h2>
       </div>
      </div>
     </div>
     <p id="ch10session4_7d">
      A subshell is just a separate instance of the shell which is a child process of the shell that launches it. Bash creates a subshell in various circumstances. Creation of subshells allows the execution of various processes simultaneously.
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" type="*">
       <li class="listitem" style="list-style-type: *">
        <p id="ch10session4_7e">
         When an external command is executed, a new subshell is created. Any built-in commands of bash are executed with int the same shell, and no new subshell is started. When an external command is run, the bash shell copies itself (along with it's environment) creating a subshell and the process is changed to the external command executed. The subshell is a child process of this shell.
        </p>
       </li>
       <li class="listitem" style="list-style-type: *">
        <p id="ch10session4_7f">
         Any pipes being used, create a subshell. The commands on the input and output ends of the pipe are run in different subshells.
        </p>
       </li>
       <li class="listitem" style="list-style-type: *">
        <p id="ch10session4_80">
         You could also, explicitly tell bash to start a subshell by enclosing a list of commands between parentheses. Each of the commands in the list is executed within a single new subshell.
        </p>
       </li>
      </ul>
     </div>
     <p id="ch10session4_81">
      To avoid creating a subshell, when running a shell script, you could use the
      <code class="literal">
       source
      </code>
      command.
     </p>
     <pre class="programlisting">
      $ source script.sh
     </pre>
     <p id="ch10session4_82">
      This will run the
      <code class="literal">
       script.sh
      </code>
      within the present shell without creating a subshell. The
      <code class="literal">
       .
      </code>
      command is an alias for the source command.
      <code class="literal">
       . script.sh
      </code>
      is therefore equivalent to
      <code class="literal">
       source script.sh
      </code>
      .
     </p>
    </div>
   </div>
  </div>
 </body>
</html>