ult/session4.rst
author Puneeth Chaganti <puneeth@fossee.in>
Thu, 17 Sep 2009 13:47:31 +0530
changeset 57 042767d3dd0d
child 58 88ab79104a62
permissions -rw-r--r--
Added Session 4 of ULT; Scite tut to be done.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     1
More text processing
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     2
====================
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     3
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     4
``sort``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     5
--------
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     6
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     7
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     8
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
     9
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    10
  Richard Stallman%rms%GNU Project
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    11
  Eric Raymond%ESR%Jargon File
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    12
  Ian Murdock% %Debian
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    13
  Lawrence Lessig% %Creative Commons
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    14
  Linus Torvalds% %Linux Kernel
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    15
  Guido van Rossum%BDFL%Python
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    16
  Larry Wall% %Perl
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    17
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    18
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    19
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    20
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    21
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    22
  $ sort stalwarts.txt 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    23
  Eric Raymond%ESR%Jargon File
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    24
  Guido van Rossum%BDFL%Python
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    25
  Ian Murdock% %Debian
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    26
  Larry Wall% %Perl
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    27
  Lawrence Lessig% %Creative Commons
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    28
  Linus Torvalds% %Linux Kernel
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    29
  Richard Stallman%rms%GNU Project
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    30
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    31
If you wish to sort them reverse alphabetically, you just need to pass the ``-r`` 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? 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    32
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    33
Below is an example that sorts the file based on "other" names. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    34
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    35
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    36
  $ sort -t % -k 2,2  stalwarts.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    37
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    38
  Ian Murdock% %Debian
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    39
  Larry Wall% %Perl
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    40
  Lawrence Lessig% %Creative Commons
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    41
  Linus Torvalds% %Linux Kernel
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    42
  Guido van Rossum%BDFL%Python
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    43
  Eric Raymond%ESR%Jargon File
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    44
  Richard Stallman%rms%GNU Project
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    45
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    46
Sort command assumes white space to be the default delimiter for columns in each line. The ``-t`` option specifies the delimiting character, which is ``%`` in this case. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    47
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    48
The ``-k`` 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. ``sort`` 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). 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    49
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    50
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    51
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    52
  $ sort -t % -k 2,2 -k 3,3  stalwarts.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    53
  
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    54
  Lawrence Lessig% %Creative Commons
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    55
  Ian Murdock% %Debian
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    56
  Linus Torvalds% %Linux Kernel
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    57
  Larry Wall% %Perl
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    58
  Guido van Rossum%BDFL%Python
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    59
  Eric Raymond%ESR%Jargon File
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    60
  Richard Stallman%rms%GNU Project
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    61
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    62
``sort`` also has a lot of other options like ignoring case differences, month sort(JAN<FEB<...), merging already sorted files. ``man sort`` would give you a lot of information. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    63
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    64
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    65
``uniq``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    66
--------
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    67
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    68
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 ``uniq`` command to achieve this. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    69
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    70
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    71
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    72
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    73
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    74
  The Mythical Man Month: Essays on Software Engineering 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    75
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    76
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    77
  Structure and Interpretation of Computer Programs
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    78
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    79
  Compilers: Principles, Techniques, and Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    80
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    81
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    82
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    83
  The Art of Computer Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    84
  Introduction to Algorithms
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    85
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    86
  The Pragmatic Programmer: From Journeyman to Master
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    87
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    88
  Unix Power Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    89
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    90
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    91
Let us try and get rid of the duplicate lines from this file using the ``uniq`` command. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    92
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    93
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    94
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    95
  $ uniq items.txt 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    96
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    97
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    98
  The Mythical Man Month: Essays on Software Engineering 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
    99
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   100
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   101
  Structure and Interpretation of Computer Programs
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   102
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   103
  Compilers: Principles, Techniques, and Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   104
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   105
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   106
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   107
  The Art of Computer Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   108
  Introduction to Algorithms
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   109
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   110
  The Pragmatic Programmer: From Journeyman to Master
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   111
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   112
  Unix Power Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   113
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   114
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   115
Nothing happens! Why? The ``uniq`` 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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   116
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   117
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   118
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   119
  $ sort items.txt > items-sorted.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   120
  $ uniq items-sorted.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   121
  Compilers: Principles, Techniques, and Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   122
  Introduction to Algorithms
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   123
  Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   124
  Structure and Interpretation of Computer Programs
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   125
  The Art of Computer Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   126
  The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   127
  The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   128
  The Mythical Man Month: Essays on Software Engineering 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   129
  The Pragmatic Programmer: From Journeyman to Master
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   130
  Unix Power Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   131
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   132
``uniq -u`` command gives the lines which are unique and do not have any duplicates in the file. ``uniq -d`` outputs only those lines which have duplicates. The ``-c`` option displays the number of times each line occurs in the file. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   133
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   134
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   135
  $ uniq -u items-sorted.txt 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   136
  Compilers: Principles, Techniques, and Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   137
  Introduction to Algorithms
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   138
  Structure and Interpretation of Computer Programs
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   139
  The Art of Computer Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   140
  The Mythical Man Month: Essays on Software Engineering 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   141
  The Pragmatic Programmer: From Journeyman to Master
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   142
  Unix Power Tools
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   143
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   144
  $ uniq -dc items-sorted.txt      
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   145
  5 Programming Pearls
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   146
  3 The Art of UNIX Programming
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   147
  3 The C Programming Language
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   148
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   149
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   150
``join``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   151
--------
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   152
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   153
Now suppose we had the file ``stalwarts1.txt``, which lists the home pages of all the people listed in ``stalwarts.txt``.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   154
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   155
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   156
  Richard Stallman%http://www.stallman.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   157
  Eric Raymond%http://www.catb.org/~esr/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   158
  Ian Murdock%http://ianmurdock.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   159
  Lawrence Lessig%http://lessig.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   160
  Linus Torvalds%http://torvalds-family.blogspot.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   161
  Guido van Rossum%http://www.python.org/~guido/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   162
  Larry Wall%http://www.wall.org/~larry/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   163
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   164
It would be nice to have a single file with the information in both the files. To achieve this we use the ``join`` command. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   165
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   166
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   167
  $ join stalwarts.txt stalwarts1.txt -t %
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   168
  Richard Stallman%rms%GNU Project%http://www.stallman.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   169
  Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   170
  Ian Murdock% %Debian%http://ianmurdock.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   171
  Lawrence Lessig% %Creative Commons%http://lessig.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   172
  Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   173
  Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   174
  Larry Wall% %Perl%http://www.wall.org/~larry/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   175
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   176
The ``join`` command joins the two files, based on the common field present in both the files, which is the name, in this case. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   177
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   178
The ``-t`` option again specifies the delimiting character. Unless that is specified, join assumes that the fields are separated by spaces. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   179
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   180
Note that, for ``join`` to work, the common field should be in the same order in both the files. If this is not so, you could use ``sort``, 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 ``-1`` and ``-2`` options to specify the field to be used for joining the files. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   181
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   182
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   183
  $ join -2 2 stalwarts.txt stalwarts2.txt -t %
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   184
  Richard Stallman%rms%GNU Project%http://www.stallman.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   185
  Eric Raymond%ESR%Jargon File%http://www.catb.org/~esr/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   186
  Ian Murdock% %Debian%http://ianmurdock.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   187
  Lawrence Lessig% %Creative Commons%http://lessig.org
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   188
  Linus Torvalds% %Linux Kernel%http://torvalds-family.blogspot.com/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   189
  Guido van Rossum%BDFL%Python%http://www.python.org/~guido/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   190
  Larry Wall% %Perl%http://www.wall.org/~larry/
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   191
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   192
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   193
Generating a word frequency list
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   194
================================
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   195
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   196
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.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   197
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   198
The basic steps to achieve this task would be -
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   199
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   200
1. Eliminate the punctuation and spaces from the document. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   201
2. Generate a list of words.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   202
3. Count the words.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   203
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   204
We first use ``grep`` and some elementary ``regex`` to eliminate the non-alpha-characters. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   205
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   206
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   207
  $ grep "[A-Za-z]*" alice-in-wonderland.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   208
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   209
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. ``man grep`` shows us the ``-o`` option for outputting only the text which matches the regular expression.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   210
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   211
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   212
  $ grep "[A-Za-z]*" -o alice-in-wonderland.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   213
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   214
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 ``sort`` and ``uniq`` commands. We pipe the output from the ``grep`` to the ``sort`` and then pipe it's output to ``uniq``.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   215
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   216
  
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   217
  $ grep "[A-Za-z]*" -o alice-in-wonderland.txt | sort | uniq -c 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   218
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   219
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 ``tr`` command. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   220
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   221
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   222
  $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   223
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   224
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 ``uniq`` command with ``-n`` and ``-r`` options, to get the desired output. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   225
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   226
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   227
  $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   228
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   229
Basic editing and editors
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   230
=========================
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   231
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   232
vim
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   233
---
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   234
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   235
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   236
To open a file in vim, we pass the filename as a parameter to the ``vim`` command. If a file with that filename does not exist, a new file is created. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   237
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   238
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   239
  $ vim first.txt
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   240
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   241
To start inserting text into the new file that we have opened, we need to press the ``i`` key. This will take us into the *insert* mode from the *command* mode. Hitting the ``esc`` key, will bring us back to the *command* mode. There is also another mode of vim, called the *visual* mode which will be discussed later in the course. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   242
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   243
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   244
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   245
To save the file, use ``:w`` 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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   246
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   247
To save a file and continue editing, use ``:w FILENAME``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   248
The file name is optional. If you donot 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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   249
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   250
To save file with a new name and continue editing the new file, use ``:saveas FILENAME``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   251
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   252
To save and quit, use ``:wq``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   253
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   254
To quit, use ``:q``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   255
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   256
To quit without saving, use ``:q!``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   257
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   258
Moving around
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   259
~~~~~~~~~~~~~
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   260
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   261
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   262
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   263
The basic cursor movement can be achieved using the keys, ``h`` (left), ``l`` (right), ``k`` (up) and ``j`` (down). 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   264
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   265
 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   266
             ^
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   267
             k              
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   268
       < h       l >        
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   269
             j              
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   270
             v
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   271
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   272
Note: Most commands can be prefixed with a number, to repeat the command. For instance, ``10j`` will move the cursor down 10 lines. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   273
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   274
Moving within a line
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   275
++++++++++++++++++++
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   276
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   277
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   278
| Cursor Movement                        | Command | 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   279
+========================================+=========+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   280
| Beginning of line                      | ``0``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   281
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   282
| First non-space character of line      | ``^``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   283
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   284
| End of line                            | ``$``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   285
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   286
| Last non-space character of line       | ``g_``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   287
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   288
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   289
Moving by words and sentences
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   290
+++++++++++++++++++++++++++++
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   291
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   292
| Cursor Movement              | Command |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   293
+==============================+=========+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   294
| Forward, word beginning      | ``w``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   295
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   296
| Backward, word beginning     | ``b``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   297
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   298
| Forward, word end            | ``e``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   299
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   300
| Backward, word end           | ``ge``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   301
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   302
| Forward, sentence beginning  | ``)``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   303
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   304
| Backward, sentence beginning | ``(``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   305
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   306
| Forward, paragraph beginning | ``}``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   307
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   308
| Backward, paragraph beginning| ``{``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   309
+------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   310
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   311
More movement commands
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   312
++++++++++++++++++++++
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   313
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   314
| Cursor Movement                 | Command    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   315
+=================================+============+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   316
| Forward by a screenful of text  | ``C-f``    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   317
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   318
| Backward by a screenful of text | ``C-b``    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   319
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   320
| Beginning of the screen         | ``H``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   321
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   322
| Middle of the screen            | ``M``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   323
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   324
| End of the screen               | ``L``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   325
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   326
| End of file                     | ``G``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   327
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   328
| Line number ``num``             | ``[num]G`` |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   329
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   330
| Beginning of file               | ``gg``     |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   331
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   332
| Next occurrence of the text     | ``*``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   333
| under the cursor                |            |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   334
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   335
| Previous occurrence of the text | ``#``      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   336
| under the cursor                |            |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   337
+---------------------------------+------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   338
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   339
Note: ``C-x`` is ``Ctrl`` + ``x``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   340
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   341
The visual mode
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   342
~~~~~~~~~~~~~~~
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   343
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   344
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   345
Editing commands
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   346
~~~~~~~~~~~~~~~~
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   347
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   348
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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   349
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   350
+-------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   351
| Editing effect          | Command |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   352
+=========================+=========+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   353
| Cutting text            | ``d``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   354
+-------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   355
| Copying/Yanking text    | ``y``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   356
+-------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   357
| Pasting copied/cut text | ``p``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   358
+-------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   359
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   360
The cut and copy commands take the motions or visual blocks as arguments and act on them. For instance, if you wish to delete the text from the current text position to the beginning of the next word, type ``dw``. If you wish to copy the text from the current position to the end of this sentence, type ``y)``.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   361
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   362
Apart from the above commands, that take any motion or visual block as an argument, there are additional special commands. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   363
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   364
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   365
| Editing effect                         | Command | 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   366
+========================================+=========+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   367
| Cut the character under the cursor     | ``x``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   368
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   369
| Replace the character under the        | ``ra``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   370
| cursor with ``a``                      |         |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   371
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   372
| Cut an entire line                     | ``dd``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   373
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   374
| Copy/yank an entire line               | ``yy``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   375
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   376
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   377
Note: You can prefix numbers to any of the commands, to repeat them.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   378
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   379
Undo and Redo
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   380
~~~~~~~~~~~~~
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   381
You can undo almost anything using ``u``. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   382
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   383
To undo the undo command type ``C-r``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   384
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   385
Searching and Replacing
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   386
~~~~~~~~~~~~~~~~~~~~~~~
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   387
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   388
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   389
| Finding                                          |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   390
+========================================+=========+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   391
| Next occurence of ``text``, forward    |``\text``|
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   392
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   393
| Next occurence of ``text``, backward   |``?text``|
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   394
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   395
| Search again in the same direction     | ``n``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   396
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   397
| Search again in the opposite direction | ``N``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   398
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   399
| Next occurence of ``x`` in the line    | ``fx``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   400
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   401
| Previous occurence of ``x`` in the line| ``Fx``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   402
+----------------------------------------+---------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   403
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   404
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   405
| Finding and Replacing                                    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   406
+=======================================+==================+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   407
| Replace the first instance of ``old`` |``:s/old/new``    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   408
| with ``new`` in the current line.     |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   409
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   410
| Replace all instances of ``old``      |``:s/old/new/g``  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   411
| with ``new`` in the current line.     |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   412
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   413
| Replace all instances of ``old``      |``:s/old/new/gc`` |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   414
| with ``new`` in the current line,     |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   415
| but ask for confirmation each time.   |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   416
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   417
| Replace the first instance of ``old`` |``:%s/old/new``   |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   418
| with ``new`` in the entire file.      |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   419
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   420
| Replace all instances of ``old``      |``:%s/old/new/g`` |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   421
| with ``new`` in the entire file.      |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   422
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   423
| Replace all instances of ``old`` with |``:%s/old/new/gc``|
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   424
| ``new`` in the entire file but ask    |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   425
| for confirmation each time.           |                  |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   426
+---------------------------------------+------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   427
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   428
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   429
scite
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   430
-----
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   431
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   432
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   433
Personalizing your Environment
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   434
==============================
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   435
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   436
.bashrc
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   437
-------
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   438
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 startup files, when it starts up. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   439
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   440
When bash starts up as an interactive login shell, it reads the files ``/etc/profile``, ``~/.bash_profile``, ``~/.bash_login``, and ``~/.profile`` in that order. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   441
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   442
When it is a shell that is not a login shell, ``~/.bashrc`` is read and the commands in it are executed. This can be prevented using the ``--norc`` option. To force bash to use another file, instead of the ``~/.bashrc`` file on startup, the ``--rcfile`` option may be used. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   443
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   444
Now, you know what you should do, to change the current directory to you Desktop. Just put a ``cd ~/Desktop`` into your ``~/.bashrc`` and you are set!
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   445
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   446
This example is quite a simple and lame one. The startup 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 ``.bashrc``. We shall look at them, in the next section where we look at environment variables and ``set`` command.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   447
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   448
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   449
.vimrc
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   450
------
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   451
``.vimrc`` is a file similar to ``.bashrc`` for vim. It is a startup 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 ``.vimrc`` file, so that they are automatically set each time vim starts. The recommended place for having your ``.vimrc`` is also your home directory. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   452
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   453
The file ``/etc/vimrc`` is the global config file and shouldn't usually be edited. You can instead edit the ``~/.vimrc`` file that is present in your home folder. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   454
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   455
There are a whole bunch of variables that you could set in the ``.vimrc`` file. You can look at all the options available, using the ``:set all`` command in vim. You could use the ``:help option_name`` 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 ``.vimrc``. You should also look at ``:help vimrc`` for more info on the ``.vimrc`` file. If you already have a ``.vimrc`` file, you can edit it from within vim, using ``:e $MYVIMRC`` command. We shall look at some of the most commonly used options. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   456
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   457
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   458
|Command                           | Vim action                                                                        |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   459
+==================================+===================================================================================+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   460
|``set nocompatible``              | Explicitly disable compatibility with vi                                          |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   461
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   462
|``set backspace=indent,eol,start``| In the insert mode, vim allows the backspace key to delete white spaces at the    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   463
|                                  | start of line, line breaks and the character before which insert mode started.    |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   464
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   465
|set autoindent                    | Vim indents a new line with the same indentation of the previous line.            |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   466
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   467
|set backup                        | Vim keeps a backup copy of a file when overwriting it.                            |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   468
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   469
|set history=50                    | Vim keeps 50 commands and 50 search patterns in the history.                      |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   470
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   471
|set ruler                         | Displays the current cursor position in the lower right corner of the vim window. |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   472
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   473
|set showcmd                       | Displays the incomplete command in the lower right corner.                        |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   474
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   475
|set incsearch                     | Turns on incremental searching. Displays search results while you type.           |
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   476
+----------------------------------+-----------------------------------------------------------------------------------+
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   477
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   478
You can see the effect of the changes made to your ``.vimrc`` file by restarting vim. If you want to see the changes that you made to your ``.vimrc`` file immediately, you could source the file from within vim.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   479
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   480
If the ``.vimrc`` file has been sourced when this instance of vim was started, you could just resource the file again::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   481
  :so $MYVIMRC
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   482
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   483
If you just created the ``.vimrc`` file or it was not sourced when you stared this instance of vim, just replace the ``$MYVIMRC`` variable above, with the location of the ``.vimrc`` file that you created/edited.
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   484
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   485
Subshells and ``source``
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   486
========================
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   487
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   488
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.   
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   489
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   490
  * 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. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   491
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   492
  * Any pipes being used, create a subshell. The commands on the input and output ends of the pipe are run in different subshells. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   493
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   494
  * 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.   
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   495
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   496
To avoid creating a subshell, when running a shell script, you could use the ``source`` command. 
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   497
::
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   498
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   499
  $ source script.sh
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   500
042767d3dd0d Added Session 4 of ULT; Scite tut to be done.
Puneeth Chaganti <puneeth@fossee.in>
parents:
diff changeset
   501
This will run the ``script.sh`` within the present shell without creating a subshell. The ``.`` command is an alias for the source command. ``. script.sh`` is therefore equivalent to ``source script.sh``.