SEESenv/web/html/chn10session4.html
changeset 28 514098969b11
parent 27 cb14131583c6
equal deleted inserted replaced
27:cb14131583c6 28:514098969b11
     1 <html>
     1 <html>
     2  <head>
     2 <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>
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch10session4">
     4   <title>
     4 <div class="titlepage"></div>
     5    Chapter. session
     5 <div class="toc">
     6   </title>
     6 <p><b>Table of Contents</b></p>
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
     7 <dl>
     8   <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
     8 <dt><span class="article"><a href="#id2773576"></a></span></dt>
     9   <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
     9 <dd><dl>
    10   <script type="text/javascript" src="/review/support/jquery-min.js">
    10 <dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
    11   </script>
    11 <dd><dl>
    12   <script type="text/javascript" src="/review/support/form.js">
    12 <dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
    13   </script>
    13 <dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    14 <dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
    15   </script>
    15 </dl></dd>
    16  </head>
    16 <dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
    17 <dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
    18   <div class="chapter" id="chn10session4">
    18 <dd><dl>
    19    <div class="titlepage">
    19 <dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
    20    </div>
    20 <dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
    21    <div class="toc">
    21 </dl></dd>
    22     <p>
    22 <dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
    23      <b>
    23 <dd><dl>
    24       Table of Contents
    24 <dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
    25      </b>
    25 <dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
    26     </p>
    26 </dl></dd>
    27     <dl>
    27 <dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
    28      <dt>
    28 </dl></dd>
    29       <span class="article">
    29 </dl>
    30        <a href="#id2501738">
    30 </div>
    31        </a>
    31 <div class="article">
    32       </span>
    32 <div class="titlepage">
    33      </dt>
    33 <div>
    34      <dd>
    34 <div><h2 class="title" id="id2773576"></h2></div>
    35       <dl>
    35 <div><h2 class="title" id="id2773576"></h2></div>
    36        <dt>
    36 </div>
    37         <span class="section">
    37 <hr />
    38          <a href="#id2311829">
    38 </div>
    39           1. More text processing
    39 <div class="toc">
    40          </a>
    40 <p><b>Table of Contents</b></p>
    41         </span>
    41 <dl>
    42        </dt>
    42 <dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
    43        <dd>
    43 <dd><dl>
    44         <dl>
    44 <dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
    45          <dt>
    45 <dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
    46           <span class="section">
    46 <dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
    47            <a href="#id2499853">
    47 </dl></dd>
    48             1.1.
    48 <dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
    49             <code class="literal">
    49 <dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
    50              sort
    50 <dd><dl>
    51             </code>
    51 <dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
    52            </a>
    52 <dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
    53           </span>
    53 </dl></dd>
    54          </dt>
    54 <dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
    55          <dt>
    55 <dd><dl>
    56           <span class="section">
    56 <dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
    57            <a href="#id2552760">
    57 <dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
    58             1.2.
    58 </dl></dd>
    59             <code class="literal">
    59 <dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
    60              uniq
    60 </dl>
    61             </code>
    61 </div>
    62            </a>
    62 <div class="section" title="1.More text processing">
    63           </span>
    63 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    64          </dt>
    64 <a name="id2583667"></a>1.More text processing</h2></div></div></div>
    65          <dt>
    65 <div class="section" title="1.1.sort">
    66           <span class="section">
    66 <div class="titlepage"><div><div><h3 class="title">
    67            <a href="#id2552862">
    67 <a name="id2771691"></a>1.1.<code class="literal">sort</code>
    68             1.3.
    68 </h3></div></div></div>
    69             <code class="literal">
    69 <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>
    70              join
    70 <pre class="programlisting">
    71             </code>
    71 Richard Stallman%rms%GNU Project
    72            </a>
       
    73           </span>
       
    74          </dt>
       
    75         </dl>
       
    76        </dd>
       
    77        <dt>
       
    78         <span class="section">
       
    79          <a href="#id2552962">
       
    80           2. Generating a word frequency list
       
    81          </a>
       
    82         </span>
       
    83        </dt>
       
    84        <dt>
       
    85         <span class="section">
       
    86          <a href="#id2553109">
       
    87           3. Basic editing and editors
       
    88          </a>
       
    89         </span>
       
    90        </dt>
       
    91        <dd>
       
    92         <dl>
       
    93          <dt>
       
    94           <span class="section">
       
    95            <a href="#id2553118">
       
    96             3.1. vim
       
    97            </a>
       
    98           </span>
       
    99          </dt>
       
   100          <dt>
       
   101           <span class="section">
       
   102            <a href="#id2553684">
       
   103             3.2. SciTE
       
   104            </a>
       
   105           </span>
       
   106          </dt>
       
   107         </dl>
       
   108        </dd>
       
   109        <dt>
       
   110         <span class="section">
       
   111          <a href="#id2553729">
       
   112           4. Personalizing your Environment
       
   113          </a>
       
   114         </span>
       
   115        </dt>
       
   116        <dd>
       
   117         <dl>
       
   118          <dt>
       
   119           <span class="section">
       
   120            <a href="#id2553738">
       
   121             4.1. .bashrc
       
   122            </a>
       
   123           </span>
       
   124          </dt>
       
   125          <dt>
       
   126           <span class="section">
       
   127            <a href="#id2554039">
       
   128             4.2. .vimrc
       
   129            </a>
       
   130           </span>
       
   131          </dt>
       
   132         </dl>
       
   133        </dd>
       
   134        <dt>
       
   135         <span class="section">
       
   136          <a href="#id2554257">
       
   137           5. Subshells and
       
   138           <code class="literal">
       
   139            source
       
   140           </code>
       
   141          </a>
       
   142         </span>
       
   143        </dt>
       
   144       </dl>
       
   145      </dd>
       
   146     </dl>
       
   147    </div>
       
   148    <div class="article">
       
   149     <div class="titlepage">
       
   150      <div>
       
   151       <div>
       
   152        <h2 class="title" id="id2501738">
       
   153        </h2>
       
   154       </div>
       
   155       <div>
       
   156        <h2 class="title" id="id2501738">
       
   157        </h2>
       
   158       </div>
       
   159      </div>
       
   160      <hr />
       
   161     </div>
       
   162     <div class="toc">
       
   163      <p>
       
   164       <b>
       
   165        Table of Contents
       
   166       </b>
       
   167      </p>
       
   168      <dl>
       
   169       <dt>
       
   170        <span class="section">
       
   171         <a href="#id2311829">
       
   172          1. More text processing
       
   173         </a>
       
   174        </span>
       
   175       </dt>
       
   176       <dd>
       
   177        <dl>
       
   178         <dt>
       
   179          <span class="section">
       
   180           <a href="#id2499853">
       
   181            1.1.
       
   182            <code class="literal">
       
   183             sort
       
   184            </code>
       
   185           </a>
       
   186          </span>
       
   187         </dt>
       
   188         <dt>
       
   189          <span class="section">
       
   190           <a href="#id2552760">
       
   191            1.2.
       
   192            <code class="literal">
       
   193             uniq
       
   194            </code>
       
   195           </a>
       
   196          </span>
       
   197         </dt>
       
   198         <dt>
       
   199          <span class="section">
       
   200           <a href="#id2552862">
       
   201            1.3.
       
   202            <code class="literal">
       
   203             join
       
   204            </code>
       
   205           </a>
       
   206          </span>
       
   207         </dt>
       
   208        </dl>
       
   209       </dd>
       
   210       <dt>
       
   211        <span class="section">
       
   212         <a href="#id2552962">
       
   213          2. Generating a word frequency list
       
   214         </a>
       
   215        </span>
       
   216       </dt>
       
   217       <dt>
       
   218        <span class="section">
       
   219         <a href="#id2553109">
       
   220          3. Basic editing and editors
       
   221         </a>
       
   222        </span>
       
   223       </dt>
       
   224       <dd>
       
   225        <dl>
       
   226         <dt>
       
   227          <span class="section">
       
   228           <a href="#id2553118">
       
   229            3.1. vim
       
   230           </a>
       
   231          </span>
       
   232         </dt>
       
   233         <dt>
       
   234          <span class="section">
       
   235           <a href="#id2553684">
       
   236            3.2. SciTE
       
   237           </a>
       
   238          </span>
       
   239         </dt>
       
   240        </dl>
       
   241       </dd>
       
   242       <dt>
       
   243        <span class="section">
       
   244         <a href="#id2553729">
       
   245          4. Personalizing your Environment
       
   246         </a>
       
   247        </span>
       
   248       </dt>
       
   249       <dd>
       
   250        <dl>
       
   251         <dt>
       
   252          <span class="section">
       
   253           <a href="#id2553738">
       
   254            4.1. .bashrc
       
   255           </a>
       
   256          </span>
       
   257         </dt>
       
   258         <dt>
       
   259          <span class="section">
       
   260           <a href="#id2554039">
       
   261            4.2. .vimrc
       
   262           </a>
       
   263          </span>
       
   264         </dt>
       
   265        </dl>
       
   266       </dd>
       
   267       <dt>
       
   268        <span class="section">
       
   269         <a href="#id2554257">
       
   270          5. Subshells and
       
   271          <code class="literal">
       
   272           source
       
   273          </code>
       
   274         </a>
       
   275        </span>
       
   276       </dt>
       
   277      </dl>
       
   278     </div>
       
   279     <div class="section" title="1.More text processing">
       
   280      <div class="titlepage">
       
   281       <div>
       
   282        <div>
       
   283         <h2 class="title" style="clear: both">
       
   284          <a name="id2311829">
       
   285          </a>
       
   286          1.More text processing
       
   287         </h2>
       
   288        </div>
       
   289       </div>
       
   290      </div>
       
   291      <div class="section" title="1.1.sort">
       
   292       <div class="titlepage">
       
   293        <div>
       
   294         <div>
       
   295          <h3 class="title">
       
   296           <a name="id2499853">
       
   297           </a>
       
   298           1.1.
       
   299           <code class="literal">
       
   300            sort
       
   301           </code>
       
   302          </h3>
       
   303         </div>
       
   304        </div>
       
   305       </div>
       
   306       <p id="ch10session4_1">
       
   307        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.
       
   308       </p>
       
   309       <pre class="programlisting">
       
   310        Richard Stallman%rms%GNU Project
       
   311 Eric Raymond%ESR%Jargon File
    72 Eric Raymond%ESR%Jargon File
   312 Ian Murdock% %Debian
    73 Ian Murdock% %Debian
   313 Lawrence Lessig% %Creative Commons
    74 Lawrence Lessig% %Creative Commons
   314 Linus Torvalds% %Linux Kernel
    75 Linus Torvalds% %Linux Kernel
   315 Guido van Rossum%BDFL%Python
    76 Guido van Rossum%BDFL%Python
   316 Larry Wall% %Perl
    77 Larry Wall% %Perl</pre>
   317       </pre>
    78 <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>
   318       <p id="ch10session4_2">
    79 <pre class="programlisting">
   319        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.
    80 $ sort stalwarts.txt
   320       </p>
       
   321       <pre class="programlisting">
       
   322        $ sort stalwarts.txt
       
   323 Eric Raymond%ESR%Jargon File
    81 Eric Raymond%ESR%Jargon File
   324 Guido van Rossum%BDFL%Python
    82 Guido van Rossum%BDFL%Python
   325 Ian Murdock% %Debian
    83 Ian Murdock% %Debian
   326 Larry Wall% %Perl
    84 Larry Wall% %Perl
   327 Lawrence Lessig% %Creative Commons
    85 Lawrence Lessig% %Creative Commons
   328 Linus Torvalds% %Linux Kernel
    86 Linus Torvalds% %Linux Kernel
   329 Richard Stallman%rms%GNU Project
    87 Richard Stallman%rms%GNU Project</pre>
   330       </pre>
    88 <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>
   331       <p id="ch10session4_3">
    89 <p id="ch10session4_4">Below is an example that sorts the file based on "other" names.</p>
   332        If you wish to sort them reverse alphabetically, you just need to pass the
    90 <pre class="programlisting">
   333        <code class="literal">
    91 $ sort -t % -k 2,2  stalwarts.txt
   334         -r
       
   335        </code>
       
   336        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?
       
   337       </p>
       
   338       <p id="ch10session4_4">
       
   339        Below is an example that sorts the file based on "other" names.
       
   340       </p>
       
   341       <pre class="programlisting">
       
   342        $ sort -t % -k 2,2  stalwarts.txt
       
   343 
    92 
   344 Ian Murdock% %Debian
    93 Ian Murdock% %Debian
   345 Larry Wall% %Perl
    94 Larry Wall% %Perl
   346 Lawrence Lessig% %Creative Commons
    95 Lawrence Lessig% %Creative Commons
   347 Linus Torvalds% %Linux Kernel
    96 Linus Torvalds% %Linux Kernel
   348 Guido van Rossum%BDFL%Python
    97 Guido van Rossum%BDFL%Python
   349 Eric Raymond%ESR%Jargon File
    98 Eric Raymond%ESR%Jargon File
   350 Richard Stallman%rms%GNU Project
    99 Richard Stallman%rms%GNU Project</pre>
   351       </pre>
   100 <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>
   352       <p id="ch10session4_5">
   101 <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>
   353        Sort command assumes white space to be the default delimiter for columns in each line. The
   102 <pre class="programlisting">
   354        <code class="literal">
   103 $ sort -t % -k 2,2 -k 3,3  stalwarts.txt
   355         -t
       
   356        </code>
       
   357        option specifies the delimiting character, which is
       
   358        <code class="literal">
       
   359         %
       
   360        </code>
       
   361        in this case.
       
   362       </p>
       
   363       <p id="ch10session4_6">
       
   364        The
       
   365        <code class="literal">
       
   366         -k
       
   367        </code>
       
   368        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.
       
   369        <code class="literal">
       
   370         sort
       
   371        </code>
       
   372        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).
       
   373       </p>
       
   374       <pre class="programlisting">
       
   375        $ sort -t % -k 2,2 -k 3,3  stalwarts.txt
       
   376 
   104 
   377 Lawrence Lessig% %Creative Commons
   105 Lawrence Lessig% %Creative Commons
   378 Ian Murdock% %Debian
   106 Ian Murdock% %Debian
   379 Linus Torvalds% %Linux Kernel
   107 Linus Torvalds% %Linux Kernel
   380 Larry Wall% %Perl
   108 Larry Wall% %Perl
   381 Guido van Rossum%BDFL%Python
   109 Guido van Rossum%BDFL%Python
   382 Eric Raymond%ESR%Jargon File
   110 Eric Raymond%ESR%Jargon File
   383 Richard Stallman%rms%GNU Project
   111 Richard Stallman%rms%GNU Project</pre>
   384       </pre>
   112 <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>
   385       <p id="ch10session4_7">
   113 </div>
   386        <code class="literal">
   114 <div class="section" title="1.2.uniq">
   387         sort
   115 <div class="titlepage"><div><div><h3 class="title">
   388        </code>
   116 <a name="id2824598"></a>1.2.<code class="literal">uniq</code>
   389        also has a lot of other options like ignoring case differences, month sort(JAN&lt;FEB&lt;...), merging already sorted files.
   117 </h3></div></div></div>
   390        <code class="literal">
   118 <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>
   391         man sort
   119 <pre class="programlisting">
   392        </code>
   120 Programming Pearls
   393        would give you a lot of information.
       
   394       </p>
       
   395      </div>
       
   396      <div class="section" title="1.2.uniq">
       
   397       <div class="titlepage">
       
   398        <div>
       
   399         <div>
       
   400          <h3 class="title">
       
   401           <a name="id2552760">
       
   402           </a>
       
   403           1.2.
       
   404           <code class="literal">
       
   405            uniq
       
   406           </code>
       
   407          </h3>
       
   408         </div>
       
   409        </div>
       
   410       </div>
       
   411       <p id="ch10session4_8">
       
   412        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
       
   413        <code class="literal">
       
   414         uniq
       
   415        </code>
       
   416        command to achieve this.
       
   417       </p>
       
   418       <pre class="programlisting">
       
   419        Programming Pearls
       
   420 The C Programming Language
   121 The C Programming Language
   421 The Mythical Man Month: Essays on Software Engineering
   122 The Mythical Man Month: Essays on Software Engineering
   422 Programming Pearls
   123 Programming Pearls
   423 The C Programming Language
   124 The C Programming Language
   424 Structure and Interpretation of Computer Programs
   125 Structure and Interpretation of Computer Programs
   431 Introduction to Algorithms
   132 Introduction to Algorithms
   432 The Art of UNIX Programming
   133 The Art of UNIX Programming
   433 The Pragmatic Programmer: From Journeyman to Master
   134 The Pragmatic Programmer: From Journeyman to Master
   434 Programming Pearls
   135 Programming Pearls
   435 Unix Power Tools
   136 Unix Power Tools
   436 The Art of UNIX Programming
   137 The Art of UNIX Programming</pre>
   437       </pre>
   138 <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>
   438       <p id="ch10session4_9">
   139 <pre class="programlisting">
   439        Let us try and get rid of the duplicate lines from this file using the
   140 $ uniq items.txt
   440        <code class="literal">
       
   441         uniq
       
   442        </code>
       
   443        command.
       
   444       </p>
       
   445       <pre class="programlisting">
       
   446        $ uniq items.txt
       
   447 Programming Pearls
   141 Programming Pearls
   448 The C Programming Language
   142 The C Programming Language
   449 The Mythical Man Month: Essays on Software Engineering
   143 The Mythical Man Month: Essays on Software Engineering
   450 Programming Pearls
   144 Programming Pearls
   451 The C Programming Language
   145 The C Programming Language
   459 Introduction to Algorithms
   153 Introduction to Algorithms
   460 The Art of UNIX Programming
   154 The Art of UNIX Programming
   461 The Pragmatic Programmer: From Journeyman to Master
   155 The Pragmatic Programmer: From Journeyman to Master
   462 Programming Pearls
   156 Programming Pearls
   463 Unix Power Tools
   157 Unix Power Tools
   464 The Art of UNIX Programming
   158 The Art of UNIX Programming</pre>
   465       </pre>
   159 <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>
   466       <p id="ch10session4_a">
   160 <pre class="programlisting">
   467        Nothing happens! Why? The
   161 $ sort items.txt &gt; items-sorted.txt
   468        <code class="literal">
       
   469         uniq
       
   470        </code>
       
   471        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.
       
   472       </p>
       
   473       <pre class="programlisting">
       
   474        $ sort items.txt &gt; items-sorted.txt
       
   475 $ uniq items-sorted.txt
   162 $ uniq items-sorted.txt
   476 Compilers: Principles, Techniques, and Tools
   163 Compilers: Principles, Techniques, and Tools
   477 Introduction to Algorithms
   164 Introduction to Algorithms
   478 Programming Pearls
   165 Programming Pearls
   479 Structure and Interpretation of Computer Programs
   166 Structure and Interpretation of Computer Programs
   480 The Art of Computer Programming
   167 The Art of Computer Programming
   481 The Art of UNIX Programming
   168 The Art of UNIX Programming
   482 The C Programming Language
   169 The C Programming Language
   483 The Mythical Man Month: Essays on Software Engineering
   170 The Mythical Man Month: Essays on Software Engineering
   484 The Pragmatic Programmer: From Journeyman to Master
   171 The Pragmatic Programmer: From Journeyman to Master
   485 Unix Power Tools
   172 Unix Power Tools</pre>
   486       </pre>
   173 <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>
   487       <p id="ch10session4_b">
   174 <pre class="programlisting">
   488        <code class="literal">
   175 $ uniq -u items-sorted.txt
   489         uniq -u
       
   490        </code>
       
   491        command gives the lines which are unique and do not have any duplicates in the file.
       
   492        <code class="literal">
       
   493         uniq -d
       
   494        </code>
       
   495        outputs only those lines which have duplicates. The
       
   496        <code class="literal">
       
   497         -c
       
   498        </code>
       
   499        option displays the number of times each line occurs in the file.
       
   500       </p>
       
   501       <pre class="programlisting">
       
   502        $ uniq -u items-sorted.txt
       
   503 Compilers: Principles, Techniques, and Tools
   176 Compilers: Principles, Techniques, and Tools
   504 Introduction to Algorithms
   177 Introduction to Algorithms
   505 Structure and Interpretation of Computer Programs
   178 Structure and Interpretation of Computer Programs
   506 The Art of Computer Programming
   179 The Art of Computer Programming
   507 The Mythical Man Month: Essays on Software Engineering
   180 The Mythical Man Month: Essays on Software Engineering
   509 Unix Power Tools
   182 Unix Power Tools
   510 
   183 
   511 $ uniq -dc items-sorted.txt
   184 $ uniq -dc items-sorted.txt
   512 5 Programming Pearls
   185 5 Programming Pearls
   513 3 The Art of UNIX Programming
   186 3 The Art of UNIX Programming
   514 3 The C Programming Language
   187 3 The C Programming Language</pre>
   515       </pre>
   188 </div>
   516      </div>
   189 <div class="section" title="1.3.join">
   517      <div class="section" title="1.3.join">
   190 <div class="titlepage"><div><div><h3 class="title">
   518       <div class="titlepage">
   191 <a name="id2824700"></a>1.3.<code class="literal">join</code>
   519        <div>
   192 </h3></div></div></div>
   520         <div>
   193 <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>
   521          <h3 class="title">
   194 <pre class="programlisting">
   522           <a name="id2552862">
   195 Richard Stallman%http://www.stallman.org
   523           </a>
       
   524           1.3.
       
   525           <code class="literal">
       
   526            join
       
   527           </code>
       
   528          </h3>
       
   529         </div>
       
   530        </div>
       
   531       </div>
       
   532       <p id="ch10session4_c">
       
   533        Now suppose we had the file
       
   534        <code class="literal">
       
   535         stalwarts1.txt
       
   536        </code>
       
   537        , which lists the home pages of all the people listed in
       
   538        <code class="literal">
       
   539         stalwarts.txt
       
   540        </code>
       
   541        .
       
   542       </p>
       
   543       <pre class="programlisting">
       
   544        Richard Stallman%http://www.stallman.org
       
   545 Eric Raymond%http://www.catb.org/~esr/
   196 Eric Raymond%http://www.catb.org/~esr/
   546 Ian Murdock%http://ianmurdock.com/
   197 Ian Murdock%http://ianmurdock.com/
   547 Lawrence Lessig%http://lessig.org
   198 Lawrence Lessig%http://lessig.org
   548 Linus Torvalds%http://torvalds-family.blogspot.com/
   199 Linus Torvalds%http://torvalds-family.blogspot.com/
   549 Guido van Rossum%http://www.python.org/~guido/
   200 Guido van Rossum%http://www.python.org/~guido/
   550 Larry Wall%http://www.wall.org/~larry/
   201 Larry Wall%http://www.wall.org/~larry/</pre>
   551       </pre>
   202 <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>
   552       <p id="ch10session4_d">
   203 <pre class="programlisting">
   553        It would be nice to have a single file with the information in both the files. To achieve this we use the
   204 $ join stalwarts.txt stalwarts1.txt -t %
   554        <code class="literal">
       
   555         join
       
   556        </code>
       
   557        command.
       
   558       </p>
       
   559       <pre class="programlisting">
       
   560        $ join stalwarts.txt stalwarts1.txt -t %
       
   561 Richard Stallman%rms%GNU Project%http://www.stallman.org
   205 Richard Stallman%rms%GNU Project%http://www.stallman.org
   562 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
   206 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
   563 Ian Murdock% %Debian%http://ianmurdock.com/
   207 Ian Murdock% %Debian%http://ianmurdock.com/
   564 Lawrence Lessig% %Creative Commons%http://lessig.org
   208 Lawrence Lessig% %Creative Commons%http://lessig.org
   565 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
   209 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
   566 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
   210 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
   567 Larry Wall% %Perl%http://www.wall.org/~larry/
   211 Larry Wall% %Perl%http://www.wall.org/~larry/</pre>
   568       </pre>
   212 <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>
   569       <p id="ch10session4_e">
   213 <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>
   570        The
   214 <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>
   571        <code class="literal">
   215 <pre class="programlisting">
   572         join
   216 $ join -2 2 stalwarts.txt stalwarts2.txt -t %
   573        </code>
       
   574        command joins the two files, based on the common field present in both the files, which is the name, in this case.
       
   575       </p>
       
   576       <p id="ch10session4_f">
       
   577        The
       
   578        <code class="literal">
       
   579         -t
       
   580        </code>
       
   581        option again specifies the delimiting character. Unless that is specified, join assumes that the fields are separated by spaces.
       
   582       </p>
       
   583       <p id="ch10session4_10">
       
   584        Note that, for
       
   585        <code class="literal">
       
   586         join
       
   587        </code>
       
   588        to work, the common field should be in the same order in both the files. If this is not so, you could use
       
   589        <code class="literal">
       
   590         sort
       
   591        </code>
       
   592        , 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
       
   593        <code class="literal">
       
   594         -1
       
   595        </code>
       
   596        and
       
   597        <code class="literal">
       
   598         -2
       
   599        </code>
       
   600        options to specify the field to be used for joining the files.
       
   601       </p>
       
   602       <pre class="programlisting">
       
   603        $ join -2 2 stalwarts.txt stalwarts2.txt -t %
       
   604 Richard Stallman%rms%GNU Project%http://www.stallman.org
   217 Richard Stallman%rms%GNU Project%http://www.stallman.org
   605 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
   218 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
   606 Ian Murdock% %Debian%http://ianmurdock.com/
   219 Ian Murdock% %Debian%http://ianmurdock.com/
   607 Lawrence Lessig% %Creative Commons%http://lessig.org
   220 Lawrence Lessig% %Creative Commons%http://lessig.org
   608 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
   221 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
   609 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
   222 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
   610 Larry Wall% %Perl%http://www.wall.org/~larry/
   223 Larry Wall% %Perl%http://www.wall.org/~larry/</pre>
   611       </pre>
   224 </div>
   612      </div>
   225 </div>
   613     </div>
   226 <div class="section" title="2.Generating a word frequency list">
   614     <div class="section" title="2.Generating a word frequency list">
   227 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   615      <div class="titlepage">
   228 <a name="id2824800"></a>2.Generating a word frequency list</h2></div></div></div>
   616       <div>
   229 <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>
   617        <div>
   230 <p id="ch10session4_12">The basic steps to achieve this task would be -</p>
   618         <h2 class="title" style="clear: both">
   231 <div class="orderedlist"><ol class="orderedlist" type="1">
   619          <a name="id2552962">
   232 <li class="listitem"><p id="ch10session4_13">Eliminate the punctuation and spaces from the document.</p></li>
   620          </a>
   233 <li class="listitem"><p id="ch10session4_14">Generate a list of words.</p></li>
   621          2.Generating a word frequency list
   234 <li class="listitem"><p id="ch10session4_15">Count the words.</p></li>
   622         </h2>
   235 </ol></div>
   623        </div>
   236 <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>
   624       </div>
   237 <pre class="programlisting">
   625      </div>
   238 $ grep "[A-Za-z]*" alice-in-wonderland.txt</pre>
   626      <p id="ch10session4_11">
   239 <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>
   627       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.
   240 <pre class="programlisting">
   628      </p>
   241 $ grep "[A-Za-z]*" -o alice-in-wonderland.txt</pre>
   629      <p id="ch10session4_12">
   242 <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>
   630       The basic steps to achieve this task would be -
   243 <pre class="programlisting">
   631      </p>
   244 $ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c</pre>
   632      <div class="orderedlist">
   245 <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>
   633       <ol class="orderedlist" type="1">
   246 <pre class="programlisting">
   634        <li class="listitem">
   247 $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c</pre>
   635         <p id="ch10session4_13">
   248 <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>
   636          Eliminate the punctuation and spaces from the document.
   249 <pre class="programlisting">
   637         </p>
   250 $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr</pre>
   638        </li>
   251 </div>
   639        <li class="listitem">
   252 <div class="section" title="3.Basic editing and editors">
   640         <p id="ch10session4_14">
   253 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   641          Generate a list of words.
   254 <a name="id2824947"></a>3.Basic editing and editors</h2></div></div></div>
   642         </p>
   255 <div class="section" title="3.1.vim">
   643        </li>
   256 <div class="titlepage"><div><div><h3 class="title">
   644        <li class="listitem">
   257 <a name="id2824956"></a>3.1.vim</h3></div></div></div>
   645         <p id="ch10session4_15">
   258 <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>
   646          Count the words.
   259 <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>
   647         </p>
   260 <pre class="programlisting">
   648        </li>
   261 $ vim first.txt</pre>
   649       </ol>
   262 <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>
   650      </div>
   263 <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>
   651      <p id="ch10session4_16">
   264 <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>
   652       We first use
   265 <p id="ch10session4_20">To save a file and continue editing, use <code class="literal">:w FILENAME</code>
   653       <code class="literal">
   266 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>
   654        grep
   267 <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>
   655       </code>
   268 <p id="ch10session4_22">To save and quit, use <code class="literal">:wq</code></p>
   656       and some elementary
   269 <p id="ch10session4_23">To quit, use <code class="literal">:q</code></p>
   657       <code class="literal">
   270 <p id="ch10session4_24">To quit without saving, use <code class="literal">:q!</code></p>
   658        regex
   271 <div class="section" title="3.1.1.Moving around">
   659       </code>
   272 <div class="titlepage"><div><div><h4 class="title">
   660       to eliminate the non-alpha-characters.
   273 <a name="id2825077"></a>3.1.1.Moving around</h4></div></div></div>
   661      </p>
   274 <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>
   662      <pre class="programlisting">
   275 <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>
   663       $ grep "[A-Za-z]*" alice-in-wonderland.txt
   276 <pre class="programlisting">
   664      </pre>
   277       ^
   665      <p id="ch10session4_17">
       
   666       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.
       
   667       <code class="literal">
       
   668        man grep
       
   669       </code>
       
   670       shows us the
       
   671       <code class="literal">
       
   672        -o
       
   673       </code>
       
   674       option for outputting only the text which matches the regular expression.
       
   675      </p>
       
   676      <pre class="programlisting">
       
   677       $ grep "[A-Za-z]*" -o alice-in-wonderland.txt
       
   678      </pre>
       
   679      <p id="ch10session4_18">
       
   680       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
       
   681       <code class="literal">
       
   682        sort
       
   683       </code>
       
   684       and
       
   685       <code class="literal">
       
   686        uniq
       
   687       </code>
       
   688       commands. We pipe the output from the
       
   689       <code class="literal">
       
   690        grep
       
   691       </code>
       
   692       to the
       
   693       <code class="literal">
       
   694        sort
       
   695       </code>
       
   696       and then pipe it's output to
       
   697       <code class="literal">
       
   698        uniq
       
   699       </code>
       
   700       .
       
   701      </p>
       
   702      <pre class="programlisting">
       
   703       $ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c
       
   704      </pre>
       
   705      <p id="ch10session4_19">
       
   706       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
       
   707       <code class="literal">
       
   708        tr
       
   709       </code>
       
   710       command.
       
   711      </p>
       
   712      <pre class="programlisting">
       
   713       $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c
       
   714      </pre>
       
   715      <p id="ch10session4_1a">
       
   716       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
       
   717       <code class="literal">
       
   718        uniq
       
   719       </code>
       
   720       command with
       
   721       <code class="literal">
       
   722        -n
       
   723       </code>
       
   724       and
       
   725       <code class="literal">
       
   726        -r
       
   727       </code>
       
   728       options, to get the desired output.
       
   729      </p>
       
   730      <pre class="programlisting">
       
   731       $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
       
   732      </pre>
       
   733     </div>
       
   734     <div class="section" title="3.Basic editing and editors">
       
   735      <div class="titlepage">
       
   736       <div>
       
   737        <div>
       
   738         <h2 class="title" style="clear: both">
       
   739          <a name="id2553109">
       
   740          </a>
       
   741          3.Basic editing and editors
       
   742         </h2>
       
   743        </div>
       
   744       </div>
       
   745      </div>
       
   746      <div class="section" title="3.1.vim">
       
   747       <div class="titlepage">
       
   748        <div>
       
   749         <div>
       
   750          <h3 class="title">
       
   751           <a name="id2553118">
       
   752           </a>
       
   753           3.1.vim
       
   754          </h3>
       
   755         </div>
       
   756        </div>
       
   757       </div>
       
   758       <p id="ch10session4_1b">
       
   759        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.
       
   760       </p>
       
   761       <p id="ch10session4_1c">
       
   762        To open a file in vim, we pass the filename as a parameter to the
       
   763        <code class="literal">
       
   764         vim
       
   765        </code>
       
   766        command. If a file with that filename does not exist, a new file is created.
       
   767       </p>
       
   768       <pre class="programlisting">
       
   769        $ vim first.txt
       
   770       </pre>
       
   771       <p id="ch10session4_1d">
       
   772        To start inserting text into the new file that we have opened, we need to press the
       
   773        <code class="literal">
       
   774         i
       
   775        </code>
       
   776        key. This will take us into the
       
   777        <span class="emphasis">
       
   778         <em>
       
   779          insert
       
   780         </em>
       
   781        </span>
       
   782        mode from the
       
   783        <span class="emphasis">
       
   784         <em>
       
   785          command
       
   786         </em>
       
   787        </span>
       
   788        mode. Hitting the
       
   789        <code class="literal">
       
   790         esc
       
   791        </code>
       
   792        key, will bring us back to the
       
   793        <span class="emphasis">
       
   794         <em>
       
   795          command
       
   796         </em>
       
   797        </span>
       
   798        mode. There is also another mode of vim, called the
       
   799        <span class="emphasis">
       
   800         <em>
       
   801          visual
       
   802         </em>
       
   803        </span>
       
   804        mode which will be discussed later in the course.
       
   805       </p>
       
   806       <p id="ch10session4_1e">
       
   807        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.
       
   808       </p>
       
   809       <p id="ch10session4_1f">
       
   810        To save the file, use
       
   811        <code class="literal">
       
   812         :w
       
   813        </code>
       
   814        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.
       
   815       </p>
       
   816       <p id="ch10session4_20">
       
   817        To save a file and continue editing, use
       
   818        <code class="literal">
       
   819         :w FILENAME
       
   820        </code>
       
   821        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.
       
   822       </p>
       
   823       <p id="ch10session4_21">
       
   824        To save file with a new name and continue editing the new file, use
       
   825        <code class="literal">
       
   826         :saveas FILENAME
       
   827        </code>
       
   828       </p>
       
   829       <p id="ch10session4_22">
       
   830        To save and quit, use
       
   831        <code class="literal">
       
   832         :wq
       
   833        </code>
       
   834       </p>
       
   835       <p id="ch10session4_23">
       
   836        To quit, use
       
   837        <code class="literal">
       
   838         :q
       
   839        </code>
       
   840       </p>
       
   841       <p id="ch10session4_24">
       
   842        To quit without saving, use
       
   843        <code class="literal">
       
   844         :q!
       
   845        </code>
       
   846       </p>
       
   847       <div class="section" title="3.1.1.Moving around">
       
   848        <div class="titlepage">
       
   849         <div>
       
   850          <div>
       
   851           <h4 class="title">
       
   852            <a name="id2553239">
       
   853            </a>
       
   854            3.1.1.Moving around
       
   855           </h4>
       
   856          </div>
       
   857         </div>
       
   858        </div>
       
   859        <p id="ch10session4_25">
       
   860         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.
       
   861        </p>
       
   862        <p id="ch10session4_26">
       
   863         The basic cursor movement can be achieved using the keys,
       
   864         <code class="literal">
       
   865          h
       
   866         </code>
       
   867         (left),
       
   868         <code class="literal">
       
   869          l
       
   870         </code>
       
   871         (right),
       
   872         <code class="literal">
       
   873          k
       
   874         </code>
       
   875         (up) and
       
   876         <code class="literal">
       
   877          j
       
   878         </code>
       
   879         (down).
       
   880        </p>
       
   881        <pre class="programlisting">
       
   882         ^
       
   883       k
   278       k
   884 &lt; h       l &gt;
   279 &lt; h       l &gt;
   885       j
   280       j
   886       v
   281       v</pre>
   887        </pre>
   282 <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>
   888        <p id="ch10session4_27">
   283 <div class="section" title="3.1.1.1.Moving within a line">
   889         Note: Most commands can be prefixed with a number, to repeat the command. For instance,
   284 <div class="titlepage"><div><div><h5 class="title">
   890         <code class="literal">
   285 <a name="id2825129"></a>3.1.1.1.Moving within a line</h5></div></div></div>
   891          10j
   286 <p id="ch10session4_28">Cursor Movement</p>
   892         </code>
   287 <p id="ch10session4_29">Command</p>
   893         will move the cursor down 10 lines.
   288 <p id="ch10session4_2a">Forward by a screenful of text</p>
   894        </p>
   289 <p id="ch10session4_2b"><code class="literal">C-f</code></p>
   895        <div class="section" title="3.1.1.1.Moving within a line">
   290 <p id="ch10session4_2c">Backward by a screenful of text</p>
   896         <div class="titlepage">
   291 <p id="ch10session4_2d"><code class="literal">C-b</code></p>
   897          <div>
   292 <p id="ch10session4_2e">Beginning of the screen</p>
   898           <div>
   293 <p id="ch10session4_2f"><code class="literal">H</code></p>
   899            <h5 class="title">
   294 <p id="ch10session4_30">Middle of the screen</p>
   900             <a name="id2553291">
   295 <p id="ch10session4_31"><code class="literal">M</code></p>
   901             </a>
   296 <p id="ch10session4_32">End of the screen</p>
   902             3.1.1.1.Moving within a line
   297 <p id="ch10session4_33"><code class="literal">L</code></p>
   903            </h5>
   298 <p id="ch10session4_34">End of file</p>
   904           </div>
   299 <p id="ch10session4_35"><code class="literal">G</code></p>
   905          </div>
   300 <p id="ch10session4_36">Line number <code class="literal">num</code></p>
   906         </div>
   301 <p id="ch10session4_37"><code class="literal">[num]G</code></p>
   907         <p id="ch10session4_28">
   302 <p id="ch10session4_38">Beginning of file</p>
   908          Cursor Movement
   303 <p id="ch10session4_39"><code class="literal">gg</code></p>
   909         </p>
   304 <p id="ch10session4_3a">Next occurrence of the text
   910         <p id="ch10session4_29">
   305 under the cursor</p>
   911          Command
   306 <p id="ch10session4_3b"><code class="literal">*</code></p>
   912         </p>
   307 <p id="ch10session4_3c">Previous occurrence of the text
   913         <p id="ch10session4_2a">
   308 under the cursor</p>
   914          Forward by a screenful of text
   309 <p id="ch10session4_3d"><code class="literal">#</code></p>
   915         </p>
   310 <p id="ch10session4_3e">Note: <code class="literal">C-x</code> is <code class="literal">Ctrl</code> + <code class="literal">x</code></p>
   916         <p id="ch10session4_2b">
   311 </div>
   917          <code class="literal">
   312 </div>
   918           C-f
   313 <div class="section" title="3.1.2.The visual mode">
   919          </code>
   314 <div class="titlepage"><div><div><h4 class="title">
   920         </p>
   315 <a name="id2825272"></a>3.1.2.The visual mode</h4></div></div></div>
   921         <p id="ch10session4_2c">
   316 <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>
   922          Backward by a screenful of text
   317 </div>
   923         </p>
   318 <div class="section" title="3.1.3.Editing commands">
   924         <p id="ch10session4_2d">
   319 <div class="titlepage"><div><div><h4 class="title">
   925          <code class="literal">
   320 <a name="id2825290"></a>3.1.3.Editing commands</h4></div></div></div>
   926           C-b
   321 <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>
   927          </code>
   322 <p id="ch10session4_41">Editing effect</p>
   928         </p>
   323 <p id="ch10session4_42">Command</p>
   929         <p id="ch10session4_2e">
   324 <p id="ch10session4_43">Cut the character under the cursor</p>
   930          Beginning of the screen
   325 <p id="ch10session4_44"><code class="literal">x</code></p>
   931         </p>
   326 <p id="ch10session4_45">Replace the character under the
   932         <p id="ch10session4_2f">
   327 cursor with <code class="literal">a</code></p>
   933          <code class="literal">
   328 <p id="ch10session4_46"><code class="literal">ra</code></p>
   934           H
   329 <p id="ch10session4_47">Cut an entire line</p>
   935          </code>
   330 <p id="ch10session4_48"><code class="literal">dd</code></p>
   936         </p>
   331 <p id="ch10session4_49">Copy/yank an entire line</p>
   937         <p id="ch10session4_30">
   332 <p id="ch10session4_4a"><code class="literal">yy</code></p>
   938          Middle of the screen
   333 <p id="ch10session4_4b">Note: You can prefix numbers to any of the commands, to repeat them.</p>
   939         </p>
   334 </div>
   940         <p id="ch10session4_31">
   335 <div class="section" title="3.1.4.Undo and Redo">
   941          <code class="literal">
   336 <div class="titlepage"><div><div><h4 class="title">
   942           M
   337 <a name="id2825370"></a>3.1.4.Undo and Redo</h4></div></div></div>
   943          </code>
   338 <p id="ch10session4_4c">You can undo almost anything using <code class="literal">u</code>.</p>
   944         </p>
   339 <p id="ch10session4_4d">To undo the undo command type <code class="literal">C-r</code></p>
   945         <p id="ch10session4_32">
   340 </div>
   946          End of the screen
   341 <div class="section" title="3.1.5.Searching and Replacing">
   947         </p>
   342 <div class="titlepage"><div><div><h4 class="title">
   948         <p id="ch10session4_33">
   343 <a name="id2825395"></a>3.1.5.Searching and Replacing</h4></div></div></div>
   949          <code class="literal">
   344 <p id="ch10session4_4e">Finding and Replacing</p>
   950           L
   345 <p id="ch10session4_4f">Command</p>
   951          </code>
   346 <p id="ch10session4_50">Replace the first instance of <code class="literal">old</code>
   952         </p>
   347 with <code class="literal">new</code> in the current line.</p>
   953         <p id="ch10session4_34">
   348 <p id="ch10session4_51"><code class="literal">:s/old/new</code></p>
   954          End of file
   349 <p id="ch10session4_52">Replace all instances of <code class="literal">old</code>
   955         </p>
   350 with <code class="literal">new</code> in the current line.</p>
   956         <p id="ch10session4_35">
   351 <p id="ch10session4_53"><code class="literal">:s/old/new/g</code></p>
   957          <code class="literal">
   352 <p id="ch10session4_54">Replace all instances of <code class="literal">old</code>
   958           G
   353 with <code class="literal">new</code> in the current line,
   959          </code>
   354 but ask for confirmation each time.</p>
   960         </p>
   355 <p id="ch10session4_55"><code class="literal">:s/old/new/gc</code></p>
   961         <p id="ch10session4_36">
   356 <p id="ch10session4_56">Replace the first instance of <code class="literal">old</code>
   962          Line number
   357 with <code class="literal">new</code> in the entire file.</p>
   963          <code class="literal">
   358 <p id="ch10session4_57"><code class="literal">:%s/old/new</code></p>
   964           num
   359 <p id="ch10session4_58">Replace all instances of <code class="literal">old</code>
   965          </code>
   360 with <code class="literal">new</code> in the entire file.</p>
   966         </p>
   361 <p id="ch10session4_59"><code class="literal">:%s/old/new/g</code></p>
   967         <p id="ch10session4_37">
   362 <p id="ch10session4_5a">Replace all instances of <code class="literal">old</code> with
   968          <code class="literal">
   363 <code class="literal">new</code> in the entire file but ask
   969           [num]G
   364 for confirmation each time.</p>
   970          </code>
   365 <p id="ch10session4_5b"><code class="literal">:%s/old/new/gc</code></p>
   971         </p>
   366 </div>
   972         <p id="ch10session4_38">
   367 </div>
   973          Beginning of file
   368 <div class="section" title="3.2.SciTE">
   974         </p>
   369 <div class="titlepage"><div><div><h3 class="title">
   975         <p id="ch10session4_39">
   370 <a name="id2825522"></a>3.2.SciTE</h3></div></div></div>
   976          <code class="literal">
   371 <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>
   977           gg
   372 <p id="ch10session4_5d">Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice.</p>
   978          </code>
   373 <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>
   979         </p>
   374 <p id="ch10session4_5f">SciTE also gives you the option to (compile and) run your code, from within the editor.</p>
   980         <p id="ch10session4_3a">
   375 </div>
   981          Next occurrence of the text
   376 </div>
   982 under the cursor
   377 <div class="section" title="4.Personalizing your Environment">
   983         </p>
   378 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   984         <p id="ch10session4_3b">
   379 <a name="id2825567"></a>4.Personalizing your Environment</h2></div></div></div>
   985          <code class="literal">
   380 <div class="section" title="4.1..bashrc">
   986           *
   381 <div class="titlepage"><div><div><h3 class="title">
   987          </code>
   382 <a name="id2825576"></a>4.1..bashrc</h3></div></div></div>
   988         </p>
   383 <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>
   989         <p id="ch10session4_3c">
   384 <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>
   990          Previous occurrence of the text
   385 <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>
   991 under the cursor
   386 <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>
   992         </p>
   387 <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>
   993         <p id="ch10session4_3d">
   388 </div>
   994          <code class="literal">
   389 <div class="section" title="4.2..vimrc">
   995           #
   390 <div class="titlepage"><div><div><h3 class="title">
   996          </code>
   391 <a name="id2825876"></a>4.2..vimrc</h3></div></div></div>
   997         </p>
   392 <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>
   998         <p id="ch10session4_3e">
   393 <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>
   999          Note:
   394 <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>
  1000          <code class="literal">
   395 <p id="ch10session4_68">Command</p>
  1001           C-x
   396 <p id="ch10session4_69">Vim action</p>
  1002          </code>
   397 <p id="ch10session4_6a"><code class="literal">set nocompatible</code></p>
  1003          is
   398 <p id="ch10session4_6b">Explicitly disable compatibility with vi</p>
  1004          <code class="literal">
   399 <p id="ch10session4_6c"><code class="literal">set backspace=indent,eol,start</code></p>
  1005           Ctrl
   400 <p id="ch10session4_6d">In the insert mode, vim allows the backspace key to delete white spaces at the
  1006          </code>
   401 start of line, line breaks and the character before which insert mode started.</p>
  1007          +
   402 <p id="ch10session4_6e">set autoindent</p>
  1008          <code class="literal">
   403 <p id="ch10session4_6f">Vim indents a new line with the same indentation of the previous line.</p>
  1009           x
   404 <p id="ch10session4_70">set backup</p>
  1010          </code>
   405 <p id="ch10session4_71">Vim keeps a backup copy of a file when overwriting it.</p>
  1011         </p>
   406 <p id="ch10session4_72">set history=50</p>
  1012        </div>
   407 <p id="ch10session4_73">Vim keeps 50 commands and 50 search patterns in the history.</p>
  1013       </div>
   408 <p id="ch10session4_74">set ruler</p>
  1014       <div class="section" title="3.1.2.The visual mode">
   409 <p id="ch10session4_75">Displays the current cursor position in the lower right corner of the vim window.</p>
  1015        <div class="titlepage">
   410 <p id="ch10session4_76">set showcmd</p>
  1016         <div>
   411 <p id="ch10session4_77">Displays the incomplete command in the lower right corner.</p>
  1017          <div>
   412 <p id="ch10session4_78">set incsearch</p>
  1018           <h4 class="title">
   413 <p id="ch10session4_79">Turns on incremental searching. Displays search results while you type.</p>
  1019            <a name="id2553434">
   414 <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>
  1020            </a>
   415 <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>
  1021            3.1.2.The visual mode
   416 </div>
  1022           </h4>
   417 </div>
  1023          </div>
   418 <div class="section" title="5.Subshells and source">
  1024         </div>
   419 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1025        </div>
   420 <a name="id2826095"></a>5.Subshells and <code class="literal">source</code>
  1026        <p id="ch10session4_3f">
   421 </h2></div></div></div>
  1027         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.
   422 <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>
  1028        </p>
   423 <div class="itemizedlist"><ul class="itemizedlist" type="*">
  1029       </div>
   424 <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>
  1030       <div class="section" title="3.1.3.Editing commands">
   425 <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>
  1031        <div class="titlepage">
   426 <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>
  1032         <div>
   427 </ul></div>
  1033          <div>
   428 <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>
  1034           <h4 class="title">
   429 <pre class="programlisting">
  1035            <a name="id2553452">
   430 $ source script.sh</pre>
  1036            </a>
   431 <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>
  1037            3.1.3.Editing commands
   432 </div>
  1038           </h4>
   433 </div>
  1039          </div>
   434 </div></body>
  1040         </div>
       
  1041        </div>
       
  1042        <p id="ch10session4_40">
       
  1043         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.
       
  1044        </p>
       
  1045        <p id="ch10session4_41">
       
  1046         Editing effect
       
  1047        </p>
       
  1048        <p id="ch10session4_42">
       
  1049         Command
       
  1050        </p>
       
  1051        <p id="ch10session4_43">
       
  1052         Cut the character under the cursor
       
  1053        </p>
       
  1054        <p id="ch10session4_44">
       
  1055         <code class="literal">
       
  1056          x
       
  1057         </code>
       
  1058        </p>
       
  1059        <p id="ch10session4_45">
       
  1060         Replace the character under the
       
  1061 cursor with
       
  1062         <code class="literal">
       
  1063          a
       
  1064         </code>
       
  1065        </p>
       
  1066        <p id="ch10session4_46">
       
  1067         <code class="literal">
       
  1068          ra
       
  1069         </code>
       
  1070        </p>
       
  1071        <p id="ch10session4_47">
       
  1072         Cut an entire line
       
  1073        </p>
       
  1074        <p id="ch10session4_48">
       
  1075         <code class="literal">
       
  1076          dd
       
  1077         </code>
       
  1078        </p>
       
  1079        <p id="ch10session4_49">
       
  1080         Copy/yank an entire line
       
  1081        </p>
       
  1082        <p id="ch10session4_4a">
       
  1083         <code class="literal">
       
  1084          yy
       
  1085         </code>
       
  1086        </p>
       
  1087        <p id="ch10session4_4b">
       
  1088         Note: You can prefix numbers to any of the commands, to repeat them.
       
  1089        </p>
       
  1090       </div>
       
  1091       <div class="section" title="3.1.4.Undo and Redo">
       
  1092        <div class="titlepage">
       
  1093         <div>
       
  1094          <div>
       
  1095           <h4 class="title">
       
  1096            <a name="id2553532">
       
  1097            </a>
       
  1098            3.1.4.Undo and Redo
       
  1099           </h4>
       
  1100          </div>
       
  1101         </div>
       
  1102        </div>
       
  1103        <p id="ch10session4_4c">
       
  1104         You can undo almost anything using
       
  1105         <code class="literal">
       
  1106          u
       
  1107         </code>
       
  1108         .
       
  1109        </p>
       
  1110        <p id="ch10session4_4d">
       
  1111         To undo the undo command type
       
  1112         <code class="literal">
       
  1113          C-r
       
  1114         </code>
       
  1115        </p>
       
  1116       </div>
       
  1117       <div class="section" title="3.1.5.Searching and Replacing">
       
  1118        <div class="titlepage">
       
  1119         <div>
       
  1120          <div>
       
  1121           <h4 class="title">
       
  1122            <a name="id2553557">
       
  1123            </a>
       
  1124            3.1.5.Searching and Replacing
       
  1125           </h4>
       
  1126          </div>
       
  1127         </div>
       
  1128        </div>
       
  1129        <p id="ch10session4_4e">
       
  1130         Finding and Replacing
       
  1131        </p>
       
  1132        <p id="ch10session4_4f">
       
  1133         Command
       
  1134        </p>
       
  1135        <p id="ch10session4_50">
       
  1136         Replace the first instance of
       
  1137         <code class="literal">
       
  1138          old
       
  1139         </code>
       
  1140         with
       
  1141         <code class="literal">
       
  1142          new
       
  1143         </code>
       
  1144         in the current line.
       
  1145        </p>
       
  1146        <p id="ch10session4_51">
       
  1147         <code class="literal">
       
  1148          :s/old/new
       
  1149         </code>
       
  1150        </p>
       
  1151        <p id="ch10session4_52">
       
  1152         Replace all instances of
       
  1153         <code class="literal">
       
  1154          old
       
  1155         </code>
       
  1156         with
       
  1157         <code class="literal">
       
  1158          new
       
  1159         </code>
       
  1160         in the current line.
       
  1161        </p>
       
  1162        <p id="ch10session4_53">
       
  1163         <code class="literal">
       
  1164          :s/old/new/g
       
  1165         </code>
       
  1166        </p>
       
  1167        <p id="ch10session4_54">
       
  1168         Replace all instances of
       
  1169         <code class="literal">
       
  1170          old
       
  1171         </code>
       
  1172         with
       
  1173         <code class="literal">
       
  1174          new
       
  1175         </code>
       
  1176         in the current line,
       
  1177 but ask for confirmation each time.
       
  1178        </p>
       
  1179        <p id="ch10session4_55">
       
  1180         <code class="literal">
       
  1181          :s/old/new/gc
       
  1182         </code>
       
  1183        </p>
       
  1184        <p id="ch10session4_56">
       
  1185         Replace the first instance of
       
  1186         <code class="literal">
       
  1187          old
       
  1188         </code>
       
  1189         with
       
  1190         <code class="literal">
       
  1191          new
       
  1192         </code>
       
  1193         in the entire file.
       
  1194        </p>
       
  1195        <p id="ch10session4_57">
       
  1196         <code class="literal">
       
  1197          :%s/old/new
       
  1198         </code>
       
  1199        </p>
       
  1200        <p id="ch10session4_58">
       
  1201         Replace all instances of
       
  1202         <code class="literal">
       
  1203          old
       
  1204         </code>
       
  1205         with
       
  1206         <code class="literal">
       
  1207          new
       
  1208         </code>
       
  1209         in the entire file.
       
  1210        </p>
       
  1211        <p id="ch10session4_59">
       
  1212         <code class="literal">
       
  1213          :%s/old/new/g
       
  1214         </code>
       
  1215        </p>
       
  1216        <p id="ch10session4_5a">
       
  1217         Replace all instances of
       
  1218         <code class="literal">
       
  1219          old
       
  1220         </code>
       
  1221         with
       
  1222         <code class="literal">
       
  1223          new
       
  1224         </code>
       
  1225         in the entire file but ask
       
  1226 for confirmation each time.
       
  1227        </p>
       
  1228        <p id="ch10session4_5b">
       
  1229         <code class="literal">
       
  1230          :%s/old/new/gc
       
  1231         </code>
       
  1232        </p>
       
  1233       </div>
       
  1234      </div>
       
  1235      <div class="section" title="3.2.SciTE">
       
  1236       <div class="titlepage">
       
  1237        <div>
       
  1238         <div>
       
  1239          <h3 class="title">
       
  1240           <a name="id2553684">
       
  1241           </a>
       
  1242           3.2.SciTE
       
  1243          </h3>
       
  1244         </div>
       
  1245        </div>
       
  1246       </div>
       
  1247       <p id="ch10session4_5c">
       
  1248        SciTE is a
       
  1249        <span class="emphasis">
       
  1250         <em>
       
  1251          source code
       
  1252         </em>
       
  1253        </span>
       
  1254        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.
       
  1255       </p>
       
  1256       <p id="ch10session4_5d">
       
  1257        Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice.
       
  1258       </p>
       
  1259       <p id="ch10session4_5e">
       
  1260        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.
       
  1261       </p>
       
  1262       <p id="ch10session4_5f">
       
  1263        SciTE also gives you the option to (compile and) run your code, from within the editor.
       
  1264       </p>
       
  1265      </div>
       
  1266     </div>
       
  1267     <div class="section" title="4.Personalizing your Environment">
       
  1268      <div class="titlepage">
       
  1269       <div>
       
  1270        <div>
       
  1271         <h2 class="title" style="clear: both">
       
  1272          <a name="id2553729">
       
  1273          </a>
       
  1274          4.Personalizing your Environment
       
  1275         </h2>
       
  1276        </div>
       
  1277       </div>
       
  1278      </div>
       
  1279      <div class="section" title="4.1..bashrc">
       
  1280       <div class="titlepage">
       
  1281        <div>
       
  1282         <div>
       
  1283          <h3 class="title">
       
  1284           <a name="id2553738">
       
  1285           </a>
       
  1286           4.1..bashrc
       
  1287          </h3>
       
  1288         </div>
       
  1289        </div>
       
  1290       </div>
       
  1291       <p id="ch10session4_60">
       
  1292        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.
       
  1293       </p>
       
  1294       <p id="ch10session4_61">
       
  1295        When bash starts up as an interactive login shell, it reads the files
       
  1296        <code class="literal">
       
  1297         /etc/profile
       
  1298        </code>
       
  1299        ,
       
  1300        <code class="literal">
       
  1301         ~/.bash_profile
       
  1302        </code>
       
  1303        ,
       
  1304        <code class="literal">
       
  1305         ~/.bash_login
       
  1306        </code>
       
  1307        , and
       
  1308        <code class="literal">
       
  1309         ~/.profile
       
  1310        </code>
       
  1311        in that order.
       
  1312       </p>
       
  1313       <p id="ch10session4_62">
       
  1314        When it is a shell that is not a login shell,
       
  1315        <code class="literal">
       
  1316         ~/.bashrc
       
  1317        </code>
       
  1318        is read and the commands in it are executed. This can be prevented using the
       
  1319        <code class="literal">
       
  1320         --norc
       
  1321        </code>
       
  1322        option. To force bash to use another file, instead of the
       
  1323        <code class="literal">
       
  1324         ~/.bashrc
       
  1325        </code>
       
  1326        file on start-up, the
       
  1327        <code class="literal">
       
  1328         --rcfile
       
  1329        </code>
       
  1330        option may be used.
       
  1331       </p>
       
  1332       <p id="ch10session4_63">
       
  1333        Now, you know what you should do, to change the current directory to you Desktop. Just put a
       
  1334        <code class="literal">
       
  1335         cd ~/Desktop
       
  1336        </code>
       
  1337        into your
       
  1338        <code class="literal">
       
  1339         ~/.bashrc
       
  1340        </code>
       
  1341        and you are set!
       
  1342       </p>
       
  1343       <p id="ch10session4_64">
       
  1344        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
       
  1345        <code class="literal">
       
  1346         .bashrc
       
  1347        </code>
       
  1348        . We shall look at them, in the next section where we look at environment variables and
       
  1349        <code class="literal">
       
  1350         set
       
  1351        </code>
       
  1352        command.
       
  1353       </p>
       
  1354      </div>
       
  1355      <div class="section" title="4.2..vimrc">
       
  1356       <div class="titlepage">
       
  1357        <div>
       
  1358         <div>
       
  1359          <h3 class="title">
       
  1360           <a name="id2554039">
       
  1361           </a>
       
  1362           4.2..vimrc
       
  1363          </h3>
       
  1364         </div>
       
  1365        </div>
       
  1366       </div>
       
  1367       <p id="ch10session4_65">
       
  1368        <code class="literal">
       
  1369         .vimrc
       
  1370        </code>
       
  1371        is a file similar to
       
  1372        <code class="literal">
       
  1373         .bashrc
       
  1374        </code>
       
  1375        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
       
  1376        <code class="literal">
       
  1377         .vimrc
       
  1378        </code>
       
  1379        file, so that they are automatically set each time vim starts. The recommended place for having your
       
  1380        <code class="literal">
       
  1381         .vimrc
       
  1382        </code>
       
  1383        is also your home directory.
       
  1384       </p>
       
  1385       <p id="ch10session4_66">
       
  1386        The file
       
  1387        <code class="literal">
       
  1388         /etc/vimrc
       
  1389        </code>
       
  1390        is the global config file and shouldn't usually be edited. You can instead edit the
       
  1391        <code class="literal">
       
  1392         ~/.vimrc
       
  1393        </code>
       
  1394        file that is present in your home folder.
       
  1395       </p>
       
  1396       <p id="ch10session4_67">
       
  1397        There are a whole bunch of variables that you could set in the
       
  1398        <code class="literal">
       
  1399         .vimrc
       
  1400        </code>
       
  1401        file. You can look at all the options available, using the
       
  1402        <code class="literal">
       
  1403         :set all
       
  1404        </code>
       
  1405        command in vim. You could use the
       
  1406        <code class="literal">
       
  1407         :help option_name
       
  1408        </code>
       
  1409        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
       
  1410        <code class="literal">
       
  1411         .vimrc
       
  1412        </code>
       
  1413        . You should also look at
       
  1414        <code class="literal">
       
  1415         :help vimrc
       
  1416        </code>
       
  1417        for more info on the
       
  1418        <code class="literal">
       
  1419         .vimrc
       
  1420        </code>
       
  1421        file. If you already have a
       
  1422        <code class="literal">
       
  1423         .vimrc
       
  1424        </code>
       
  1425        file, you can edit it from within vim, using
       
  1426        <code class="literal">
       
  1427         :e $MYVIMRC
       
  1428        </code>
       
  1429        command. We shall look at some of the most commonly used options.
       
  1430       </p>
       
  1431       <p id="ch10session4_68">
       
  1432        Command
       
  1433       </p>
       
  1434       <p id="ch10session4_69">
       
  1435        Vim action
       
  1436       </p>
       
  1437       <p id="ch10session4_6a">
       
  1438        <code class="literal">
       
  1439         set nocompatible
       
  1440        </code>
       
  1441       </p>
       
  1442       <p id="ch10session4_6b">
       
  1443        Explicitly disable compatibility with vi
       
  1444       </p>
       
  1445       <p id="ch10session4_6c">
       
  1446        <code class="literal">
       
  1447         set backspace=indent,eol,start
       
  1448        </code>
       
  1449       </p>
       
  1450       <p id="ch10session4_6d">
       
  1451        In the insert mode, vim allows the backspace key to delete white spaces at the
       
  1452 start of line, line breaks and the character before which insert mode started.
       
  1453       </p>
       
  1454       <p id="ch10session4_6e">
       
  1455        set autoindent
       
  1456       </p>
       
  1457       <p id="ch10session4_6f">
       
  1458        Vim indents a new line with the same indentation of the previous line.
       
  1459       </p>
       
  1460       <p id="ch10session4_70">
       
  1461        set backup
       
  1462       </p>
       
  1463       <p id="ch10session4_71">
       
  1464        Vim keeps a backup copy of a file when overwriting it.
       
  1465       </p>
       
  1466       <p id="ch10session4_72">
       
  1467        set history=50
       
  1468       </p>
       
  1469       <p id="ch10session4_73">
       
  1470        Vim keeps 50 commands and 50 search patterns in the history.
       
  1471       </p>
       
  1472       <p id="ch10session4_74">
       
  1473        set ruler
       
  1474       </p>
       
  1475       <p id="ch10session4_75">
       
  1476        Displays the current cursor position in the lower right corner of the vim window.
       
  1477       </p>
       
  1478       <p id="ch10session4_76">
       
  1479        set showcmd
       
  1480       </p>
       
  1481       <p id="ch10session4_77">
       
  1482        Displays the incomplete command in the lower right corner.
       
  1483       </p>
       
  1484       <p id="ch10session4_78">
       
  1485        set incsearch
       
  1486       </p>
       
  1487       <p id="ch10session4_79">
       
  1488        Turns on incremental searching. Displays search results while you type.
       
  1489       </p>
       
  1490       <p id="ch10session4_7a">
       
  1491        You can see the effect of the changes made to your
       
  1492        <code class="literal">
       
  1493         .vimrc
       
  1494        </code>
       
  1495        file by restarting vim. If you want to see the changes that you made to your
       
  1496        <code class="literal">
       
  1497         .vimrc
       
  1498        </code>
       
  1499        file immediately, you could source the file from within vim.
       
  1500       </p>
       
  1501       If the
       
  1502       <code class="literal">
       
  1503        .vimrc
       
  1504       </code>
       
  1505       file has been sourced when this instance of vim was started, you could just resource the file again::
       
  1506       <p id="ch10session4_7b">
       
  1507        :so $MYVIMRC
       
  1508       </p>
       
  1509       <p id="ch10session4_7c">
       
  1510        If you just created the
       
  1511        <code class="literal">
       
  1512         .vimrc
       
  1513        </code>
       
  1514        file or it was not sourced when you stared this instance of vim, just replace the
       
  1515        <code class="literal">
       
  1516         $MYVIMRC
       
  1517        </code>
       
  1518        variable above, with the location of the
       
  1519        <code class="literal">
       
  1520         .vimrc
       
  1521        </code>
       
  1522        file that you created/edited.
       
  1523       </p>
       
  1524      </div>
       
  1525     </div>
       
  1526     <div class="section" title="5.Subshells and source">
       
  1527      <div class="titlepage">
       
  1528       <div>
       
  1529        <div>
       
  1530         <h2 class="title" style="clear: both">
       
  1531          <a name="id2554257">
       
  1532          </a>
       
  1533          5.Subshells and
       
  1534          <code class="literal">
       
  1535           source
       
  1536          </code>
       
  1537         </h2>
       
  1538        </div>
       
  1539       </div>
       
  1540      </div>
       
  1541      <p id="ch10session4_7d">
       
  1542       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.
       
  1543      </p>
       
  1544      <div class="itemizedlist">
       
  1545       <ul class="itemizedlist" type="*">
       
  1546        <li class="listitem" style="list-style-type: *">
       
  1547         <p id="ch10session4_7e">
       
  1548          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.
       
  1549         </p>
       
  1550        </li>
       
  1551        <li class="listitem" style="list-style-type: *">
       
  1552         <p id="ch10session4_7f">
       
  1553          Any pipes being used, create a subshell. The commands on the input and output ends of the pipe are run in different subshells.
       
  1554         </p>
       
  1555        </li>
       
  1556        <li class="listitem" style="list-style-type: *">
       
  1557         <p id="ch10session4_80">
       
  1558          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.
       
  1559         </p>
       
  1560        </li>
       
  1561       </ul>
       
  1562      </div>
       
  1563      <p id="ch10session4_81">
       
  1564       To avoid creating a subshell, when running a shell script, you could use the
       
  1565       <code class="literal">
       
  1566        source
       
  1567       </code>
       
  1568       command.
       
  1569      </p>
       
  1570      <pre class="programlisting">
       
  1571       $ source script.sh
       
  1572      </pre>
       
  1573      <p id="ch10session4_82">
       
  1574       This will run the
       
  1575       <code class="literal">
       
  1576        script.sh
       
  1577       </code>
       
  1578       within the present shell without creating a subshell. The
       
  1579       <code class="literal">
       
  1580        .
       
  1581       </code>
       
  1582       command is an alias for the source command.
       
  1583       <code class="literal">
       
  1584        . script.sh
       
  1585       </code>
       
  1586       is therefore equivalent to
       
  1587       <code class="literal">
       
  1588        source script.sh
       
  1589       </code>
       
  1590       .
       
  1591      </p>
       
  1592     </div>
       
  1593    </div>
       
  1594   </div>
       
  1595  </body>
       
  1596 </html>
   435 </html>
       
   436