Vi / Vim Editor


Introduction


  • 'vi' is the default editor of RHEL and also the most widely used text editor.
  • It is the also used as the default editor by other UNIX variants. 
  • 'Vim' (Vi iMproved) is the enhanched version of vi editor, a programmer's text editor.

Modes of operation

Vi / Vim operates in one of the following modes
  1. Insert Mode
  2. Command Mode
  3. Ex Mode
  4. Visual Mode


1. Commands of Insert Mode

  • i -----> To insert text before the current cursor pointer
  • a -----> To insert text after the current cursor pointer
  • -----> To insert text from the beginning of the current line
  • -----> To insert text from the end of the current line
  • -----> To open a New line below the current one
  • -----> To open a New line above the current one
  • -----> Substitute the current character
  • -----> Substitute the current line
  • -----> To replace the current character
  • -----> To replace the current line
  • 'Esc'-----> To come out of the current line


2. Commands of Command Mode


 * Cursor Movement

  • -----> To move one character left
  • -----> To move one character right
  • -----> To move one line down
  • '<Enter>' -----> To move one line down
  • -----> To move one line up
  • -----> To move to the beginning of the current line
  • -----> To move to the end of the current line
  • gg -----> To move to the top of the file
  • -----> To move to the bottom of the file
  • 'N'G -----> To move to Nth line of the file
  • -----> To move one word ahead
  • -----> To move one word back
  • ----->  To move one sentence ahead
  • -----> To move one sentence back
  • -----> To move one para ahead
  • -----> To move one para back  
  • :set number ----------> To show line number before each line (Temporarily)


3. Commands of Ex - Mode


   * Searching Text

  • /text -----> To perform search in forward direction (Top-to-bottom search)
  • ?txt -----> To perform search in reverse direction (Bottom-to-top search)
  • -----> To search next occurance of the searched text in forward direction
  • ----->To search must occurance of the searched text in reverse direction

  * Save and exit

  • ----->  To save the changes
  • :wq or :x -----> To save the changes into the current file and exit
  • :wq <filename> -----> To save the changes in the given file and exit
  • :wq ! -----> To forcefully save into the read-only file
  • q! -----> To exit without saving the changes


4. Commands of Visual Mode

  • -----> starts line oriented visual mode
  • CTRL + V -----> starts block oriented visual mode
    • -----> To copy the selected text
    • -----> To delete the selected text
    • p -----> To paste the copied / deleted text


5. To password protect a file (supported in Vim Editor only)

     Example:

          vim file1
          .............
          .............(text)
       Esc
      :X -----> Enter encryption key: mypassword
                                      Retype: mypassword
      :wq -----> Save and exit



6. Modifying / Removing password from a password protected file

    Example:

         vim file1
           password ........
      Esc
     :X -----> Enter Encryption Key (Leave Blank to remove password)
                      Retype :  (Leave blank and press enter)
      :wq -----> Save and Exit


7. Recovering a unsaved data of a file after crash


 Suppose, file1 is the file which was not saved and we want to recover:

    ls -a 

(This command will show the .swp file of your original file which was
stored in the virtual memory (swap memory)).

    # vi -r .filename.swp 




 (To restore / recover unsaved data of a file)


 :wq (Save the file and exit)

Now, remove the .swp file after data recovery

   # rm .filename.swp 


    8. Open the file in Read-Only mode 

    # vi -R <filename> 






    If you need any further help, go and try
     man vim  or vim --help   in your terminal






    Thankyou.. :)


     

    Post a Comment

    0 Comments