Sunday, January 10, 2010

Important tips in VI editor

  • Reading file into vi:  When we are editing a file in vi, it may so happen we may need to copy the contents of a file into this file. VI provides a way for this using the 'r' command.  Say, we are editing a file f1, and in the 10th line of f1 we want to paste all the contents of file f2. Open the file f1, move the cursor to the 10th line, and give the following command in the escape mode:   
               :r f2    

  • Writing portion of a file to a different file:  When we are editing a file in vi, we may want to write a set of lines in a fresh file. Say, we want to write lines 10 to 20 of the current file f1 to a new file f2. Open the file f1, and give the following command in the escape mode:
               :10,20w f2

          • Set tags in vi:   For programmers,  when we open a file, in order to go to a function definition, we need to close the file, search for the file of the function definition and open the definition file. In places where there are lots of function, this is very cumbersome. Tags can be highly useful here. Once we generate the tags for our function calls, we can set the tags variable in the vi editor, after which on clicking some hot key combination, we can reach the function definition, and similarly with some other hot key combination, we can return to the original file. Issue the following command from the escape mode,
            •             :set tags=tag file
            • Abbreviations in VI: While editing in vi, we may use a particular word lot of times, say subscriber.  And assuming we use this word lot of times, it will be quite time consuming to type this word every time. vi allows us to use abbreviations by which we can have an abbreviation for the subscriber_no word. Also, the abbreviations used are also applied for searching also. Say, if we use sub as the abbreviation for subscriber_no, every time we type sub, it will be automatically be replaced with subscriber_no. To set the abbreviation, go to the escape mode and type:
                    :set sub subscriber_no
              • Swap characters in VI: While editing in vi, lot of times we make spelling mistakes. For example, we may write 'the' as teh'.  In order to correct it, we take the cursor to 'e' and type x command which removes 'e'. And then we go to append mode and type 'e' so it becomes 'the'.  This can easily be achieved using the 'xp' command combination.  x deletes the character and p pastes the character to the right of the character at the cursor. Hence, when 'xp' is issued with cursor at 'e' in 'teh', it becomes 'the'.

              • Mapping keys: vi allows us to map keys to some commands. For example, when we want to quit a file, we issue the command :q!.  We can map this say, to say Q, which means whenever we give 'Q' command, it works like :q!. Once this mapping is done, whenever we give 'Q' in escape mode, it will quit the file.
                          :map  Q :q!

              No comments:

              Post a Comment