Tuesday 24 November 2015

VIM Tutorial | Getting Started with VIM

VIM Tutorial

A Video Tutorial to get started with the most powerful command line editor "VIM".  Vim editor has many features and functions that can help us any programmer to be more productive. So lets start our journey with "vim".

Installation process

To install vim on Ubuntu (debian based flavor of Linux), use the following command:


sudo apt-get install vim


If you are using operating system other than Ubuntu, then you can get help here.

Editing with vim

To use vim you can use command either vim or vi. Now move ahead step by step:

1. Create a file using vim:
    Let suppose file name is file.txt, then to create it, issue the following command


vim file.txt


It will open a blank file named as file.txt. You can see the file name at the bottom of editor with status [new file].

2. Insert text in a file using vim:
    Now, we have file.txt opned in a vim editor. To insert text in it or write in a file, we will require to go into insert mode. For this press  i . You can see that insert mode is on as it is mentioned at the bottom of editor with -- INSERT --. Now you can write anything, you want to save in file.txt.

3. Exit from vim editor:
    There are two methods to exit from vim editor:
    a. Without saving text in created file 
    b. With saving text in created file

    Before performing above task, lets know about one concept about vim that is mode. Vim has two mode, first one is insert mode (we discussed above) and second one is command mode. Command mode is used to apply vim commands. To insert in this mode, press escape key (esc). After pressing, you will see -- INSERT -- is now gone from the bottom of editor. You are now, not able to write in the file.
Note: To enter in insert mode you can press i again. By default file remains in command mode (when file get open).

  
a. Without saving text in created file: First enter into command mode, then press colon and q like  :q . Here "q" stands for quit.
    b. With saving text in created file: For this enter into command mode first (If you are not already) and then press colon, w and q like  :wq .
    So. you learned how to create a file, insert data into it and exit from it through vim. Lets move further.
4. See content of file or open a file:
    To see the content of any file, just use vim command with file name (as to create a file)
   
vim <name of file>

  
for example to see the content of our created file.txt file, you can write command:

vim file.txt

 5. Move into file:
     To move into file, either you can use arrow keys or you can use J, K, L, H keys. Pressing  will move down your curser, pressing  will move curser up, pressing  will move curser right an will move curser left.

      i. To move at the end of file: Press Shift + G, it will move curser, directly at the last line of the file.
      ii. To move at the end of line: Press Shift + $, it will move curser, directly at the end of line.
      iii. To move at start of line: Press Shift + ^, it will move curser, directly at the start of line.
      
  6. To write in a new line: 
      If you are in a command mode, and want to write data on a new line (new can be between existing lines or after last line of the file), simply press  O . No need to enter in insert mode and the pressing enter. Its a pretty shortcut.

7. Exit fro file without saving last changes:
    If any case, you don't want last changes to be saved in a file, but without hurting previous content of file. So first press esc to enter in command mode and then press colon, q and excexclamatory mark key like  :q!
 
8. Copy or Cut and paste the content of file:
    To copy content of a line, first data selection is required. To select data to copy, enter into visual mode. For this press  key (being in a command mode, not in insert mode). You can see -- ViSUAL -- at bottom of editor. Now select content in usual manner using Shift + arrow keys. Selected content will get highlighted.

    a. To copy: Press  key, now your content is get copied.
    b. To cut: Press   D  key, now your content is get cut.
    c. To paste: move at appropriate place, and press  P . Now your selected content get pasted at desired place.

 9. UNDO changes:
     To discard or UNDO changes, press  U  key, now your last changes are gone. To UNDO more, press  U  key again untill you want. Vim allow to UNDO till oldest change, when you saved the content.

So it is all about basic operations with file. Enjoy vim. 

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel