Friday 27 June 2014

Vim a beginning

 

Contents

  • Introduction
  • Mode of Operations
  • Commands for basic Operations
    • Opening a File
    • Inserting Text
    • Moving around
    • Copy & Paste
    • Undo & Redo
    • Search & Replace
    • Save & Quit
    • Splitting windows
    • Advance Options
  • Conclusion

 

Introduction

The VI editor is a one of the most powerful text editor which has a very extensive functionalities to allow developer enabling to edit files with a minimum of keystrokes. This power and functionality comes as a cost. This article I am going to explain basic command which helps you to put your hands on in VIM editor to write your program.

Mode of Operations

VIM has three modes of operation.

  1. Command Mode : This is the mode VIM starts in by default. In this mode, most keys on the keyboard are defined to specific command. At any time, pressing the ESC key and typing colon : returns the user to command mode.
  2. Insert Mode: This mode allows a user to start inputing data into file. To reach this mode, press i or o
  3. Visual Mode: This mode allows you to do the visual operation which is common in grapical editors like selecting punch of lines copy/pasting/deleting the text in the editor. Pressing v will enters you in visual mode.

Commands for basic Operations

  • Opening a File

 

 

Commands

Descriptions
vim This will start the empty vim editor
vim [File name] (eg. vim hello.c) This will start vim editing the specified file(or create a new file if no file specified
vim [File name] : [Line No] (eg. vim hello.c :5) This will open the specified file with the cursor point to the given line number.
vim [File name] / [expression] This will open the specified file with the place where the expression matches

 

  • Inserting Text


     

    Commands

    Descriptions
    i Insert a text in after the current cursor position
    I Insert a text before the cursor position
    o Insert a text one line below the cursor position
    O Insert a text one line above the cursor position
    a Append a text after the cursor
    A Append a text before the cursor

 

  • Moving around


     

    Commands

    Descriptions
    k Go up
    j Go down
    h Go left
    i Go right
    w Go write a word
    b Go left a word
    gg Go to the beginning of the file
    G Go to the end of the file
    22j Go to the line no 22
    1 Go to start of the line
    $ Go to end of the line

 

  • Copy & Paste


     

    Commands

    Descriptions
    v Press v to enter visual mode
    Use Arrow keys [k, j h, I ] To select the text to copy
    y To copy the selected text
    p To paste the text
    d To cut the text followed by y to paste
    d To delete the selected text
    yy To copy a Line
    yw To copy a word
    dd To delete/cut a Line
    dw To delete/cut a word
    x To delete a character
    r To replace a character
    cw To replace a word

 

  • Undo & Redo


     

    Commands

    Descriptions
    u Undo the changes
    Ctrl + R Redo the changes

 

  • Search & Replace


     

    Commands

    Descriptions
    /<keyword> after the slash put whatever you want to find and press the enter key)
    n Find next
    N Find previous
    * Pressing star on top of the word you want to search automatically show the search result
    :noh Turn off highlighting (after a search)
    :%s/search/replace/gc It will search and replace the text in the entire document [g mean global] [c mean confirm before replacing the text]
    :1,5s/search/replace/ci It will only replace the text in between line number 1 and 5. [I mean ignore case]

 

  • Save & Quit


     

    Commands [see colon is there before ]

    Descriptions
    :w Save the file
    :!w Save it forcefully to override the readonly files
    :q Close the file
    :q! Close the unsaved file forcefully :
    :wq save and close the file
    :wq! Save and Close the file forcefully
    :x Save and close the file

 

  • Splitting windows


     

    Commands

    Descriptions
    : sp Split the window into two buffer
    : sp filename open the new file in the window
    : vsp Vertically split the window
    : vsp filename Open the file in vertical window
    ctrl + w w Switch between windows

 

  • Advance Options


 

Commands

Descriptions
Indentation

Press v and then arrow keys (or h,j,k,l,w,$) to highlight lines of text.
Type > or < to indent right or left.

: set et Use space for indenting instead of tab
: set ai set auto-indenting
: set si set smart indenting context sensitive indentation
: set nu set numbering in the file
: set nonu Remove the numbering [add “no” to any command to remove it ]
: syntex on Based on the programming the syntex will be enabled.
: ! cmd (eg. :!e filename) opens new file in vim
: !sh Go to shell prompt
ctrl + G Tell the file name and current line number

 

Conclusion

This article provides a very basic command which is used in VIM for day to  day work. As I told VIM is so powerful text editor It has more than 150 commands which provides the different functionality. VIM can be configured as better than modern IDE. It has support of editing all the modern programming language. Please feel free to give your comments in the below section. If you have any query feel free to send a mail to sentenwin@gmail.com

No comments:

Post a Comment