|
1 <html> |
|
2 <head> |
|
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
4 <title> |
|
5 Chapter. session |
|
6 </title> |
|
7 <link rel="stylesheet" href="/review/support/styles.css" type="text/css" /> |
|
8 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /> |
|
9 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /> |
|
10 <script type="text/javascript" src="/review/support/jquery-min.js"> |
|
11 </script> |
|
12 <script type="text/javascript" src="/review/support/form.js"> |
|
13 </script> |
|
14 <script type="text/javascript" src="/review/support/hsbook.js"> |
|
15 </script> |
|
16 </head> |
|
17 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|
18 <div class="chapter" id="chn10session4"> |
|
19 <div class="titlepage"> |
|
20 </div> |
|
21 <div class="toc"> |
|
22 <p> |
|
23 <b> |
|
24 Table of Contents |
|
25 </b> |
|
26 </p> |
|
27 <dl> |
|
28 <dt> |
|
29 <span class="article"> |
|
30 <a href="#id2501738"> |
|
31 </a> |
|
32 </span> |
|
33 </dt> |
|
34 <dd> |
|
35 <dl> |
|
36 <dt> |
|
37 <span class="section"> |
|
38 <a href="#id2311829"> |
|
39 1. More text processing |
|
40 </a> |
|
41 </span> |
|
42 </dt> |
|
43 <dd> |
|
44 <dl> |
|
45 <dt> |
|
46 <span class="section"> |
|
47 <a href="#id2499853"> |
|
48 1.1. |
|
49 <code class="literal"> |
|
50 sort |
|
51 </code> |
|
52 </a> |
|
53 </span> |
|
54 </dt> |
|
55 <dt> |
|
56 <span class="section"> |
|
57 <a href="#id2552760"> |
|
58 1.2. |
|
59 <code class="literal"> |
|
60 uniq |
|
61 </code> |
|
62 </a> |
|
63 </span> |
|
64 </dt> |
|
65 <dt> |
|
66 <span class="section"> |
|
67 <a href="#id2552862"> |
|
68 1.3. |
|
69 <code class="literal"> |
|
70 join |
|
71 </code> |
|
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 |
|
312 Ian Murdock% %Debian |
|
313 Lawrence Lessig% %Creative Commons |
|
314 Linus Torvalds% %Linux Kernel |
|
315 Guido van Rossum%BDFL%Python |
|
316 Larry Wall% %Perl |
|
317 </pre> |
|
318 <p id="ch10session4_2"> |
|
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. |
|
320 </p> |
|
321 <pre class="programlisting"> |
|
322 $ sort stalwarts.txt |
|
323 Eric Raymond%ESR%Jargon File |
|
324 Guido van Rossum%BDFL%Python |
|
325 Ian Murdock% %Debian |
|
326 Larry Wall% %Perl |
|
327 Lawrence Lessig% %Creative Commons |
|
328 Linus Torvalds% %Linux Kernel |
|
329 Richard Stallman%rms%GNU Project |
|
330 </pre> |
|
331 <p id="ch10session4_3"> |
|
332 If you wish to sort them reverse alphabetically, you just need to pass the |
|
333 <code class="literal"> |
|
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 |
|
344 Ian Murdock% %Debian |
|
345 Larry Wall% %Perl |
|
346 Lawrence Lessig% %Creative Commons |
|
347 Linus Torvalds% %Linux Kernel |
|
348 Guido van Rossum%BDFL%Python |
|
349 Eric Raymond%ESR%Jargon File |
|
350 Richard Stallman%rms%GNU Project |
|
351 </pre> |
|
352 <p id="ch10session4_5"> |
|
353 Sort command assumes white space to be the default delimiter for columns in each line. The |
|
354 <code class="literal"> |
|
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 |
|
377 Lawrence Lessig% %Creative Commons |
|
378 Ian Murdock% %Debian |
|
379 Linus Torvalds% %Linux Kernel |
|
380 Larry Wall% %Perl |
|
381 Guido van Rossum%BDFL%Python |
|
382 Eric Raymond%ESR%Jargon File |
|
383 Richard Stallman%rms%GNU Project |
|
384 </pre> |
|
385 <p id="ch10session4_7"> |
|
386 <code class="literal"> |
|
387 sort |
|
388 </code> |
|
389 also has a lot of other options like ignoring case differences, month sort(JAN<FEB<...), merging already sorted files. |
|
390 <code class="literal"> |
|
391 man sort |
|
392 </code> |
|
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 |
|
421 The Mythical Man Month: Essays on Software Engineering |
|
422 Programming Pearls |
|
423 The C Programming Language |
|
424 Structure and Interpretation of Computer Programs |
|
425 Programming Pearls |
|
426 Compilers: Principles, Techniques, and Tools |
|
427 The C Programming Language |
|
428 The Art of UNIX Programming |
|
429 Programming Pearls |
|
430 The Art of Computer Programming |
|
431 Introduction to Algorithms |
|
432 The Art of UNIX Programming |
|
433 The Pragmatic Programmer: From Journeyman to Master |
|
434 Programming Pearls |
|
435 Unix Power Tools |
|
436 The Art of UNIX Programming |
|
437 </pre> |
|
438 <p id="ch10session4_9"> |
|
439 Let us try and get rid of the duplicate lines from this file using the |
|
440 <code class="literal"> |
|
441 uniq |
|
442 </code> |
|
443 command. |
|
444 </p> |
|
445 <pre class="programlisting"> |
|
446 $ uniq items.txt |
|
447 Programming Pearls |
|
448 The C Programming Language |
|
449 The Mythical Man Month: Essays on Software Engineering |
|
450 Programming Pearls |
|
451 The C Programming Language |
|
452 Structure and Interpretation of Computer Programs |
|
453 Programming Pearls |
|
454 Compilers: Principles, Techniques, and Tools |
|
455 The C Programming Language |
|
456 The Art of UNIX Programming |
|
457 Programming Pearls |
|
458 The Art of Computer Programming |
|
459 Introduction to Algorithms |
|
460 The Art of UNIX Programming |
|
461 The Pragmatic Programmer: From Journeyman to Master |
|
462 Programming Pearls |
|
463 Unix Power Tools |
|
464 The Art of UNIX Programming |
|
465 </pre> |
|
466 <p id="ch10session4_a"> |
|
467 Nothing happens! Why? The |
|
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 > items-sorted.txt |
|
475 $ uniq items-sorted.txt |
|
476 Compilers: Principles, Techniques, and Tools |
|
477 Introduction to Algorithms |
|
478 Programming Pearls |
|
479 Structure and Interpretation of Computer Programs |
|
480 The Art of Computer Programming |
|
481 The Art of UNIX Programming |
|
482 The C Programming Language |
|
483 The Mythical Man Month: Essays on Software Engineering |
|
484 The Pragmatic Programmer: From Journeyman to Master |
|
485 Unix Power Tools |
|
486 </pre> |
|
487 <p id="ch10session4_b"> |
|
488 <code class="literal"> |
|
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 |
|
504 Introduction to Algorithms |
|
505 Structure and Interpretation of Computer Programs |
|
506 The Art of Computer Programming |
|
507 The Mythical Man Month: Essays on Software Engineering |
|
508 The Pragmatic Programmer: From Journeyman to Master |
|
509 Unix Power Tools |
|
510 |
|
511 $ uniq -dc items-sorted.txt |
|
512 5 Programming Pearls |
|
513 3 The Art of UNIX Programming |
|
514 3 The C Programming Language |
|
515 </pre> |
|
516 </div> |
|
517 <div class="section" title="1.3.join"> |
|
518 <div class="titlepage"> |
|
519 <div> |
|
520 <div> |
|
521 <h3 class="title"> |
|
522 <a name="id2552862"> |
|
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/ |
|
546 Ian Murdock%http://ianmurdock.com/ |
|
547 Lawrence Lessig%http://lessig.org |
|
548 Linus Torvalds%http://torvalds-family.blogspot.com/ |
|
549 Guido van Rossum%http://www.python.org/~guido/ |
|
550 Larry Wall%http://www.wall.org/~larry/ |
|
551 </pre> |
|
552 <p id="ch10session4_d"> |
|
553 It would be nice to have a single file with the information in both the files. To achieve this we use the |
|
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 |
|
562 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/ |
|
563 Ian Murdock% %Debian%http://ianmurdock.com/ |
|
564 Lawrence Lessig% %Creative Commons%http://lessig.org |
|
565 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/ |
|
566 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/ |
|
567 Larry Wall% %Perl%http://www.wall.org/~larry/ |
|
568 </pre> |
|
569 <p id="ch10session4_e"> |
|
570 The |
|
571 <code class="literal"> |
|
572 join |
|
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 |
|
605 Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/ |
|
606 Ian Murdock% %Debian%http://ianmurdock.com/ |
|
607 Lawrence Lessig% %Creative Commons%http://lessig.org |
|
608 Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/ |
|
609 Guido van Rossum%BDFL%Python%http://www.python.org/~guido/ |
|
610 Larry Wall% %Perl%http://www.wall.org/~larry/ |
|
611 </pre> |
|
612 </div> |
|
613 </div> |
|
614 <div class="section" title="2.Generating a word frequency list"> |
|
615 <div class="titlepage"> |
|
616 <div> |
|
617 <div> |
|
618 <h2 class="title" style="clear: both"> |
|
619 <a name="id2552962"> |
|
620 </a> |
|
621 2.Generating a word frequency list |
|
622 </h2> |
|
623 </div> |
|
624 </div> |
|
625 </div> |
|
626 <p id="ch10session4_11"> |
|
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. |
|
628 </p> |
|
629 <p id="ch10session4_12"> |
|
630 The basic steps to achieve this task would be - |
|
631 </p> |
|
632 <div class="orderedlist"> |
|
633 <ol class="orderedlist" type="1"> |
|
634 <li class="listitem"> |
|
635 <p id="ch10session4_13"> |
|
636 Eliminate the punctuation and spaces from the document. |
|
637 </p> |
|
638 </li> |
|
639 <li class="listitem"> |
|
640 <p id="ch10session4_14"> |
|
641 Generate a list of words. |
|
642 </p> |
|
643 </li> |
|
644 <li class="listitem"> |
|
645 <p id="ch10session4_15"> |
|
646 Count the words. |
|
647 </p> |
|
648 </li> |
|
649 </ol> |
|
650 </div> |
|
651 <p id="ch10session4_16"> |
|
652 We first use |
|
653 <code class="literal"> |
|
654 grep |
|
655 </code> |
|
656 and some elementary |
|
657 <code class="literal"> |
|
658 regex |
|
659 </code> |
|
660 to eliminate the non-alpha-characters. |
|
661 </p> |
|
662 <pre class="programlisting"> |
|
663 $ grep "[A-Za-z]*" alice-in-wonderland.txt |
|
664 </pre> |
|
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 |
|
884 < h l > |
|
885 j |
|
886 v |
|
887 </pre> |
|
888 <p id="ch10session4_27"> |
|
889 Note: Most commands can be prefixed with a number, to repeat the command. For instance, |
|
890 <code class="literal"> |
|
891 10j |
|
892 </code> |
|
893 will move the cursor down 10 lines. |
|
894 </p> |
|
895 <div class="section" title="3.1.1.1.Moving within a line"> |
|
896 <div class="titlepage"> |
|
897 <div> |
|
898 <div> |
|
899 <h5 class="title"> |
|
900 <a name="id2553291"> |
|
901 </a> |
|
902 3.1.1.1.Moving within a line |
|
903 </h5> |
|
904 </div> |
|
905 </div> |
|
906 </div> |
|
907 <p id="ch10session4_28"> |
|
908 Cursor Movement |
|
909 </p> |
|
910 <p id="ch10session4_29"> |
|
911 Command |
|
912 </p> |
|
913 <p id="ch10session4_2a"> |
|
914 Forward by a screenful of text |
|
915 </p> |
|
916 <p id="ch10session4_2b"> |
|
917 <code class="literal"> |
|
918 C-f |
|
919 </code> |
|
920 </p> |
|
921 <p id="ch10session4_2c"> |
|
922 Backward by a screenful of text |
|
923 </p> |
|
924 <p id="ch10session4_2d"> |
|
925 <code class="literal"> |
|
926 C-b |
|
927 </code> |
|
928 </p> |
|
929 <p id="ch10session4_2e"> |
|
930 Beginning of the screen |
|
931 </p> |
|
932 <p id="ch10session4_2f"> |
|
933 <code class="literal"> |
|
934 H |
|
935 </code> |
|
936 </p> |
|
937 <p id="ch10session4_30"> |
|
938 Middle of the screen |
|
939 </p> |
|
940 <p id="ch10session4_31"> |
|
941 <code class="literal"> |
|
942 M |
|
943 </code> |
|
944 </p> |
|
945 <p id="ch10session4_32"> |
|
946 End of the screen |
|
947 </p> |
|
948 <p id="ch10session4_33"> |
|
949 <code class="literal"> |
|
950 L |
|
951 </code> |
|
952 </p> |
|
953 <p id="ch10session4_34"> |
|
954 End of file |
|
955 </p> |
|
956 <p id="ch10session4_35"> |
|
957 <code class="literal"> |
|
958 G |
|
959 </code> |
|
960 </p> |
|
961 <p id="ch10session4_36"> |
|
962 Line number |
|
963 <code class="literal"> |
|
964 num |
|
965 </code> |
|
966 </p> |
|
967 <p id="ch10session4_37"> |
|
968 <code class="literal"> |
|
969 [num]G |
|
970 </code> |
|
971 </p> |
|
972 <p id="ch10session4_38"> |
|
973 Beginning of file |
|
974 </p> |
|
975 <p id="ch10session4_39"> |
|
976 <code class="literal"> |
|
977 gg |
|
978 </code> |
|
979 </p> |
|
980 <p id="ch10session4_3a"> |
|
981 Next occurrence of the text |
|
982 under the cursor |
|
983 </p> |
|
984 <p id="ch10session4_3b"> |
|
985 <code class="literal"> |
|
986 * |
|
987 </code> |
|
988 </p> |
|
989 <p id="ch10session4_3c"> |
|
990 Previous occurrence of the text |
|
991 under the cursor |
|
992 </p> |
|
993 <p id="ch10session4_3d"> |
|
994 <code class="literal"> |
|
995 # |
|
996 </code> |
|
997 </p> |
|
998 <p id="ch10session4_3e"> |
|
999 Note: |
|
1000 <code class="literal"> |
|
1001 C-x |
|
1002 </code> |
|
1003 is |
|
1004 <code class="literal"> |
|
1005 Ctrl |
|
1006 </code> |
|
1007 + |
|
1008 <code class="literal"> |
|
1009 x |
|
1010 </code> |
|
1011 </p> |
|
1012 </div> |
|
1013 </div> |
|
1014 <div class="section" title="3.1.2.The visual mode"> |
|
1015 <div class="titlepage"> |
|
1016 <div> |
|
1017 <div> |
|
1018 <h4 class="title"> |
|
1019 <a name="id2553434"> |
|
1020 </a> |
|
1021 3.1.2.The visual mode |
|
1022 </h4> |
|
1023 </div> |
|
1024 </div> |
|
1025 </div> |
|
1026 <p id="ch10session4_3f"> |
|
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. |
|
1028 </p> |
|
1029 </div> |
|
1030 <div class="section" title="3.1.3.Editing commands"> |
|
1031 <div class="titlepage"> |
|
1032 <div> |
|
1033 <div> |
|
1034 <h4 class="title"> |
|
1035 <a name="id2553452"> |
|
1036 </a> |
|
1037 3.1.3.Editing commands |
|
1038 </h4> |
|
1039 </div> |
|
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> |