SEESenv/web/html/chn10session4.html
changeset 42 1f61ee485958
parent 41 e54725be4df6
child 43 134b87b382f5
equal deleted inserted replaced
41:e54725be4df6 42:1f61ee485958
     1 <html>
       
     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 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch10session4">
       
     4 <div class="titlepage"></div>
       
     5 <div class="toc">
       
     6 <p><b>Table of Contents</b></p>
       
     7 <dl>
       
     8 <dt><span class="article"><a href="#id2773576"></a></span></dt>
       
     9 <dd><dl>
       
    10 <dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
       
    11 <dd><dl>
       
    12 <dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
       
    13 <dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
       
    14 <dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
       
    15 </dl></dd>
       
    16 <dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
       
    17 <dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
       
    18 <dd><dl>
       
    19 <dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
       
    20 <dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
       
    21 </dl></dd>
       
    22 <dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
       
    23 <dd><dl>
       
    24 <dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
       
    25 <dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
       
    26 </dl></dd>
       
    27 <dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
       
    28 </dl></dd>
       
    29 </dl>
       
    30 </div>
       
    31 <div class="article">
       
    32 <div class="titlepage">
       
    33 <div>
       
    34 <div><h2 class="title" id="id2773576"></h2></div>
       
    35 <div><h2 class="title" id="id2773576"></h2></div>
       
    36 </div>
       
    37 <hr />
       
    38 </div>
       
    39 <div class="toc">
       
    40 <p><b>Table of Contents</b></p>
       
    41 <dl>
       
    42 <dt><span class="section"><a href="#id2583667">1. More text processing</a></span></dt>
       
    43 <dd><dl>
       
    44 <dt><span class="section"><a href="#id2771691">1.1. <code class="literal">sort</code></a></span></dt>
       
    45 <dt><span class="section"><a href="#id2824598">1.2. <code class="literal">uniq</code></a></span></dt>
       
    46 <dt><span class="section"><a href="#id2824700">1.3. <code class="literal">join</code></a></span></dt>
       
    47 </dl></dd>
       
    48 <dt><span class="section"><a href="#id2824800">2. Generating a word frequency list</a></span></dt>
       
    49 <dt><span class="section"><a href="#id2824947">3. Basic editing and editors</a></span></dt>
       
    50 <dd><dl>
       
    51 <dt><span class="section"><a href="#id2824956">3.1. vim</a></span></dt>
       
    52 <dt><span class="section"><a href="#id2825522">3.2. SciTE</a></span></dt>
       
    53 </dl></dd>
       
    54 <dt><span class="section"><a href="#id2825567">4. Personalizing your Environment</a></span></dt>
       
    55 <dd><dl>
       
    56 <dt><span class="section"><a href="#id2825576">4.1. .bashrc</a></span></dt>
       
    57 <dt><span class="section"><a href="#id2825876">4.2. .vimrc</a></span></dt>
       
    58 </dl></dd>
       
    59 <dt><span class="section"><a href="#id2826095">5. Subshells and <code class="literal">source</code></a></span></dt>
       
    60 </dl>
       
    61 </div>
       
    62 <div class="section" title="1.More text processing">
       
    63 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
    64 <a name="id2583667"></a>1.More text processing</h2></div></div></div>
       
    65 <div class="section" title="1.1.sort">
       
    66 <div class="titlepage"><div><div><h3 class="title">
       
    67 <a name="id2771691"></a>1.1.<code class="literal">sort</code>
       
    68 </h3></div></div></div>
       
    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 <pre class="programlisting">
       
    71 Richard Stallman%rms%GNU Project
       
    72 Eric Raymond%ESR%Jargon File
       
    73 Ian Murdock% %Debian
       
    74 Lawrence Lessig% %Creative Commons
       
    75 Linus Torvalds% %Linux Kernel
       
    76 Guido van Rossum%BDFL%Python
       
    77 Larry Wall% %Perl</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>
       
    79 <pre class="programlisting">
       
    80 $ sort stalwarts.txt
       
    81 Eric Raymond%ESR%Jargon File
       
    82 Guido van Rossum%BDFL%Python
       
    83 Ian Murdock% %Debian
       
    84 Larry Wall% %Perl
       
    85 Lawrence Lessig% %Creative Commons
       
    86 Linus Torvalds% %Linux Kernel
       
    87 Richard Stallman%rms%GNU Project</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>
       
    89 <p id="ch10session4_4">Below is an example that sorts the file based on "other" names.</p>
       
    90 <pre class="programlisting">
       
    91 $ sort -t % -k 2,2  stalwarts.txt
       
    92 
       
    93 Ian Murdock% %Debian
       
    94 Larry Wall% %Perl
       
    95 Lawrence Lessig% %Creative Commons
       
    96 Linus Torvalds% %Linux Kernel
       
    97 Guido van Rossum%BDFL%Python
       
    98 Eric Raymond%ESR%Jargon File
       
    99 Richard Stallman%rms%GNU Project</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>
       
   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>
       
   102 <pre class="programlisting">
       
   103 $ sort -t % -k 2,2 -k 3,3  stalwarts.txt
       
   104 
       
   105 Lawrence Lessig% %Creative Commons
       
   106 Ian Murdock% %Debian
       
   107 Linus Torvalds% %Linux Kernel
       
   108 Larry Wall% %Perl
       
   109 Guido van Rossum%BDFL%Python
       
   110 Eric Raymond%ESR%Jargon File
       
   111 Richard Stallman%rms%GNU Project</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>
       
   113 </div>
       
   114 <div class="section" title="1.2.uniq">
       
   115 <div class="titlepage"><div><div><h3 class="title">
       
   116 <a name="id2824598"></a>1.2.<code class="literal">uniq</code>
       
   117 </h3></div></div></div>
       
   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>
       
   119 <pre class="programlisting">
       
   120 Programming Pearls
       
   121 The C Programming Language
       
   122 The Mythical Man Month: Essays on Software Engineering
       
   123 Programming Pearls
       
   124 The C Programming Language
       
   125 Structure and Interpretation of Computer Programs
       
   126 Programming Pearls
       
   127 Compilers: Principles, Techniques, and Tools
       
   128 The C Programming Language
       
   129 The Art of UNIX Programming
       
   130 Programming Pearls
       
   131 The Art of Computer Programming
       
   132 Introduction to Algorithms
       
   133 The Art of UNIX Programming
       
   134 The Pragmatic Programmer: From Journeyman to Master
       
   135 Programming Pearls
       
   136 Unix Power Tools
       
   137 The Art of UNIX Programming</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>
       
   139 <pre class="programlisting">
       
   140 $ uniq items.txt
       
   141 Programming Pearls
       
   142 The C Programming Language
       
   143 The Mythical Man Month: Essays on Software Engineering
       
   144 Programming Pearls
       
   145 The C Programming Language
       
   146 Structure and Interpretation of Computer Programs
       
   147 Programming Pearls
       
   148 Compilers: Principles, Techniques, and Tools
       
   149 The C Programming Language
       
   150 The Art of UNIX Programming
       
   151 Programming Pearls
       
   152 The Art of Computer Programming
       
   153 Introduction to Algorithms
       
   154 The Art of UNIX Programming
       
   155 The Pragmatic Programmer: From Journeyman to Master
       
   156 Programming Pearls
       
   157 Unix Power Tools
       
   158 The Art of UNIX Programming</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>
       
   160 <pre class="programlisting">
       
   161 $ sort items.txt &gt; items-sorted.txt
       
   162 $ uniq items-sorted.txt
       
   163 Compilers: Principles, Techniques, and Tools
       
   164 Introduction to Algorithms
       
   165 Programming Pearls
       
   166 Structure and Interpretation of Computer Programs
       
   167 The Art of Computer Programming
       
   168 The Art of UNIX Programming
       
   169 The C Programming Language
       
   170 The Mythical Man Month: Essays on Software Engineering
       
   171 The Pragmatic Programmer: From Journeyman to Master
       
   172 Unix Power Tools</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>
       
   174 <pre class="programlisting">
       
   175 $ uniq -u items-sorted.txt
       
   176 Compilers: Principles, Techniques, and Tools
       
   177 Introduction to Algorithms
       
   178 Structure and Interpretation of Computer Programs
       
   179 The Art of Computer Programming
       
   180 The Mythical Man Month: Essays on Software Engineering
       
   181 The Pragmatic Programmer: From Journeyman to Master
       
   182 Unix Power Tools
       
   183 
       
   184 $ uniq -dc items-sorted.txt
       
   185 5 Programming Pearls
       
   186 3 The Art of UNIX Programming
       
   187 3 The C Programming Language</pre>
       
   188 </div>
       
   189 <div class="section" title="1.3.join">
       
   190 <div class="titlepage"><div><div><h3 class="title">
       
   191 <a name="id2824700"></a>1.3.<code class="literal">join</code>
       
   192 </h3></div></div></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>
       
   194 <pre class="programlisting">
       
   195 Richard Stallman%http://www.stallman.org
       
   196 Eric Raymond%http://www.catb.org/~esr/
       
   197 Ian Murdock%http://ianmurdock.com/
       
   198 Lawrence Lessig%http://lessig.org
       
   199 Linus Torvalds%http://torvalds-family.blogspot.com/
       
   200 Guido van Rossum%http://www.python.org/~guido/
       
   201 Larry Wall%http://www.wall.org/~larry/</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>
       
   203 <pre class="programlisting">
       
   204 $ join stalwarts.txt stalwarts1.txt -t %
       
   205 Richard Stallman%rms%GNU Project%http://www.stallman.org
       
   206 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
       
   207 Ian Murdock% %Debian%http://ianmurdock.com/
       
   208 Lawrence Lessig% %Creative Commons%http://lessig.org
       
   209 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
       
   210 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
       
   211 Larry Wall% %Perl%http://www.wall.org/~larry/</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>
       
   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>
       
   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>
       
   215 <pre class="programlisting">
       
   216 $ join -2 2 stalwarts.txt stalwarts2.txt -t %
       
   217 Richard Stallman%rms%GNU Project%http://www.stallman.org
       
   218 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
       
   219 Ian Murdock% %Debian%http://ianmurdock.com/
       
   220 Lawrence Lessig% %Creative Commons%http://lessig.org
       
   221 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
       
   222 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
       
   223 Larry Wall% %Perl%http://www.wall.org/~larry/</pre>
       
   224 </div>
       
   225 </div>
       
   226 <div class="section" title="2.Generating a word frequency list">
       
   227 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   228 <a name="id2824800"></a>2.Generating a word frequency list</h2></div></div></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>
       
   230 <p id="ch10session4_12">The basic steps to achieve this task would be -</p>
       
   231 <div class="orderedlist"><ol class="orderedlist" type="1">
       
   232 <li class="listitem"><p id="ch10session4_13">Eliminate the punctuation and spaces from the document.</p></li>
       
   233 <li class="listitem"><p id="ch10session4_14">Generate a list of words.</p></li>
       
   234 <li class="listitem"><p id="ch10session4_15">Count the words.</p></li>
       
   235 </ol></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>
       
   237 <pre class="programlisting">
       
   238 $ grep "[A-Za-z]*" alice-in-wonderland.txt</pre>
       
   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>
       
   240 <pre class="programlisting">
       
   241 $ grep "[A-Za-z]*" -o alice-in-wonderland.txt</pre>
       
   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>
       
   243 <pre class="programlisting">
       
   244 $ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c</pre>
       
   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>
       
   246 <pre class="programlisting">
       
   247 $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c</pre>
       
   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>
       
   249 <pre class="programlisting">
       
   250 $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr</pre>
       
   251 </div>
       
   252 <div class="section" title="3.Basic editing and editors">
       
   253 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   254 <a name="id2824947"></a>3.Basic editing and editors</h2></div></div></div>
       
   255 <div class="section" title="3.1.vim">
       
   256 <div class="titlepage"><div><div><h3 class="title">
       
   257 <a name="id2824956"></a>3.1.vim</h3></div></div></div>
       
   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>
       
   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>
       
   260 <pre class="programlisting">
       
   261 $ vim first.txt</pre>
       
   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>
       
   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>
       
   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>
       
   265 <p id="ch10session4_20">To save a file and continue editing, use <code class="literal">:w FILENAME</code>
       
   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>
       
   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>
       
   268 <p id="ch10session4_22">To save and quit, use <code class="literal">:wq</code></p>
       
   269 <p id="ch10session4_23">To quit, use <code class="literal">:q</code></p>
       
   270 <p id="ch10session4_24">To quit without saving, use <code class="literal">:q!</code></p>
       
   271 <div class="section" title="3.1.1.Moving around">
       
   272 <div class="titlepage"><div><div><h4 class="title">
       
   273 <a name="id2825077"></a>3.1.1.Moving around</h4></div></div></div>
       
   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>
       
   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>
       
   276 <pre class="programlisting">
       
   277       ^
       
   278       k
       
   279 &lt; h       l &gt;
       
   280       j
       
   281       v</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>
       
   283 <div class="section" title="3.1.1.1.Moving within a line">
       
   284 <div class="titlepage"><div><div><h5 class="title">
       
   285 <a name="id2825129"></a>3.1.1.1.Moving within a line</h5></div></div></div>
       
   286 <p id="ch10session4_28">Cursor Movement</p>
       
   287 <p id="ch10session4_29">Command</p>
       
   288 <p id="ch10session4_2a">Forward by a screenful of text</p>
       
   289 <p id="ch10session4_2b"><code class="literal">C-f</code></p>
       
   290 <p id="ch10session4_2c">Backward by a screenful of text</p>
       
   291 <p id="ch10session4_2d"><code class="literal">C-b</code></p>
       
   292 <p id="ch10session4_2e">Beginning of the screen</p>
       
   293 <p id="ch10session4_2f"><code class="literal">H</code></p>
       
   294 <p id="ch10session4_30">Middle of the screen</p>
       
   295 <p id="ch10session4_31"><code class="literal">M</code></p>
       
   296 <p id="ch10session4_32">End of the screen</p>
       
   297 <p id="ch10session4_33"><code class="literal">L</code></p>
       
   298 <p id="ch10session4_34">End of file</p>
       
   299 <p id="ch10session4_35"><code class="literal">G</code></p>
       
   300 <p id="ch10session4_36">Line number <code class="literal">num</code></p>
       
   301 <p id="ch10session4_37"><code class="literal">[num]G</code></p>
       
   302 <p id="ch10session4_38">Beginning of file</p>
       
   303 <p id="ch10session4_39"><code class="literal">gg</code></p>
       
   304 <p id="ch10session4_3a">Next occurrence of the text
       
   305 under the cursor</p>
       
   306 <p id="ch10session4_3b"><code class="literal">*</code></p>
       
   307 <p id="ch10session4_3c">Previous occurrence of the text
       
   308 under the cursor</p>
       
   309 <p id="ch10session4_3d"><code class="literal">#</code></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>
       
   311 </div>
       
   312 </div>
       
   313 <div class="section" title="3.1.2.The visual mode">
       
   314 <div class="titlepage"><div><div><h4 class="title">
       
   315 <a name="id2825272"></a>3.1.2.The visual mode</h4></div></div></div>
       
   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>
       
   317 </div>
       
   318 <div class="section" title="3.1.3.Editing commands">
       
   319 <div class="titlepage"><div><div><h4 class="title">
       
   320 <a name="id2825290"></a>3.1.3.Editing commands</h4></div></div></div>
       
   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>
       
   322 <p id="ch10session4_41">Editing effect</p>
       
   323 <p id="ch10session4_42">Command</p>
       
   324 <p id="ch10session4_43">Cut the character under the cursor</p>
       
   325 <p id="ch10session4_44"><code class="literal">x</code></p>
       
   326 <p id="ch10session4_45">Replace the character under the
       
   327 cursor with <code class="literal">a</code></p>
       
   328 <p id="ch10session4_46"><code class="literal">ra</code></p>
       
   329 <p id="ch10session4_47">Cut an entire line</p>
       
   330 <p id="ch10session4_48"><code class="literal">dd</code></p>
       
   331 <p id="ch10session4_49">Copy/yank an entire line</p>
       
   332 <p id="ch10session4_4a"><code class="literal">yy</code></p>
       
   333 <p id="ch10session4_4b">Note: You can prefix numbers to any of the commands, to repeat them.</p>
       
   334 </div>
       
   335 <div class="section" title="3.1.4.Undo and Redo">
       
   336 <div class="titlepage"><div><div><h4 class="title">
       
   337 <a name="id2825370"></a>3.1.4.Undo and Redo</h4></div></div></div>
       
   338 <p id="ch10session4_4c">You can undo almost anything using <code class="literal">u</code>.</p>
       
   339 <p id="ch10session4_4d">To undo the undo command type <code class="literal">C-r</code></p>
       
   340 </div>
       
   341 <div class="section" title="3.1.5.Searching and Replacing">
       
   342 <div class="titlepage"><div><div><h4 class="title">
       
   343 <a name="id2825395"></a>3.1.5.Searching and Replacing</h4></div></div></div>
       
   344 <p id="ch10session4_4e">Finding and Replacing</p>
       
   345 <p id="ch10session4_4f">Command</p>
       
   346 <p id="ch10session4_50">Replace the first instance of <code class="literal">old</code>
       
   347 with <code class="literal">new</code> in the current line.</p>
       
   348 <p id="ch10session4_51"><code class="literal">:s/old/new</code></p>
       
   349 <p id="ch10session4_52">Replace all instances of <code class="literal">old</code>
       
   350 with <code class="literal">new</code> in the current line.</p>
       
   351 <p id="ch10session4_53"><code class="literal">:s/old/new/g</code></p>
       
   352 <p id="ch10session4_54">Replace all instances of <code class="literal">old</code>
       
   353 with <code class="literal">new</code> in the current line,
       
   354 but ask for confirmation each time.</p>
       
   355 <p id="ch10session4_55"><code class="literal">:s/old/new/gc</code></p>
       
   356 <p id="ch10session4_56">Replace the first instance of <code class="literal">old</code>
       
   357 with <code class="literal">new</code> in the entire file.</p>
       
   358 <p id="ch10session4_57"><code class="literal">:%s/old/new</code></p>
       
   359 <p id="ch10session4_58">Replace all instances of <code class="literal">old</code>
       
   360 with <code class="literal">new</code> in the entire file.</p>
       
   361 <p id="ch10session4_59"><code class="literal">:%s/old/new/g</code></p>
       
   362 <p id="ch10session4_5a">Replace all instances of <code class="literal">old</code> with
       
   363 <code class="literal">new</code> in the entire file but ask
       
   364 for confirmation each time.</p>
       
   365 <p id="ch10session4_5b"><code class="literal">:%s/old/new/gc</code></p>
       
   366 </div>
       
   367 </div>
       
   368 <div class="section" title="3.2.SciTE">
       
   369 <div class="titlepage"><div><div><h3 class="title">
       
   370 <a name="id2825522"></a>3.2.SciTE</h3></div></div></div>
       
   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>
       
   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>
       
   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>
       
   374 <p id="ch10session4_5f">SciTE also gives you the option to (compile and) run your code, from within the editor.</p>
       
   375 </div>
       
   376 </div>
       
   377 <div class="section" title="4.Personalizing your Environment">
       
   378 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   379 <a name="id2825567"></a>4.Personalizing your Environment</h2></div></div></div>
       
   380 <div class="section" title="4.1..bashrc">
       
   381 <div class="titlepage"><div><div><h3 class="title">
       
   382 <a name="id2825576"></a>4.1..bashrc</h3></div></div></div>
       
   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>
       
   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>
       
   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>
       
   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>
       
   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>
       
   388 </div>
       
   389 <div class="section" title="4.2..vimrc">
       
   390 <div class="titlepage"><div><div><h3 class="title">
       
   391 <a name="id2825876"></a>4.2..vimrc</h3></div></div></div>
       
   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>
       
   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>
       
   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>
       
   395 <p id="ch10session4_68">Command</p>
       
   396 <p id="ch10session4_69">Vim action</p>
       
   397 <p id="ch10session4_6a"><code class="literal">set nocompatible</code></p>
       
   398 <p id="ch10session4_6b">Explicitly disable compatibility with vi</p>
       
   399 <p id="ch10session4_6c"><code class="literal">set backspace=indent,eol,start</code></p>
       
   400 <p id="ch10session4_6d">In the insert mode, vim allows the backspace key to delete white spaces at the
       
   401 start of line, line breaks and the character before which insert mode started.</p>
       
   402 <p id="ch10session4_6e">set autoindent</p>
       
   403 <p id="ch10session4_6f">Vim indents a new line with the same indentation of the previous line.</p>
       
   404 <p id="ch10session4_70">set backup</p>
       
   405 <p id="ch10session4_71">Vim keeps a backup copy of a file when overwriting it.</p>
       
   406 <p id="ch10session4_72">set history=50</p>
       
   407 <p id="ch10session4_73">Vim keeps 50 commands and 50 search patterns in the history.</p>
       
   408 <p id="ch10session4_74">set ruler</p>
       
   409 <p id="ch10session4_75">Displays the current cursor position in the lower right corner of the vim window.</p>
       
   410 <p id="ch10session4_76">set showcmd</p>
       
   411 <p id="ch10session4_77">Displays the incomplete command in the lower right corner.</p>
       
   412 <p id="ch10session4_78">set incsearch</p>
       
   413 <p id="ch10session4_79">Turns on incremental searching. Displays search results while you type.</p>
       
   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>
       
   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>
       
   416 </div>
       
   417 </div>
       
   418 <div class="section" title="5.Subshells and source">
       
   419 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   420 <a name="id2826095"></a>5.Subshells and <code class="literal">source</code>
       
   421 </h2></div></div></div>
       
   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>
       
   423 <div class="itemizedlist"><ul class="itemizedlist" type="*">
       
   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>
       
   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>
       
   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>
       
   427 </ul></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>
       
   429 <pre class="programlisting">
       
   430 $ source script.sh</pre>
       
   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>
       
   432 </div>
       
   433 </div>
       
   434 </div></body>
       
   435 </html>
       
   436